标签归档:modulo

Python中%的结果是什么?

问题:Python中%的结果是什么?

什么是%在计算?我似乎无法弄清楚它的作用。

例如,它算出计算的百分比4 % 2吗:显然等于0。如何?

What does the % in a calculation? I can’t seem to work out what it does.

Does it work out a percent of the calculation for example: 4 % 2 is apparently equal to 0. How?


回答 0

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

取自http://docs.python.org/reference/expressions.html

例1: 6%2计算结果为0,如果将6除以2(3次),则没有余数。

实施例27%2评估为1因为有一个的其余部分1时7由2(3次)划分。

综上所述,它返回除法运算的余数,或者0没有余数。因此,6%2意味着找到6的余数除以2。

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 ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 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].

Taken from http://docs.python.org/reference/expressions.html

Example 1: 6%2 evaluates to 0 because there’s no remainder if 6 is divided by 2 ( 3 times ).

Example 2: 7%2 evaluates to 1 because there’s a remainder of 1 when 7 is divided by 2 ( 3 times ).

So to summarise that, it returns the remainder of a division operation, or 0 if there is no remainder. So 6%2 means find the remainder of 6 divided by 2.


回答 1

在某种程度上,主题%还用于字符串格式化操作,例如%=将值替换为字符串:

>>> x = 'abc_%(key)s_'
>>> x %= {'key':'value'}
>>> x 
'abc_value_'

同样,偏离主题,但它似乎是一个小记录功能,它花了一段时间来跟踪,并且我认为这是有关Python的模数计算的此SO页面高度行列。

Somewhat off topic, the % is also used in string formatting operations like %= to substitute values into a string:

>>> x = 'abc_%(key)s_'
>>> x %= {'key':'value'}
>>> x 
'abc_value_'

Again, off topic, but it seems to be a little documented feature which took me awhile to track down, and I thought it was related to Pythons modulo calculation for which this SO page ranks highly.


回答 2

像这样的表达式x % y求余数x ÷ y-从技术上讲,它是“模数”而不是“提醒”,因此,如果与其他语言%(余数运算符)进行比较,结果可能会有所不同。有一些细微的差异(如果您对实际的结果感兴趣,另请参见“为什么要使用Python的整数分区”)。

优先级与运算符/(除法)和*(乘法)相同。

>>> 9 / 2
4
>>> 9 % 2
1
  • 9除以2等于4。
  • 4乘2是8
  • 9减去8为1-余数。

Python陷阱:取决于您使用的Python版本,%它也是(不建议使用的)字符串插值运算符,因此请注意您是否来自使用自动类型转换的语言(如PHP或JS),在其中使用'12' % 2 + 3合法的表达式: Python,TypeError: not all arguments converted during string formatting这可能会使您感到困惑。

[Python 3更新]

用户n00p评论:

9/2在Python中为4.5。您必须像这样进行整数除法:9 // 2如果要python告诉您除法(4)之后还剩下多少个整个对象。

确切地说,整数除法曾经是Python 2中的默认设置(请注意,这个答案比我的男孩年龄大,这个男孩已经在学校学习,当时2.x是主流):

