Updated July 28, 2023

## Overview of Prime Numbers in Python

A Prime number can be explained as a finite number that is only divisible by 1 and by itself. It goes on like 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, etc. This series of numbers can be recreated, and any given number can be identified if it is a prime number or not by implementing the logic in the python programming language. A few of the ways for this operation are using python libraries, coding with while loops, coding with loops and conditions, and using the lambda function.

Prime Numbers: 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 etc.

### Techniques to Implement Prime Number in Python

Prime numbers can be implemented in python by several techniques; four among them are explained below:

#### 1. Using Lambda Function

**Code:**

```
# Prime determination method
def Prime_series(number):
for iter in range(2,number):
if is_prime(iter) == True:
print(iter,end = " ")
else:
pass
number = int(input("Enter the input Range : "))
is_prime = lambda number: all( number%i != 0 for i in range(2, int(number**.5)+1) )
Prime_series(number)
```

**Output: **

**Explanation: **This program determines the range of prime numbers using the lambda function technique; lambda represents an anonymous function or an orphan function. The program executes in such a manner that once a specific integer is keyed in by the user, then all the prime numbers within the range of 2 to key in the input will be generated and displayed.

**Program Flow:**

- The input range is keyed in by the user; the python input function is used to receive the user’s input. The received input is handily cast into INT type.
- The casted input is passed as an argument in a function call process. This function is responsible for triggering the lambda function for each and every integer in the given range of 2 to key in the input.
- Hence the lambda function is called for each and every integer, and a prime check is carried on; the below-formulated logic is used for attaining this prime check, number%2 != 0

So as per the above-formulated logic, a Boolean value will be returned; the next step for each verified integer in the function is to check whether the returned Boolean is true or false, in the case when it falls true, then the corresponding variable is printed in the console.

#### 2. Using for Loops and Conditions

**Code:**

```
# Prime determination method
number = int(input("Enter the input Range : "))
for iter in range(2,number):
for i in range(2,iter):
if (iter%i==0):
break
else:
print(iter)
```

**Output: **

**Explanation:**** **This program determines the range of prime numbers using for loops and conditions; the program executes in such a manner that once a specific integer is keyed in by the user, then all the prime numbers within the range of 2 to keyed in input value will be generated and displayed.

**Program Flow:**

- The input range is keyed in by the user; the python input function is used to receive the user’s input. The received input is handily casted into INT type.
- Here, a nested loop is used to determine two integers; the first loop ensures to retrieve all the elements or integers falling within the range keyed. The second for-loop is responsible for determining the denominator of the prime check.
- For every integer value denominated, if the outcome of the process is not equal to zero, then the integer handled is printed to the console.
- This process is looped on and executed for every integer in the mentioned range of 2 to keyed in the input.

#### 3. Using While Loops

**Code:**

```
range = int(input('Enter the integer range to find prime no: '))
number = 1
while(number <= range):
count = 0
i = 2
while(i <= number//2):
if(number % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and number!= 1):
print(" %d" %number, end = ' ')
number = number + 1
```

**Output:**

**Explanation:**** **This program determines the range of prime numbers using while loops and conditions; the program executes in such a manner that once a specific integer is keyed in by the user, then all the prime numbers within the range of 2 to the keyed in the input will be generated and displayed.

**Program Flow:**

- The input range is keyed in by the user; the python input function is used to receive the user’s input. The received input is handily casted into INT type.
- Here a while loop is used for the prime check; the loop control is designed on a condition check where the looping process will be happening until the input value is greater than the loop control variable. Notable, the loop control variable is initiated with 1 and incremented by 1 on each and every looping.
- Here again, for every integer value denominated in the process’s outcome equal to zero, then the integer handled is printed as ” Not a prime number”; otherwise wise it is printed as ” Prime Number.”
- This process is looped on and executed for every integer in the mentioned range of 1 to a keyed input value.

#### 4. Using Python Libraries

**Code:**

```
import sympy
start = int(input( " starting value : " ))
end = int(input( " ending value : " ))
list_prime_values = list(sympy.primerange(start,end+1))
print("All prime values in the range : " , list_prime_values)
```

**Output: **

**Explanation:**** **This program determines the range of prime numbers using predefined libraries; here, the sympy library is used for attaining the prime check; the program flow is designed to expect the starting and ending range for the determination of prime numbers.

**Program Flow:**

- The starting and ending range is keyed in by the user; the python input function is used for receiving the input from the user. The received input is handily casted into INT type.
- The keyed-in start and end range are passed as input to the prime range function of the sympy library. The output of the function is casted into a list variable, and then it printed.

### Recommended Articles

This is a guide to the Prime Numbers in Python. Here we discuss the basic concept and techniques to implement prime numbers in python. You may also look at the following articles to learn more –