新手 -- 方法一:
num = []
for i in range(2, 100):
for j in range(2, i):
if i%j==0 :
break
else:
num.append(i)
print(num)
老手 -- 列表推导式:
from math import sqrt
arr = [p for p in range(2, 100) if 0 not in [p%d for in d in range(2, int(sqrt(p))+1)]]
print(arr)
列表推导式解析:
from math import sqrt
for p in range(2, 100);
for d in range(2, int(sqrt(p))+1):
if p%d==0:
break
else:
print(p)