# Prime number program in python

A prime number is a number that is only divisible by 1 and itself. By using python programming we can find whether any given number is a prime number or not.

We can also find all numbers within the range of 2 and N(given number). Let us see how we can write these two programs using python programming.

#### Finding a number a prime number or not?

``````# primeornot.py
# Below function returns true if number is prime number.
def isPrime(num):
if num < 2:
return False
for val in range(2, num):
# check if given number divisible any number other than 1 and itself.
if num % val == 0:
return False

return True

# 5 is prime number, returns True
print(isPrime(5))
# 8 is non prime, returns False
print(isPrime(8))
# 13 is prime number, return True
print(isPrime(13))``````

The above program checks given numbers 5, 8, and 13 are prime numbers or not and returns True or False respectively.

``````# Output
\$python primeornot.py
True
False
True``````

In a similar way by using the above-defined “isPrime()” function we can define a series of prime numbers less than the given input number.

#### Python program to print prime number series below given input number

We get the series using the above “isPrime” function, loop through all numbers and check if that number is prime or not, and print if it is a prime number. The code looks as follows.

``````# primeseries.py
# Below function returns true if number is prime number.
def isPrime(num):
if num < 2:
return False
for val in range(2, num):
# check if given number divisible any number other than 1 and itself.
if num % val == 0:
return False

return True

# below function prints all prime numbers which are less than given number.
def primeSeries(num):
for val in range(2, num):
# check if current number is prime or not.
if isPrime(val):
print(val)

# prints all prime numbers less than 20.
primeSeries(20)``````

The above python program prints all prime numbers series less than the given input number.

``````# Output
\$python primeseries.py
2
3
5
7
11
13
17
19``````

Hope these above two python prime number programs help you to understand the primary number generation using python. Keep learning!!