Python

Generate Prime Numbers in Python

In Python, prime numbers are integers that exceed the value of “1” and possess solely two factors, namely “1” and the numbers themselves. For example, “5” is a prime number, but “4” is not because it can be divided by 2. A few prime numbers examples are 2, 11, 23, and 43. To generate prime numbers, different methods are used in Python.

In this article, we will present a comprehensive overview of how to generate prime numbers in Python using numerous examples.

How to Generate/Create Prime Numbers in Python?

To generate/create prime numbers, the following methods are used in Python:

Method 1: Generate Prime Numbers Using the “for” Loop

The simple and easiest way to generate/create prime numbers in Python is by utilizing the for loop:

upper_limit = int(input("Input the range: "))
for num in range(2, upper_limit):
    for divisor in range(2, num):
        if num % divisor == 0:
            break  
    else:
        print(num, end=' ')

In the above code:

  • The input() function is employed to take the upper limit of the range from user input and convert/transform it into an integer utilizing the int() function.
  • After that, the for loop is employed to loop/iterate through numbers from “2” to the upper limit.
  • Next, the nested for loop iterates through numbers from “2” to “num” to check for factors.
  • If the number is divisible by a divisor, then the code breaks out of the inner loop using the break statement.
  • If a number cannot be divided evenly, then the input number is prime and displayed to the output.

Output

Note: We can also use the “While” Loop to generate prime numbers in Python.

Method 2: Generate Prime Numbers Using the “lambda” Function

The lambda function is employed to generate Python prime numbers. Here is an example demonstration:

def Prime_sequence(limit):
    for number in range(2, limit):
        if is_prime(number) == True:
            print(number, end=" ")
        else:
            pass

limit = int(input("Enter the range: "))
is_prime = lambda num: all(num % x != 0 for x in range(2, int(num ** 0.5) + 1))
Prime_sequence(limit)

In the above-given code:

  • First, the function called “Prime_sequence()” is defined with a single parameter named limit.
  • Next, the function uses the “for” loop and the “is_prime” lambda function to verify whether the number is prime.
  • Then, the lambda function is defined and uses the all() function to determine/calculate whether an input number is prime:
  • If the number is evenly divisible by any of the input integers, the lambda function retrieves False, specifying that the number is not prime. Otherwise, it returns True.
  • Finally, the input() function is used to take the user input and pass it to the “Prime_sequence” function. This function finds and prints prime numbers within the specified range.

Output

Method 3: Generate Prime Numbers Using the “Sympy” Library

We can get a list of prime numbers in a certain range by utilizing the sympy.primerange() function:

sympy library.
import sympy
lower = int(input( "Enter Lower Limit : " ))          
upper = int(input( "Enter Upper Limit : " ))          
prime_numbers = list(sympy.primerange(lower,upper+1))  
print("Prime Numbers : " , prime_numbers)

In this code, the “sympy.primerange()” function takes the upper and lower limit user input as an argument and generates the prime numbers. The prime number is converted into a list using the list() function.

Output

How to Check Python Prime Numbers?

To check/determine whether the input number is prime, the following code is used in Python. Here, the function called “check_prime()” is declared in the program, which retrieves the Boolean value whether the number is prime or not:

def check_prime(n):
    if n > 1:
        for i in range(2, n):
            if n % i == 0:
                return False
        return True
    return False

value = int(input("Enter a number to check if it's prime: "))
print(check_prime(value))

The below output verified that the input number is not a prime number:

Conclusion

The “for” loop, “lambda” function, and the “sympy.primerange()” function of the Sympy library are used to generate prime numbers in Python. These methods can generate prime numbers within specified ranges. We also use a custom function to determine/calculate whether the specified number is prime. This guide provided a detailed tutorial on generating prime numbers in Python.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.