How to check if a Python 3 number is prime

Goodnight! I just started training, and here is homework that I can't do for 6 hours) The homework actually sounds like this: "Write a function is_prime that takes 1 argument-a number from 0 to 1000, and returns True if it is simple, and False otherwise."

def is_prime(a):
if a % a == 0 and a != 0:
    return True
else:
    return False

A = int(input("Enter a number: ")) print(is_prime(a))

 0
Author: Sherlock, 2019-10-29

1 answers

Try this :

def is_prime(a):
    if a % 2 == 0:
        return a == 2
    d = 3
    while d * d <= a and a % d != 0:
        d += 2
    return d * d > a

print(is_prime(int(input("Enter a number: "))))

print( [ '{} - True'.format(i) for i in range(2, 1001)  if is_prime(i)]) 
 0
Author: S. Nick, 2019-10-29 23:58:55