# Prime number program in python (4 different way)

- Link: https://technotaught.com/prime-number-program-in-python-3-different-way/
- Published: 2020-03-15T10:01:00+05:30
- Author: Ashishkumar Vishwakarma

In this article, we are going to study ” Prime number program in python in three
different ways”.

### What is a Prime Number?

A positive integer greater than 1 which does not have other factors except 1 and
the number itself is called a prime number. The numbers 2, 3, 5, 7, etc. are prime
numbers as they do not have any other factors. To find a prime number in Python,
you have to iterate the value from start to end using a for loop and for every number,
if it is greater than 1, check if it divides n. If we find any other number which
divides, print that value.

![Prime number program in python](https://technotaught.com/wp-content/uploads/2020/
03/Prime-number-program-in-python.jpg)

There are **different** **ways** to optimize the prime number.

## 1. Python Program to Check Prime Number

A prime number is always positive and it will be checked at the beginning of the
program. Here, you will divide the input number by all the numbers to see whether
there are any positive divisors other than 1 and number itself. If any divisor is
found then we display that the “number is not a prime number” else we display that
the “number is a prime number”.

Example to check whether an integer is a prime number or not using for loop and 
if…else statement.

Source code:

    ```language-git
    num=int(input("Enter prime no: "))
    if num > 1 :
      for n in range(2, num):
        if  (num % n) == 0 :
          print(f"{num} is not Prime Number")
          break
      else:
        print(f"{num} is Prime Number")    
    else:
      print(f"{num} not Prime Number")  
    ```

    ```wp-block-code
    Output-
    Enter prime no: 56
    56 is not Prime Number
    ```

## 2. Python Program to Print all Prime Numbers in an Interval

In this program, you’ll learn to print all prime numbers within an interval using
for loops and display it.

Here, we store the interval as num1 for lower interval and num2 for upper interval
and find prime numbers in that range. Visit this page to learn how to check whether
a number is prime or not.

Source Code:

    ```language-git
    num1 = int(input("Enter first interval: "))
    num2 = int(input("Enter second interval: "))
    for n in range(num1, num2):
      for num2 in range(2,n):
        if  n % num2 == 0 :
          break
      else:
        print(n)    
    ```

    ```wp-block-code
    Output-
    Enter first interval: 10
    Enter second interval: 20
    11
    13
    17
    19
    ```

## 3. Python to Print Prime Numbers and not Prime Number

 In this program, you’ll learn to print all prime numbers and not Prime number at
the same time using for loops and display it.

    ```language-git
    num2 = int(input("Enter Number: "))
    for n in range(2, num2):
      for num2 in range(2,n):
        if  n % num2 == 0 :
          print(f"{n} = Not Prime")
          break
      else:
          print(f"{n} = Prime")    
    ```

    ```wp-block-code
    Output-
    Enter Number: 10
    2 = Prime
    3 = Prime
    4 = Not Prime
    5 = Prime
    6 = Not Prime
    7 = Prime
    8 = Not Prime
    9 = Not Prime
    ```

## 4. Python Program to Print all Prime Numbers using list comprehension

    ```language-markup
    n=int(input())
    primes = [i for i in range(2,n) if all(i%j !=0 for j in range(2,int(i**0.5) + 1))]
    print(primes)
    ```

Output:-

    ```wp-block-verse
    100
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    ```

Source:- [Programiz](https://www.programiz.com/)

Also View:- [List & tuple & set & dictionary in Python](https://technotaught.com/list-tuple-set-dictionary-in-python/)

 So these were some of the different ways to optimize the prime number. Let us know
in the comment section if you know any extra way to solve. 

### Share this:

 * [  X ](https://technotaught.com/prime-number-program-in-python-3-different-way/?share=twitter)
 * [  Facebook ](https://technotaught.com/prime-number-program-in-python-3-different-way/?share=facebook)
 * [More](https://technotaught.com/prime-number-program-in-python-3-different-way/#)

 * [  WhatsApp ](https://technotaught.com/prime-number-program-in-python-3-different-way/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/prime-number-program-in-python-3-different-way/?share=linkedin)
 * [  Email ](https://technotaught.com/prime-number-program-in-python-3-different-way/?subject=%5BShared%20Post%5D%20Prime%20number%20program%20in%20python%20%284%20different%20way%29&body=https%3A%2F%2Ftechnotaught.com%2Fprime-number-program-in-python-3-different-way%2F&share=email)
 * [  Reddit ](https://technotaught.com/prime-number-program-in-python-3-different-way/?share=reddit)
 * [  Tumblr ](https://technotaught.com/prime-number-program-in-python-3-different-way/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/prime-number-program-in-python-3-different-way/?share=pinterest)
 * [  Telegram ](https://technotaught.com/prime-number-program-in-python-3-different-way/?share=telegram)
 * [  Print ](https://technotaught.com/prime-number-program-in-python-3-different-way/#print?share=print)

### _Related_
