English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python program to print all prime numbers in a range

Python complete examples

In this program, you will learn to use the for loop to print all prime numbers in a range and display them.

To understand this example, you should understand the followingPython programmingTopic:

a number greater than1positive integers, except1has no other factors, and this number itself is called a prime number.

2,3,5,7are prime numbers because they have no other factors. But6Is not a prime number (it is a composite number) because2×3 = 6.

Source code

#Python program to display all prime numbers in a range
lower = 900
upper = 1000
print(lower, "and", upper, "Prime numbers between:")
for num in range(lower, upper + 1):
   #All prime numbers are greater than1
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           print(num)

Output results

900 and 1000 Prime numbers between:
907
911
919
929
937
941
947
953
967
971
977
983
991
997

Here, in lower and upper+1Find prime numbers in the range. Visit this page to learn howCheck if a number is a prime number.

Python complete examples