# Different Methods to find Prime Number in Python

PythonProgrammingServer Side Programming

First we need to know what a prime number is.

A prime number always a positive integer number and divisible by exactly 2 integers (1 and the number itself), 1 is not a prime number.

Now we shall discuss some methods to find Prime Number.

## Method1

Using For loops

Example

def primemethod1(number):
# Initialize a list
my_primes = []
for pr in range(2, number):
isPrime = True
for i in range(2, pr):
if pr % i == 0:
isPrime = False
if isPrime:
my_primes.append(pr)
print(my_primes)
primemethod1(50)

## Output

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

## Method2

For Loops with Break

Example

def primemethod2(number):
# Initialize a list
my_primes = []
for pr in range(2, number + 1):
isPrime = True
for num in range(2, pr):
if pr % num == 0:
isPrime = False
break
if isPrime:
my_primes.append(pr)
return(my_primes)
print(primemethod2(50))

## Output

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

## Method3

For Loop, Break, and Square Root

Example

def primemethod3(number):
# Initialize a list
primes = []
for pr in range(2, number):
isPrime = True
for num in range(2, int(pr ** 0.5) + 1):
if pr % num == 0:
isPrime = False
break
if (isPrime):
print("Prime number: ",pr)
primemethod3(50)

## Output

Prime number: 2
Prime number: 3
Prime number: 5
Prime number: 7
Prime number: 11
Prime number: 13
Prime number: 17
Prime number: 19
Prime number: 23
Prime number: 29
Prime number: 31
Prime number: 37
Prime number: 41
Prime number: 43
Prime number: 47