Prime Number in Python

In this article, we will guide you through understanding, identifying, and generating Prime Numbers using Python.

What is a Prime Number?

A prime number is a number greater than 1, which is not divisible by any other number except 1 and itself. For example 2, 3,5,7,11 etc.,

Python Code to check Prime Number

steps to check prime number

  • Take a number input from the user
  • First, check if the number is less than 2, then it’s not a prime number
  • else run a loop from i =0 to i =number and create a boolean variable and initialize it true
  • check if the number is divisible by i, if yes then the number is not a prime number, update the boolean variable to false and break the loop
  • end of loop
  • if the boolean variable is true, then the number is a prime number

Python Prime Number Code

number = int(input("enter the number "))

if number<2:
    print("this is not a prime number")
else:
    status = True
    for i in range(2,number):
        if (number%i == 0):
            status = False
            print("this is not a prime number")
            break
        
    if status:
        print("this is a prime number")
    else:
        print("this is not a prime number")    

Output

Prime Number in Python

Happy Coding & Learning

See Also

6 thoughts on “Prime Number in Python”

Leave a Comment