What do these operators mean (** , ^ , %, //)?
You are correct that **
is the power function.
^
is bitwise XOR.
%
is indeed the modulus operation, but note that for positive numbers, x % m = x
whenever m > x
. This follows from the definition of modulus. (Additionally, Python specifies x % m
to have the sign of 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 3 changed 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:
| 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
**
: exponentiation^
: exclusive-or (bitwise)%
: modulus//
: divide with integral result (discard remainder)
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:
- The
**
operator does exponentiation.a ** b
isa
raised to theb
power. 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 ^ b
will return a value with only the bits set ina
or inb
but not both. This one is simple! - The
%
operator is mostly to find the modulus of two integers.a % b
returns the remainder after dividinga
byb
. 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 % b
can return a string ifa
is a format string andb
is 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 division
at the top of your files. I strongly recommend it!)