Page Contents

In this tutorial we will write a Python program to print all prime numbers in a range . Before reading further we recommend to read about below tutorial-

## What is Prime number

A positive integer number which is greater than 1 and only divisible by 1 and that number is called **Prime number** .

#### Example :

- 2 is a prime number as 2 is only divisible by 1 and 3 .
- 3 is a prime number as 3 is only divisible by 1 and 3.
- 4 is not a prime number because it is not only divisible by 1 and 4 , it is also divisible by 2 .

## Python program to print all prime numbers in a range

#### Steps:

- First we will define a
`is_prime()`

function which will take a number as function argument and return`True`

if the given number is a prime number else return`False`

. To know more about`is_prime()`

function click here . - After that we will define a function named
`prime_numbers()`

which will take two numbers as range(start-end) . This function will print all the prime numbers in the given range . - If the value of end parameter is less than 2 then we simply return
`None`

. Because value of lowest prime number is 2 - If the value of start parameter is less than 2 and value of end parameter is greater than 1 so we simply change the value of start parameter as 2 . Because no of prime numbers in range -10 to 10 is equal to no of prime numbers in range 2 to 10 .
- After that we will iterate every number from start to end and print the number if the number is a prime number .

```
import math
# defining a function to check
# if a given number is prime or not
def is_prime(number):
# declaring a flag variable
prime=True
# trying to divide the number by the numbers
# starting from 2
for i in range(2,int(math.sqrt(number)+1)):
if number%i==0:
# if the number is divisable by any number
# staring from 2 then change the flag variable
# and exit immediately from the loop
prime=False
break
return prime
# define a function to print
# all prime numbers in a range
def prime_numbers(start,end):
# if end is less than 2 we simply return none
# and exit from the function
if end<2:
return None
else:
# if start is less than 2 and end is greater than 1
# so change the value of start as 2
if (start<2 and end>1):
start=2
# traversing every number from start to end
# and print the number if the number is prime number
for i in range(start,end+1):
if is_prime(i)==True:
print(i,end=" ")
print()
# calling the function
prime_numbers(-2,0)
prime_numbers(-2,5)
prime_numbers(4,10)
```

2 3 5 5 7

- In the above code we have written a function named
`prime_numbers()`

which will print all the prime numbers in a range . - As there is no prime number in between -2 and 0 so
`prime_numbers(-2,0)`

did not print anything . `prime_numbers(-2,5)`

line is printed`2 3 5`

in the output as in between -2 to 5 there are only three prime numbers 2 , 3 ,5 .`prime_numbers(4,10)`

line is printed`5 7`

in the output as in between 4 to 10 there are only two prime numbers 5 , 7 .

**Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment **.