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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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:
Java Program to Implement the Program Used in grep/egrep/fgrep
Python List append()
Python String format()
Python Program to Print Colored Text to the Terminal
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
How to Get Started With Python?
Python Program to Differentiate Between type() and isinstance()
Python String isdecimal()
Python Program to Remove Duplicate Element From a List
Python Dictionary update()
Python Program to Count the Number of Digits Present In a Number
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python *args and **kwargs
Python property()
Python sum()
Python strptime()
Python hash()
Python String isnumeric()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Get a Substring of a String
Python Directory and Files Management
Python Program to Compute all the Permutation of the String
Python Multiple Inheritance
Python print()
Python String isspace()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python dir()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Merge Two Dictionaries
Python Program to Check Whether a String is Palindrome or Not
Python Custom Exceptions