$ python2.7
Python 2.7.10 (default, Oct  6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 / 2
4
>>> 9 // 2
4
>>> 9 % 2
1

在现代Python中,9 / 2结果4.5确实是:

$ python3.6
Python 3.6.1 (default, Apr 27 2017, 00:15:59)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 / 2
4.5
>>> 9 // 2
4
>>> 9 % 2
1

[更新]

用户dahiya_boy在评论会话中询问:

问:能否请您解释原因-11 % 5 = 4-dahiya_boy

这很奇怪吧?如果您使用JavaScript尝试此操作:

> -11 % 5
-1

这是因为在JavaScript %中,它是“余数”运算符,而在Python中,它是“模数”(时钟数学)运算符。

您可以直接从GvR获得解释


编辑-dahiya_boy

在Java和iOS中,-11 % 5 = -1而在python和ruby中-11 % 5 = 4

Paulo Scardine解释了一半的原因,下面是其余的解释

在Java和iOS,%使剩余这意味着,如果你把11%的5 给出了Quotient = 2 and remainder = 1-11%5给出Quotient = -2 and remainder = -1

快速iOS中的示例代码。

但是当我们在python中谈论它时,它给出了时钟模数。及其使用以下公式的工作

mod(a,n) = a - {n * Floor(a/n)}

那就意味着

mod(11,5) = 11 - {5 * Floor(11/5)} => 11 - {5 * 2}

所以, mod(11,5) = 1

mod(-11,5) = -11 - 5 * Floor(11/5) => -11 - {5 * (-3)}

所以, mod(-11,5) = 4

python 3.0中的示例代码。


为什么Python的整数分区

今天(再次)被要求解释为什么Python中的整数除法返回结果的底数,而不是像C一样向零截断。

对于正数,不足为奇:

>>> 5//2
2

但是,如果其中一个操作数是负数,则结果是下限的,即从零舍入(朝负无穷大):

>>> -5//2
-3
>>> 5//-2
-3

这打扰了一些人,但是有一个很好的数学理由。整数除法运算(//)及其兄弟运算,取模运算(%)在一起并满足良好的数学关系(所有变量均为整数):

a/b = q with remainder r

这样

b*q + r = a and 0 <= r < b

(假设a和b> = 0)。

如果要使关系扩展为负a(使b为正),则有两种选择:如果将q截断为零,则r将变为负,因此不变量变为0 <= abs(r)<否则,您可以将q朝负无穷大移动,并且不变保持0 <= r <b。[更新:解决了此问题]

在数学数论中,数学家总是喜欢后一种选择(例如参见Wikipedia)。对于Python,我做出了相同的选择,因为模运算有一些有趣的应用,其中a的符号没有意义。考虑采用POSIX时间戳(自1970年初以来的秒数),并将其转换为一天中的时间。由于一天中有24 * 3600 = 86400秒,因此该计算仅为t%86400。但是,如果我们使用负数来表示1970年之前的时间,则“截断为零”规则将得出毫无意义的结果!使用下限规则,一切正常。

我想到的其他应用程序是计算机图形学中像素位置的计算。我敢肯定还有更多。

顺便说一下,对于负数b,所有事物都翻转了,并且不变式变为:

0 >= r > b.

那么C为什么不这样做呢?在设计C时,硬件可能没有这样做。而且硬件可能不会这样做,因为在最旧的硬件中,负数表示为“符号+幅度”,而不是如今使用的二进制补码表示法(至少对于整数)。我的第一台计算机是Control Data大型机,它使用整数和浮点数的补码。60的模式表示负零!

蒂姆·彼得斯(Tim Peters)知道所有Python的浮点骨架都埋在哪里,对我希望将这些规则扩展到浮点模数表示担忧。他可能是对的。当x是非常小的负数时,truncate-towards-negative-infinity规则会导致x%1.0的精度损失。但这还不足以打破整数模,并且//与之紧密耦合。

PS。请注意,我使用的是//而不是/-这是Python 3语法,并且在Python 2中也允许强调您知道自己正在调用整数除法。Python 2中的/运算符是模棱两可的,因为它对两个整数操作数返回的结果与对int和一个或两个或多个浮点数的返回不同。但这是一个完全独立的故事。参见PEP 238。

由Guido van Rossum发表于9:49 AM

An expression like x % y evaluates to the remainder of x ÷ y – well, technically it is “modulus” instead of “reminder” so results may be different if you are comparing with other languages where % is the remainder operator. There are some subtle differences (if you are interested in the practical consequences see also “Why Python’s Integer Division Floors” bellow).

Precedence is the same as operators / (division) and * (multiplication).

>>> 9 / 2
4
>>> 9 % 2
1
  • 9 divided by 2 is equal to 4.
  • 4 times 2 is 8
  • 9 minus 8 is 1 – the remainder.

Python gotcha: depending on the Python version you are using, % is also the (deprecated) string interpolation operator, so watch out if you are coming from a language with automatic type casting (like PHP or JS) where an expression like '12' % 2 + 3 is legal: in Python it will result in TypeError: not all arguments converted during string formatting which probably will be pretty confusing for you.

[update for Python 3]

User n00p comments:

9/2 is 4.5 in python. You have to do integer division like so: 9//2 if you want python to tell you how many whole objects is left after division(4).

To be precise, integer division used to be the default in Python 2 (mind you, this answer is older than my boy who is already in school and at the time 2.x were mainstream):

$ python2.7
Python 2.7.10 (default, Oct  6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 / 2
4
>>> 9 // 2
4
>>> 9 % 2
1

In modern Python 9 / 2 results 4.5 indeed:

$ python3.6
Python 3.6.1 (default, Apr 27 2017, 00:15:59)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 / 2
4.5
>>> 9 // 2
4
>>> 9 % 2
1

[update]

User dahiya_boy asked in the comment session:

Q. Can you please explain why -11 % 5 = 4 – dahiya_boy

This is weird, right? If you try this in JavaScript:

> -11 % 5
-1

This is because in JavaScript % is the “remainder” operator while in Python it is the “modulus” (clock math) operator.

You can get the explanation directly from GvR:


Edit – dahiya_boy

In Java and iOS -11 % 5 = -1 whereas in python and ruby -11 % 5 = 4.

Well half of the reason is explained by the Paulo Scardine, and rest of the explanation is below here

In Java and iOS, % gives the remainder that means if you divide 11 % 5 gives Quotient = 2 and remainder = 1 and -11 % 5 gives Quotient = -2 and remainder = -1.

Sample code in swift iOS.

But when we talk about in python its gives clock modulus. And its work with below formula

mod(a,n) = a - {n * Floor(a/n)}

Thats means,

mod(11,5) = 11 - {5 * Floor(11/5)} => 11 - {5 * 2}

So, mod(11,5) = 1

And

mod(-11,5) = -11 - 5 * Floor(-11/5) => -11 - {5 * (-3)}

So, mod(-11,5) = 4

Sample code in python 3.0.


Why Python’s Integer Division Floors

I was asked (again) today to explain why integer division in Python returns the floor of the result instead of truncating towards zero like C.

For positive numbers, there’s no surprise:

>>> 5//2
2

But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):

>>> -5//2
-3
>>> 5//-2
-3

This disturbs some people, but there is a good mathematical reason. The integer division operation (//) and its sibling, the modulo operation (%), go together and satisfy a nice mathematical relationship (all variables are integers):

a/b = q with remainder r

such that

b*q + r = a and 0 <= r < b

(assuming a and b are >= 0).

If you want the relationship to extend for negative a (keeping b positive), you have two choices: if you truncate q towards zero, r will become negative, so that the invariant changes to 0 <= abs(r) < otherwise, you can floor q towards negative infinity, and the invariant remains 0 <= r < b. [update: fixed this para]

In mathematical number theory, mathematicians always prefer the latter choice (see e.g. Wikipedia). For Python, I made the same choice because there are some interesting applications of the modulo operation where the sign of a is uninteresting. Consider taking a POSIX timestamp (seconds since the start of 1970) and turning it into the time of day. Since there are 24*3600 = 86400 seconds in a day, this calculation is simply t % 86400. But if we were to express times before 1970 using negative numbers, the “truncate towards zero” rule would give a meaningless result! Using the floor rule it all works out fine.

Other applications I’ve thought of are computations of pixel positions in computer graphics. I’m sure there are more.

For negative b, by the way, everything just flips, and the invariant becomes:

0 >= r > b.

So why doesn’t C do it this way? Probably the hardware didn’t do this at the time C was designed. And the hardware probably didn’t do it this way because in the oldest hardware, negative numbers were represented as “sign + magnitude” rather than the two’s complement representation used these days (at least for integers). My first computer was a Control Data mainframe and it used one’s complement for integers as well as floats. A pattern of 60 ones meant negative zero!

Tim Peters, who knows where all Python’s floating point skeletons are buried, has expressed some worry about my desire to extend these rules to floating point modulo. He’s probably right; the truncate-towards-negative-infinity rule can cause precision loss for x%1.0 when x is a very small negative number. But that’s not enough for me to break integer modulo, and // is tightly coupled to that.

PS. Note that I am using // instead of / — this is Python 3 syntax, and also allowed in Python 2 to emphasize that you know you are invoking integer division. The / operator in Python 2 is ambiguous, since it returns a different result for two integer operands than for an int and a float or two floats. But that’s a totally separate story; see PEP 238.

Posted by Guido van Rossum at 9:49 AM


回答 3

模数是一种数学运算,有时也称为“时钟算术”。我发现将其简单描述为剩余部分会产生误导和混乱,因为它掩盖了它在计算机科学中被大量使用的真正原因。它实际上是用于环绕循环的。

想想时钟:假设您以“军事”时间查看时钟,该时间范围为0:00-23.59。现在,如果您希望每天午夜发生一些事情,那么您希望当前时间mod 24为零:

如果(小时%24 == 0):

您可以想到历史中的所有小时都围绕24小时一圈地循环,并且一天中的当前小时是无限长的mod24。这是一个比余数更深刻的概念,这是一种数学方法处理周期,这在计算机科学中非常重要。它也用于环绕数组,允许您增加索引并使用模数在到达数组末尾后返回到开头。

The modulus is a mathematical operation, sometimes described as “clock arithmetic.” I find that describing it as simply a remainder is misleading and confusing because it masks the real reason it is used so much in computer science. It really is used to wrap around cycles.

Think of a clock: Suppose you look at a clock in “military” time, where the range of times goes from 0:00 – 23.59. Now if you wanted something to happen every day at midnight, you would want the current time mod 24 to be zero:

if (hour % 24 == 0):

You can think of all hours in history wrapping around a circle of 24 hours over and over and the current hour of the day is that infinitely long number mod 24. It is a much more profound concept than just a remainder, it is a mathematical way to deal with cycles and it is very important in computer science. It is also used to wrap around arrays, allowing you to increase the index and use the modulus to wrap back to the beginning after you reach the end of the array.


回答 4

Python-基本运算符
http://www.tutorialspoint.com/python/python_basic_operators.htm

模量-将左操作数除以右操作数并返回余数

a = 10和b = 20

b%a = 0

Python – Basic Operators
http://www.tutorialspoint.com/python/python_basic_operators.htm

Modulus – Divides left hand operand by right hand operand and returns remainder

a = 10 and b = 20

b % a = 0


回答 5

在大多数语言中,%用于模数。Python也不exceptions。

In most languages % is used for modulus. Python is no exception.


回答 6

%Modulo运算符也可以用于打印字符串(就像在C中一样),如Google https://developers.google.com/edu/python/strings上定义的那样。

      # % operator
  text = "%d little pigs come out or I'll %s and %s and %s" % (3, 'huff', 'puff', 'blow down')

这似乎有点题外话,但肯定会有所帮助。

% Modulo operator can be also used for printing strings (Just like in C) as defined on Google https://developers.google.com/edu/python/strings.

      # % operator
  text = "%d little pigs come out or I'll %s and %s and %s" % (3, 'huff', 'puff', 'blow down')

This seems to bit off topic but It will certainly help someone.


回答 7

x % y计算除法的余数x除以y其中的商是一个整数。其余的符号为y


在Python 3上计算得出6.75; 这是因为这样/做是真正的除法,而不是Python 2上的整数除法(默认情况下)(默认情况下)。在Python 2上1 / 4,由于结果舍入为0。

整数除法也可以在Python 3上使用//运算符完成,因此要得到7,可以执行:

3 + 2 + 1 - 5 + 4 % 2 - 1 // 4 + 6

此外,只需添加以下行,即可在Python 2上获得Python样式划分

from __future__ import division

作为每个源文件中的第一条源代码行。

x % y calculates the remainder of the division x divided by y where the quotient is an integer. The remainder has the sign of y.


On Python 3 the calculation yields 6.75; this is because the / does a true division, not integer division like (by default) on Python 2. On Python 2 1 / 4 gives 0, as the result is rounded down.

The integer division can be done on Python 3 too, with // operator, thus to get the 7 as a result, you can execute:

3 + 2 + 1 - 5 + 4 % 2 - 1 // 4 + 6

Also, you can get the Python style division on Python 2, by just adding the line

from __future__ import division

as the first source code line in each source file.


回答 8

模运算符,通常用于整数的余数除法,但在Python中可用于浮点数。

http://docs.python.org/reference/expressions.html

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

Modulus operator, it is used for remainder division on integers, typically, but in Python can be used for floating point numbers.

http://docs.python.org/reference/expressions.html

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 ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 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].


回答 9

这是一个模运算,除了它是老式的C风格的字符串格式化运算符,而不是模运算。有关详细信息,请参见此处。您将在现有代码中看到很多这样的内容。

It’s a modulo operation, except when it’s an old-fashioned C-style string formatting operator, not a modulo operation. See here for details. You’ll see a lot of this in existing code.


回答 10

此外,还有一个有用的内置函数,称为divmod

divmod(a,b)

使用两个(非复数)数字作为参数,并在使用长除法时返回一对包含其商和余数的数字。

Also, there is a useful built-in function called divmod:

divmod(a, b)

Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using long division.


回答 11

意识到

(3 +2 + 1 - 5) + (4 % 2) - (1/4) + 6

如果使用Python 3.4计算,即使带括号的结果也是6.75,而不是7。


而且’/’运算符也不是那么容易理解(python2.7):试试…

- 1/4

1 - 1/4

这里有点题外话,但是在评估上面的表达式时应该考虑:)

Be aware that

(3 +2 + 1 - 5) + (4 % 2) - (1/4) + 6

even with the brackets results in 6.75 instead of 7 if calculated in Python 3.4.


And the ‘/’ operator is not that easy to understand, too (python2.7): try…

- 1/4

1 - 1/4

This is a bit off-topic here, but should be considered when evaluating the above expression :)


