Easy Tutorial
❮ Python Os Chown Ref Set Remove ❯

Prime Number Check in Python

Python3 Examples

A natural number greater than 1 that is not divisible by any other natural number (prime numbers) other than 1 and itself (such as 2, 3, 5, 7, etc.), in other words, the number has no other factors besides 1 and itself.

test.py File:

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.tutorialpro.org

# Python program to check if the user input is a prime number

# User input number
num = int(input("Please 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 the input number is less than or equal to 1, it is not a prime number
else:
   print(num, "is not a prime number")

Execution of the above code produces the following results:

$ python3 test.py 
Please enter a number: 1
1 is not a prime number
$ python3 test.py 
Please enter a number: 4
4 is not a prime number
2 times 2 is 4
$ python3 test.py 
Please enter a number: 5
5 is a prime number

Python3 Examples

❮ Python Os Chown Ref Set Remove ❯