Print All Prime Numbers in a Range Using Python

A simple Python program to print prime numbers in a range

Prime numbers are a set of positive numbers that is greater than one and are divisible only by 1 and the number itself. 

For example, The first 10 prime numbers are 2,3,5,7,11,13,17,19,23, and 29. Each of these numbers is divisible only by 1 or the number itself.

Real-life applications of the prime numbers are encryption of various communication (VPNs and HTTPS) and securing confidential information such as digital signatures, credit cards, and online transactions.

As there are infinite numbers of Prime numbers, we cannot determine all the prime numbers. Therefore, in today’s lesson, we are discussing about a Python program to print Prime Numbers in a range

Python Program to Print All Prime Numbers in a Range

# Python program to print all prime numbers in the given range

num_from = 20
num_to = 50

prime_nums = [];

for num in range(num_from, num_to + 1):
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           prime_nums.append(num)

print("List of prime numbers within range", num_from, "to", num_to, "are:", prime_nums)
  • Establishing the Range
    • It is impossible to determine all the prime numbers as there are an infinite number of them. However, we can find all within a certain range
    • Hence, it is important to establish a range
    • In our code we are going to define the lower and upper bounds of the range by ‘num_from’ and ‘num_to’ respectively
  • List to store the prime number
    • As the range may or may not contain more than one Prime number, we need a list to store our Prime numbers
    • We create an empty list ‘prime_nums’ to store our Prime numbers
  • ‘for’ loop to determine if the number is greater than 1
    • A ‘for’ loop is used to iterate over every number within the range. This loop checks whether the number is greater than 1
  • ‘for’ loop and ‘break’ to check whether the number is Prime
    • Another ‘for’ loop is used to check whether the number (if it is greater than 1) is a Prime number or not.
    • This inner loop iterates over every number between 2 and the number itself, and checks whether the number is divisible by any of those numbers.
    • If the number is divisible by any number, it is not a Prime number and the inner loop is exited using the ‘break’ statement
  • Adding the Prime number to the lsu
    • If the loop completes without finding the factor of the number, the number is a Prime number and is added to the ‘prime_nums’ list using the ‘append()’ method
    • Syntax: else: prime_nums.append(num)
  • Displaying the Output
    • Finally, we print the outcome with the print statement mentioned below
    • Syntax : print(“List of prime numbers within range”, num_from, “to”, num_to, are:”, prime_nums)

Output:

List of prime numbers within range 20 to 50 are: [23, 29, 31, 37, 41, 43, 47]

This program generates all the prime numbers within the numbers 20 and 50. This code defines a range of numbers using two variables ‘num_from’ and ‘num_to’. A list called ‘prime_nums’ is also defined to store all the prime numbers found in this range.

It checks if the number is greater than 1 for each number. It then loops through all numbers from 2 to the number itself minus one using another for loop. Then it checks if the number is divisible by any of these numbers. If yes, then the program breaks the loop as the number is not prime. If the number is not divisible by any of these numbers, then it is a prime number and it is added to the list ‘prime_nums’.

Finally, the program prints a message showing the range of numbers checked and the list of prime numbers found within that range.

Output: “List of prime numbers within range 20 to 50 are: [23, 29, 31, 37, 41, 43, 47]”

Why do we use nested for loop to print all prime number

Some other methods to find all prime numbers in the given range include using the Sieve of Eratosthenes algorithm or using the Miller-Rabin primality test.

But, the above method using a nested for loop and appending the prime numbers to a list is a simple and straightforward method that can be easily understood and implemented by beginners. It also has relatively good performance for small ranges.

For larger ranges or more complex prime number problems, it may be beneficial to use more advanced algorithms.

In this tutorial, we learned about a Python program to print prime numbers in a range. We also discussed their real-life application and condition such as Prime numbers can only be positive integers greater than number 1. We clarified each step of the code in detail, making it simple for beginners to understand the logic behind the code.

FAQs on Prime Numbers in a range

What is the range of prime numbers?

Prime numbers can be found in any range of positive integers, but they become increasingly sparse as the numbers get larger.

What will happen if I insert a negative range?

The program will result in an error message since prime numbers are only defined as positive integers.

What is the Sieve of Eratosthenes?

The Sieve of Eratosthenes is an algorithm for finding all prime numbers up to a given limit or range. It works by iteratively marking the multiples of each prime, starting with 2, and then continuing to the next unmarked number until all numbers in the range have been processed. The unmarked numbers that remain are all prime numbers.

About The Author

Leave a Reply