How to Find Prime Number Between Range in Python?

In this Python example, we will discuss how can we find all the prime numbers which lie within a given range or in a given internal.

1. How can we Find Prime Numbers in a Range?

prime number is a number greater than 1 with only two factors – themselves and 1.

According to Wikipedia

We can find prime numbers in a range by providing the starting point and the ending point. All these numbers are divisible by 1 and itself only.

Start : 1
Stop : 7
Output: 2, 3, 5

Start : 3
Stop : 50
Output: 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47

Some of the topics which will be helpful for understanding the program implementation better are:

2. Python Program to to find Prime Number in a Range

The program takes input from the user as starting value and ending value and one by one the prime numbers are printed.

Let’s implement the code and see how it works.

#Python program to find prime numbers within a range

start = int(input("Enter the lower bound: "))
stop = int(input("Enter the upper bound: "))

print("Prime numbers between", start, "and", stop, "are:")

for val in range(start, stop):
  if val > 1:
    for i in range(2, val):
      if (val % i) == 0:
      print(val, end=" ")
Enter the lower bound: 3
Enter the upper bound: 71
Prime numbers between 3 and 71 are:
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 

3. Conclusion

In this article, we have learned to find the prime number in a given range using the for loop and if-else condition.

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x