Easy Tutorial
❮ Python Att Time Tzset Html Python Armstrong Number ❯

Python Output Prime Numbers in a Specified Range

Python3 Examples

A prime number (prime) is also known as a prime, and there are infinitely many of them. It is not divisible by any other divisor except 1 and itself.

The following example can output prime numbers within a specified range:

Example (Python 3.0+)

#!/usr/bin/python3

# Output prime numbers in a specified range

# take input from the user
lower = int(input("Enter the lower interval: "))
upper = int(input("Enter the upper interval: "))

for num in range(lower, upper + 1):
    # Prime numbers are greater than 1
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                break
        else:
            print(num)

Executing the above program, the output is:

$ python3 test.py 
Enter the lower interval: 1
Enter the upper interval: 100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Python3 Examples

❮ Python Att Time Tzset Html Python Armstrong Number ❯