回答 12

对于我来说,很难在线找到使用%的特定用例,例如,为什么做分数模除法或负模除法会得出答案呢?希望这有助于澄清如下问题:

模量部门:

模除法返回数学除法运算的余数。这样做如下:

假设我们的股息为5,除数为2,则以下除法运算将等于(等于x):

dividend = 5
divisor = 2

x = 5/2 
  1. 模量计算的第一步是进行整数除法:

    x_int = 5 // 2(Python中的整数除法使用双斜杠)

    x_int = 2

  2. 接下来,x_int的输出乘以除数:

    x_mult = x_int *除数x_mult = 4

  3. 最后,从x_mult中减去股息

    红利-x_mult = 1

  4. 模运算,因此返回1:

    5%2 = 1

申请将模数应用于分数

Example: 2 % 5 

当应用于分数时,模量的计算与上面相同;但是,重要的是要注意,当除数大于被除数时,整数除法将得出零值:

dividend = 2 
divisor = 5

整数除法得出0,而; 因此,当执行上面的步骤3时,将计算分红的值(减去零):

dividend - 0 = 2  —> 2 % 5 = 2 

申请将模数应用于负数

发生地板除法,其中整数除法的值四舍五入到最低整数值:

import math 

x = -1.1
math.floor(-1.1) = -2 

