Python 这些运算符是什么意思 (** , ^ , %, //)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15193927/
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
What do these operators mean (** , ^ , %, //)?
提问by alvas
Other than the standard +, -, *and /operators; but what does these mean (**, ^, %, //) ?
除了标准+,-,*和/运营商; 但这些是什么意思 ( **, ^, %, //) ?
>>> 9+float(2) # addition
11.0
>>> 9-float(2) # subtraction
7.0
>>> 9*float(2) # multiplication
18.0
>>> 9/float(2) # division
4.5
>>>
>>> 9**float(2) # This looks like a square, (i.e. power 2)
81.0
>>> 9**float(3) # So ** is equivalent to `math.pow(x,p)` ?
729.0
How about the ^operator?
又如何^操作?
>>> 9^int(2) # What is `^` in `x^u` , it only allows `int` for `u`
11
>>> 9^int(3)
10
>>> 9^int(4)
13
>>> 9^int(5)
12
>>> 9^int(6)
15
>>> 9^int(7)
14
>>> 9^int(8)
1
>>> 9^int(9)
0
>>> 9^int(10)
3
>>> 9^int(11)
2
>>> 9^int(12)
5
%in x%mreturns a normal remainder modulus, but only if m < x, why is that so? What does %do?
%inx%m返回一个正常的余数模数,但只有当m < x,为什么会这样?有什么作用%?
>>> 9%float(2)
1.0
>>> 9%float(3)
0.0
>>> 9%float(4)
1.0
>>> 9%float(5)
4.0
>>> 9%float(6)
3.0
>>> 9%float(7)
2.0
>>> 9%float(8)
1.0
>>> 9%float(9)
0.0
>>> 9%float(10)
9.0
>>> 9%float(11)
9.0
>>> 9%float(12)
9.0
How about the //operator? what does it do?
又如何//操作?它有什么作用?
>>> 9//float(2)
4.0
>>> 9//float(3)
3.0
>>> 9//float(4)
2.0
>>> 9//float(5)
1.0
>>> 9//float(6)
1.0
>>> 9//float(7)
1.0
>>> 9//float(8)
1.0
>>> 9//float(9)
1.0
>>> 9//float(1)
9.0
>>> 9//float(0.5)
18.0
采纳答案by John Zwinck
**: exponentiation^: exclusive-or (bitwise)%: modulus//: divide with integral result (discard remainder)
**: 求幂^:异或(按位)%: 模数//: 除以整数结果(丢弃余数)
回答by Kyle Strand
You are correct that **is the power function.
你是对的,这**是幂函数。
^is bitwise XOR.
^是按位异或。
%is indeed the modulus operation, but note that for positive numbers, x % m = xwhenever m > x. This follows from the definition of modulus. (Additionally, Python specifies x % mto have the sign of m.)
%确实是模运算,但请注意,对于正数,x % m = x每当m > x. 这遵循模数的定义。(此外,Python 指定x % m具有 的符号m。)
//is a division operation that returns an integer by discarding the remainder. This is the standard form of division using the /in most programming languages. However, Python 3changed the behavior of /to perform floating-point division even if the arguments are integers. The //operator was introduced in Python 2.6 and Python 3 to provide an integer-division operator that would behave consistently between Python 2 and Python 3. This means:
//是通过丢弃余数返回整数的除法运算。这是/大多数编程语言中使用 的标准除法形式。然而,Python 3改变了/执行浮点除法的行为,即使参数是 integers。该//运算符是在 Python 2.6 和 Python 3 中引入的,以提供一个整数除法运算符,该运算符在 Python 2 和 Python 3 之间的行为一致。这意味着:
| context | `/` behavior | `//` behavior |
---------------------------------------------------------------------------
| floating-point arguments, Python 2 & 3 | float division | int divison |
---------------------------------------------------------------------------
| integer arguments, python 2 | int division | int division |
---------------------------------------------------------------------------
| integer arguments, python 3 | float division | int division |
For more details, see this question: Division in Python 2.7. and 3.3
有关更多详细信息,请参阅此问题:Python 2.7 中的分区。和 3.3
回答by Blckknght
You can find all of those operators in the Python language reference, though you'll have to scroll around a bit to find them all. As other answers have said:
您可以在Python 语言参考 中找到所有这些运算符,但您必须稍微滚动一下才能找到所有这些运算符。正如其他答案所说:
- The
**operator does exponentiation.a ** bisaraised to thebpower. The same**symbol is also used in function argument and calling notations, with a different meaning (passing and receiving arbitrary keyword arguments). - The
^operator does a binary xor.a ^ bwill return a value with only the bits set inaor inbbut not both. This one is simple! - The
%operator is mostly to find the modulus of two integers.a % breturns the remainder after dividingabyb. Unlike the modulus operators in some other programming languages (such as C), in Python a modulus it will have the same sign asb, rather than the same sign asa. The same operator is also used for the "old" style of string formatting, soa % bcan return a string ifais a format string andbis a value (or tuple of values) which can be inserted intoa. - The
//operator does Python's version of integer division. Python's integer division is not exactly the same as the integer division offered by some other languages (like C), since it rounds towards negative infinity, rather than towards zero. Together with the modulus operator, you can say thata == (a // b)*b + (a % b). In Python 2, floor division is the default behavior when you divide two integers (using the normal division operator/). Since this can be unexpected (especially when you're not picky about what types of numbers you get as arguments to a function), Python 3 has changed to make "true" (floating point) division the norm for division that would be rounded off otherwise, and it will do "floor" division only when explicitly requested. (You can also get the new behavior in Python 2 by puttingfrom __future__ import divisionat the top of your files. I strongly recommend it!)
- 该
**运营商不幂。a ** b被a提升到b权力。**在函数参数和调用符号中也使用相同的符号,具有不同的含义(传递和接收任意关键字参数)。 - 该
^运营商做一个二进制XOR。a ^ b将返回一个值,其中只设置了 ina或 inb但不是两者都设置的位。这个很简单! - 该
%运营商大多是找两个整数的模量。a % b返回后除以剩余a的b。与某些其他编程语言(例如 C)中的模数运算符不同,在 Python 中,模数将与 具有相同的符号b,而不是与 相同的符号a。相同的运算符也用于字符串格式化的“旧”样式,因此a % b如果a是格式字符串并且b是可以插入到a. - 该
//运营商不Python的版本整数除法。Python 的整数除法与其他一些语言(如 C)提供的整数除法并不完全相同,因为它向负无穷大舍入,而不是向零舍入。与模运算符一起,您可以说a == (a // b)*b + (a % b). 在 Python 2 中,除法是两个整数相除时的默认行为(使用普通除法运算符/)。由于这可能出乎意料(尤其是当您不挑剔作为函数参数的数字类型时),Python 3 已更改为使“真”(浮点)除法成为四舍五入的除法规范否则,它只会在明确要求时进行“地板”划分。from __future__ import division在文件的顶部。我强烈推荐它!)

