Python. Operators (operations) for working with numerical objects. Operator priority table

Operators (operations) for working with numerical objects. Operator priority table


1. The concept of an operator for processing numbers in an expression

For any number (literal) in Python, the corresponding numerical object is generated that contains:

  • the value of this number (literal);
  • the set of operations or operators that can be performed on this number.

Examples of operators.

x % y # the operator of taking the remainder of the division
x==y # comparison operator
-z # unary minus


2. Classification of operations for working with numerical objects

There are three types of operations for working with numerical objects:

  • expression operators +, , *, /, **, not, yield, and others;
  • built-in math functions round, int, bin, pow, and more;
  • auxiliary modules, for example math, random, sys, contextlib and others.


3. Expression operators. Operator priority table

The following is a table of Python language operators in descending order of priority.

Operator (operation) Definition
{ . . . } Dictionary, set, dictionary generator, set generator (highest priority)
[ . . . ] List, list generator
( . . . ) Tuple, subexpression, expression-generator
x.attr Access to the attribute
x( . . . ) Calling functions and classes. Here x is the name of the function, class
x[l:j:k] Slice
x[i] Take by index i
x ** y Involution
~x Bit operation NO (negation, inversion)
-x, +x Unary minus, identity
x/y, x//y Normal division, division with rounding down
x%y Remainder of division, format
x*y Multiplication, repetition
x-y Subtraction, difference of sets
x+y Summation, concatenation
x<<y, x>>y Shifting x left or right on the y bits
x&y Bit operation AND, intersection of sets
x^y Bit operation “exclusive OR” (XOR), the symmetric difference of sets
x | y Bitwise OR operation, union of sets
x==y, x!=y, x<>y Equality Check Operators. The case x<>y only works in Python 2.6
x>y, x<y, x>=y, x<=y Comparison operators
x is y, x is not y Checking the identity of objects
x in y, x not in y For iterated objects and sets
not x Logical negation
x and y Logical operator AND (y value is calculated only if x value is true
x or y Logical operation OR (y value is calculated only if x is not true)
x if y else z Triple selection operator (x value is calculated only if y value is true)
lambda args: expression Creating an anonymous function
yield x Support for the send protocol in generator functions (in Python 2.5 and later)

Operators can form complex expressions. The interpreter performs calculations in expressions according to priority (see table). Using parentheses (), you can change the priority of operators in expressions. Operators placed in parentheses are executed first.

Examples of expressions

x + y
a < b < c
a < b and b < c
2 * (a + b)


4. What is the sequence of executing statements with the same priority?

If the operators in the expression have the same priority (see table), then they are executed in the direction from left to right, with the exception of the exponentiation operator (**), which runs from right to left.


Related topics