Prime numbers are numbers that can only be divisible by themselves or 1. Examples of prime numbers include 2, 3, 5, 7, 11, and so on. In this tutorial, you will learn how to check if a number is a prime number. We will discuss how to write a prime number program in python – this program prints numbers that are prime numbers within a given range.
Let’s get on.
Table of Contents
Python Program For Prime Numbers
Before getting started, it is important to understand the nature of prime numbers. This will guide us when writing the code.
First things first. Prime numbers are natural numbers. In other words, we do not consider negative numbers to be prime numbers. This, therefore, means that when writing your python program, the number must be checked at the start of the program to ensure that the number is a positive integer.
To find a prime number in python, we first check if the number is greater than 1. We then use a for loop to iterate from 2 to the number. Inside the for loop, we divide the input number by all numbers less than the number. If we find any divisor, then we can display it by printing “the number is not a prime number”. If however, we don’t find any divisor, we print “the number is a prime number”.
Before we code, you will need to have a knowledge of the python for loop statement, python if-else statement, and the python break statement. If these concepts are above your head, refer to this tutorial that explains Python Break Statement with a conditional statement.
The code is written below.
#A program to check if a number is a prime number or not
#recieve an input from the user
number = int(input('Enter a positive number: '))
#check if its a prime number
if number>1:
for numbers in range(2, number):
if (number % numbers) ==0:
print(f"{number} is not a prime number")
break
else:
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
Output:
Enter a positive number: 31
31 is a prime number
Let’s input a number that is not a prime, say 32.
Enter a positive number: 32
32 is not a prime number
Finding Prime Numbers in Python
We can go a step further by printing the list of prime numbers within a particular range. The code is written thus.
#A python program to print all the prime numbers within an interval of numbers
#create an interval for the range of numbers
lower_number = int(input('Enter the lower limit: '))
upper_number = int(input('Enter the upper limit: '))
prime_numbers = []
#Then, we use for loop to produce the prime numbers
for number in range(lower_number, upper_number +1):
if number >1:
for numbers in range(2, number):
if (number %numbers) ==0:
break
else:
prime_numbers.append(number)
print(f"The prime numbers between {lower_number} and {upper_number} are {prime_numbers}")
Output:
Enter the lower limit: 4
Enter the upper limit: 30
The prime numbers between 4 and 30 are [5, 7, 11, 13, 17, 19, 23, 29]
You can change the upper and lower limit as you wish. The code would print the number within that range.
To conclude,
Now you know how to write prime number program in python. You learned how to write python code for checking prime numbers and how to find prime numbers amongst a range of numbers. If you have any questions or you’re running into an error, tell me about it in the comment section. I’d be sure to reply you.