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)
列表推導式解析:app
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)