list contains numbers. Write a program for containing only prime numbers.
def is_prime(x):
if x < 2:
return False
if x == 2:
return True
if x % 2 == 0:
return False
i = 3
while i*i <= x:
if x % i == 0:
return False
i += 2
return True
def main():
L = list(range(1,21))
print('L =', L)
L = [x for x in L if is_prime(x)]
print('Prime:', L)
main()
Comments
Leave a comment