Python Prime Number Check
A natural number greater than 1 that cannot be divided by any other natural number (2, 3, 5, 7, etc.) except 1 and itself; in other words, the number has no factors other than 1 and itself.
The test.py file:
-- coding: UTF-8 --
Filename : test.py
author by : www.runoob.com
Python program to check whether a number entered by the user is prime
User enters numbers
num
=
int
(
input
(
"
Please enter a number:
"
)
)
A prime number is greater than 1
if
num
1
:
Check the factors
for
i
in
range
(
2
,
num
)
:
if
(
num
%
i
)
==
0
:
(
num
,
"
Not a prime number
"
)
(
i
,
"
Multiplied by
"
,
num
//
i
,
"
Is
"
,
num
)
break
else
:
(
num
,
"
is a prime number
"
)
If the number entered is less than or equal to 1, it is not prime
else
:
(
num
,
"
Not a prime number
"
)
The output of the code above is:
$ python3 test.py
请输入一个数字: 1
1 不是质数
$ python3 test.py
请输入一个数字: 4
4 不是质数
2 乘于 2 是 4
$ python3 test.py
请输入一个数字: 5
5 是质数
Python3 Examples