Python x % 2 ==0 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16129650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:49:11  来源:igfitidea点击:

What does x % 2 ==0 mean?

python

提问by user2304000

I'm sure it's very basic and I should understand it, but I don't!

我确定这是非常基本的,我应该理解它,但我不明白!

I'm given this to do:

我的任务是:

Loop through and print out all even numbers from the numbers list in the same order they are received. Don't print any numbers that come after 237 in the sequence.

循环并以接收到的相同顺序从数字列表中打印出所有偶数。不要打印序列中 237 之后的任何数字。

This is the program i have for these numbers.

这是我对这些数字的程序。

numbers = [
    951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 
    615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 
    386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
    399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
    815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
    958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 
    743, 527
]

# your code goes here
for x in numbers:
    if x % 2 == 0:
        print x
    if x == 237:
        break

I get the right solution and everything right but I have no idea what the ==0 is there for. The only reason I used it is because it was used in the other example during the lesson before the practice!

我得到了正确的解决方案,一切都正确,但我不知道 ==0 是做什么用的。我使用它的唯一原因是因为它在练习前的课程中用于另一个示例!

回答by jamylak

x % 2gives the remainder after the integer division (when dealing with only integers such as in this case, otherwise a common type) of x/2. The %is called the modulo operator. Of course when the remainder is 0, the number is even.

x % 2给出x/2. 在%被称为模运算符。当然,当余数为0时,这个数字是偶数。

Docs:

文档

The %(modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionErrorexception. The arguments may be floating point numbers, e.g., 3.14%0.7equals 0.34(since 3.14equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2].

%(模)运算符产生了由第二的第一个参数的除法的余数。数字参数首先转换为通用类型。零右参数引发ZeroDivisionError异常。参数可以是浮点数,例如,3.14%0.7等于0.34(因为3.14等于4*0.7 + 0.34)。模运算符总是产生一个与它的第二个操作数(或零)具有相同符号的结果;结果的绝对值严格小于第二个操作数的绝对值 [2]。

回答by Petar Minchev

if x % 2 == 0checks if a number is even.

if x % 2 == 0检查数字是否为偶数。

x % 2is 1when the number is odd, and 0when it is even.

x % 21奇数0时,偶数时。

回答by sepp2k

== 0means "equal to 0 (zero)". So if foo == 0:means "do the following if foois equal to 0", thus if x % 2 == 0:means "do the following if x % 2is equal to 0".

== 0表示“等于 0(零)”。Soif foo == 0:表示“如果foo等于 0,if x % 2 == 0:则执行以下操作”,因此表示“如果等于 0,则执行以下操作x % 2”。