y = 1.1
math.floor = 1

因此,当您进行整数除法时,您可能会得到与预期不同的结果!

将以上步骤应用于以下除数和除数可以说明模量概念:

dividend: -5 
divisor: 2 

步骤1:应用整数除法

x_int = -5 // 2  = -3

步骤2:将整数除法的结果乘以除数

x_mult = x_int * 2 = -6

步骤3:从乘法变量中减去红利,请注意双负数。

dividend - x_mult = -5 -(-6) = 1

因此:

-5 % 2 = 1

It was hard for me to readily find specific use cases for the use of % online ,e.g. why does doing fractional modulus division or negative modulus division result in the answer that it does. Hope this helps clarify questions like this:

Modulus Division In General:

Modulus division returns the remainder of a mathematical division operation. It is does it as follows:

Say we have a dividend of 5 and divisor of 2, the following division operation would be (equated to x):

dividend = 5
divisor = 2

x = 5/2 
  1. The first step in the modulus calculation is to conduct integer division:

    x_int = 5 // 2 ( integer division in python uses double slash)

    x_int = 2

  2. Next, the output of x_int is multiplied by the divisor:

    x_mult = x_int * divisor x_mult = 4

  3. Lastly, the dividend is subtracted from the x_mult

    dividend – x_mult = 1

  4. The modulus operation ,therefore, returns 1:

    5 % 2 = 1

