Operators and Expressions in Python
This chapter describes Python's built-in operators as well as the precedence rules used in the evaluation of expressions.
Operations on Numbers
The following operations can be applied to all numeric types:
Operation |
Description |
x + y |
Addition |
x - y |
Subtraction |
x * y |
Multiplication |
x / y |
Division |
x // y |
Truncating division |
x ** y |
Power (x^{y}) |
x % y |
Modulo (x mod y) |
–x |
Unary minus |
+x |
Unary plus |
The truncating division operator (also known as floor division) truncates the result to an integer and works with both integers and floating-point numbers. As of this writing, the true division operator (/) also truncates the result to an integer if the operands are integers. Therefore, 7/4 is 1, not 1.75. However, this behavior is scheduled to change in a future version of Python, so you will need to be careful. The modulo operator returns the remainder of the division x // y. For example, 7 % 4 is 3. For floating-point numbers, the modulo operator returns the floating-point remainder of x // y, which is x – (x // y) * y. For complex numbers, the modulo (%) and truncating division operators (//) are invalid.
The following shifting and bitwise logical operators can only be applied to integers and long integers:
Operation |
Description |
x << y |
Left shift |
x >> y |
Right shift |
x & y |
Bitwise AND |
x | y |
Bitwise OR |
x ^ y |
Bitwise XOR (exclusive OR) |
~x |
Bitwise negation |
The bitwise operators assume that integers are represented in a 2's complement binary representation. For long integers, the bitwise operators operate as if the sign bit is infinitely extended to the left. Some care is required if you are working with raw bit- patterns that are intended to map to native integers on the hardware. This is because Python does not truncate the bits or allow values to overflow—instead, a result is promoted to a long integer.
In addition, you can apply the following built-in functions to all the numerical types:
Function |
Description |
abs(x) |
Absolute value |
divmod(x,y) |
Returns (x // y, x % y) |
pow(x,y [,modulo]) |
Returns (x ** y) % modulo |
round(x,[n]) |
Rounds to the nearest multiple of 10^{-}^{n} (floating-point numbers only) |
The abs() function returns the absolute value of a number. The divmod() function returns the quotient and remainder of a division operation. The pow() function can be used in place of the ** operator, but also supports the ternary power-modulo function (often used in cryptographic algorithms). The round() function rounds a floating-point number, x, to the nearest multiple of 10 to the power minus n. If n is omitted, it's set to 0. If x is equally close to two multiples, rounding is performed away from zero (for example, 0.5 is rounded to 1 and -0.5 is rounded to -1).
When working with integers, the result of an expression is automatically promoted to a long integer if it exceeds the precision available in the integer type. In addition, the Boolean values True and False can be used anywhere in an expression and have the values 1 and 0, respectively.
The following comparison operators have the standard mathematical interpretation and return a Boolean value of True for true, False for false:
Operation |
Description |
x < y |
Less than |
x > y |
Greater than |
x == y |
Equal to |
x != y |
Not equal to (same as <>) |
x >= y |
Greater than or equal to |
x <= y |
Less than or equal to |
Comparisons can be chained together, such as in w < x < y < z. Such expressions are evaluated as w < x and x < y and y < z. Expressions such as x < y > z are legal, but are likely to confuse anyone else reading the code (it's important to note that no comparison is made between x and z in such an expression). Comparisons other than equality involving complex numbers are undefined and result in a TypeError.
Operations involving numbers are valid only if the operands are of the same type. If the types differ, a coercion operation is performed to convert one of the types to the other, as follows:
- If either operand is a complex number, the other operand is converted to a complex number.
- If either operand is a floating-point number, the other is converted to a float.
- If either operand is a long integer, the other is converted to a long integer.
- Otherwise, both numbers must be integers and no conversion is performed.