Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 36

Python Exercise Example 36

Python 100 Examples Python 100 Examples

**Problem:** Find the prime numbers within 100.

**Analysis:** None.

Program source code:

Example

#!/usr/bin/python

-- coding: UTF-8 --

Output the prime numbers in a specified range

User input data

lower

=

int

(

input

(

"

Enter the minimum value of the range:

"

)

)

upper

=

int

(

input

(

"

Enter the maximum value of the range:

"

)

)

for

num

in

range

(

lower

,

upper

1

)

:

A prime number is greater than 1

if

num

1

:

for

i

in

range

(

2

,

num

)

:

if

(

num

%

i

)

==

0

:

break

else

:

print

(

num

)

The output of the above example is:


输入区间最小值: 1
输入区间最大值: 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

Python 100 Examples Python 100 Examples