Application to apply the modulus to a fraction

Example: 2 % 5 

The calculation of the modulus when applied to a fraction is the same as above; however, it is important to note that the integer division will result in a value of zero when the divisor is larger than the dividend:

dividend = 2 
divisor = 5

The integer division results in 0 whereas the; therefore, when step 3 above is performed, the value of the dividend is carried through (subtracted from zero):

dividend - 0 = 2  —> 2 % 5 = 2 

Application to apply the modulus to a negative

Floor division occurs in which the value of the integer division is rounded down to the lowest integer value:

import math 

x = -1.1
math.floor(-1.1) = -2 

y = 1.1
math.floor = 1

Therefore, when you do integer division you may get a different outcome than you expect!

Applying the steps above on the following dividend and divisor illustrates the modulus concept:

dividend: -5 
divisor: 2 

Step 1: Apply integer division

x_int = -5 // 2  = -3

Step 2: Multiply the result of the integer division by the divisor

x_mult = x_int * 2 = -6

Step 3: Subtract the dividend from the multiplied variable, notice the double negative.

dividend - x_mult = -5 -(-6) = 1

Therefore:

-5 % 2 = 1

回答 13

%(模)运算符从第一个参数除以第二个参数得出余数。首先将数字参数转换为通用类型。

3 + 2 + 1-5 + 4%2-1/4 + 6 = 7

