百分号在Python中是什么意思 [英] What does the percentage sign mean in Python
问题描述
在本教程中,有一个查找素数的示例:
In the tutorial there is an example for finding prime numbers:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else:
... # loop fell through without finding a factor
... print(n, 'is a prime number')
...
我知道双精度==
是对相等性的检验,但我不理解if n % x
部分.就像我可以口头遍历每个部分,并说出该语句对示例的作用.但是我不明白百分号是如何下降的.
I understand that the double ==
is a test for equality, but I don't understand the if n % x
part. Like I can verbally walk through each part and say what the statement does for the example. But I don't understand how the percentage sign falls in.
if n % x
实际怎么说?
推荐答案
模运算符;给出剩余的左值除以右值.喜欢:
Modulus operator; gives the remainder of the left value divided by the right value. Like:
3 % 1
等于零(因为3被1除以均分)
3 % 1
would equal zero (since 3 divides evenly by 1)
3 % 2
等于1(因为3除以2会得到1的余数).
3 % 2
would equal 1 (since dividing 3 by 2 results in a remainder of 1).
这篇关于百分号在Python中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!