Answer to Question #315337 in Python for Michael

Question #315337

list contains numbers. Write a program for containing only prime numbers.


1
Expert's answer
2022-03-21T14:07:53-0400
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS