In this program, you’ll learn to print all prime numbers within an interval using for loops and display it.
To understand this example, you should have the knowledge of the following Python programming topics:
A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number.
2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime (it is composite) since, 2 x 3 = 6.
Source Code
# Python program to display all the prime numbers within an interval
lower = 900
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Output
Prime numbers between 900 and 1000 are: 907 911 919 929 937 941 947 953 967 971 977 983 991 997
Here, we store the interval as lower for lower interval and upper for upper interval, and find prime numbers in that range. Visit this page to learn how to check whether a number is prime or not.
Related posts:
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String rstrip()
Python String isalnum()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Set intersection_update()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Copy a File
Python strptime()
Python Directory and Files Management
Python Set union()
Python Dictionary copy()
Python map()
Python Program to Convert Kilometers to Miles
Python Program to Represent enum
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python locals()
Python Shallow Copy and Deep Copy
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Check Armstrong Number
Python Namespace and Scope
Python Sets
Python Program to Find Hash of File
Python del Statement
Python Set symmetric_difference()
Python Global, Local and Nonlocal variables
Python int()
Python Package
Python String maketrans()
Python Program to Safely Create a Nested Directory
Python String islower()