Skip to content
Home » Python Programs » Python Program to Find Prime Numbers Between a Given Range

Python Program to Find Prime Numbers Between a Given Range

Simple python program to find prime numbers between a given range. In this program, we can find the prime number between a given range for beginners to understand the concepts in python.

Return Function in Python

The function call is ended by a return statement, which “returns” to the caller the result of the expression after the return keyword. Following the return statements, the statements are skipped. If the return statement contains no expression, the special value None is returned.

If Else Statement in Python

An if-else clause The true or false value of an expression is determined by a Python statement. The “if” statement is executed if a condition is true. If not, the “else” statement is executed. If statements in Python enable programmers to manage the flow of their programming. 

You can create a chain of if statements using the if/else if statement. The if statements are evaluated one after the other until one of them if expressions are true or the if/else if chain ends. If there is no true expression at the end of the if/else if chain, no code blocks are executed.

For Loop Definition in Python

When you have a block of code that you want to repeat a certain number of times, you use a for a loop. The for the statement in Python iterates through the members of a sequence, executing the block each time. By using for loop we have executed the python program to find prime numbers between a given range

Python Program to Find Prime Numbers Between a Given Range

def prime(x, y):
	prime_no = []
	for i in range(x, y):
		if i == 0 or i == 1:
			continue
		else:
			for j in range(2, int(i/2)+1):
				if i % j == 0:
					break
			else:
				prime_no.append(i)
	return prime_no
initial = 10
final = 50
lst = prime(initial, final)
if len(lst) == 0:
	print("No Prime Number")
else:
	print("Prime Numbers Are ", lst)

Output:

Prime Numbers Are  [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]