How to Check if a Number is Prime in Python?

Yo fam! We gonna learn the way to check if a number is prime in Python. We shall use for loop and if..else statement. If the number is not prime, it’s explained in output why it is not a prime number.

Codevarsity
Coding Tutorials
2 min readJan 29, 2019

--

To get this example, you should be knowing for loop, if..else statement, break and continue topics in Python.

So firstly, let’s know what exactly is a prime number?

Prime Numbers Table

A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number. 2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 8 is not prime (it is composite) since, 2 x 4 = 8.

Python Code

# Python program to check if the input number is prime or notnum = 411# take input from the user
# num = int(input(“Enter a number: “))
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,”is not a prime number”)
print(i,”times”,num//i,”is”,num)
break
else:
print(num,”is a prime number”)

# if input number is less than
# or equal to 1, it is not prime
else:
print(num,”is not a prime number”)

Output

411 is not a prime number
3 times 137 is 411

Explanation

In the above program, the variable num is checked if it’s prime or not. The numbers which are less than or equal to 1 are not prime numbers. Hence, we only proceed if the num is greater than 1.

We check if num is exactly divisible by any number from 2 to num-1. If we find a factor in that range, the number is not prime. Else the number is prime.

We can decrease the range of numbers where we look for factors.

In the above program, our search range is from 2 to num-1.

We could have used the range, [2,num/2] or [2,num**0.5]. The later range is based on the fact that a composite number must have a factor less than the square root of that number; otherwise, the number is prime.

You can change the value of the variable num in the above code and test for other integers.

You can also check the similar article on ‘codevarsity.org

--

--