这基于运算符优先级。

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.

3 + 2 + 1 – 5 + 4 % 2 – 1 / 4 + 6 = 7

This is based on operator precedence.


回答 14

%3 % 2 = 14 % 2 = 0

/ 是(在这种情况下为整数)除,因此:

3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
1 + 4%2 - 1/4 + 6
1 + 0 - 0 + 6
7

% is modulo. 3 % 2 = 1, 4 % 2 = 0

/ is (an integer in this case) division, so:

3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
1 + 4%2 - 1/4 + 6
1 + 0 - 0 + 6
7

回答 15

这是模运算 http://en.wikipedia.org/wiki/Modulo_operation

http://docs.python.org/reference/expressions.html

因此,按照操作顺序,

(3 + 2 + 1-5)+(4%2)-(1/4)+ 6

(1)+(0)-(0)+ 6

7

1/4 = 0,因为我们在这里进行整数运算。

It’s a modulo operation http://en.wikipedia.org/wiki/Modulo_operation

http://docs.python.org/reference/expressions.html

So with order of operations, that works out to

(3+2+1-5) + (4%2) – (1/4) + 6

(1) + (0) – (0) + 6

7

The 1/4=0 because we’re doing integer math here.


回答 16

就像许多类似C的语言一样,它是余数或模运算。有关数字类型信息,请参见文档-int,float,long,complex

It is, as in many C-like languages, the remainder or modulo operation. See the documentation for numeric types — int, float, long, complex.


回答 17

模数-左操作数除以右操作数,并返回余数。

如果有帮助:

1:0> 2%6
=> 2
2:0> 8%6
=> 2
3:0> 2%6 == 8%6
=> true

… 等等。

Modulus – Divides left hand operand by right hand operand and returns remainder.

If it helps:

1:0> 2%6
=> 2
2:0> 8%6
=> 2
3:0> 2%6 == 8%6
=> true

… and so on.


回答 18

我发现掌握模数运算符(%)的最简单方法是进行长除法。它是余数,可用于确定数字是偶数还是奇数:

4%2 = 0

  2
2|4
 -4
  0


11%3 = 2

  3
3|11
 -9
  2

I have found that the easiest way to grasp the modulus operator (%) is through long division. It is the remainder and can be useful in determining a number to be even or odd:

4%2 = 0

  2
2|4
 -4
  0


11%3 = 2

  3
3|11
 -9
  2