Python operators and their execution precedence

Table of contents:

Python operators and their execution precedence
Python operators and their execution precedence

Each operation has its own operators. Basic operators (addition, subtraction, negation, unary plus, unary minus, assignment) are used in unary and binary operations. Ternary has three arguments: an if condition, an expression if condition==true, and an expression if condition==false.

The following example will help you understand what an operator is.

a=b + c

C is added to variable b, the result is assigned to variable a. The whole example a=b + c is an expression. The variables that appear in it are the operands. The operation being performed is addition, and the operator used for this is “+”.

Python Arithmetic Operators

Pyton provides a huge number of libraries for solving computational problems. A large set of methods puts Python on par with Matlab and Octave. Arithmetic operations are applied relatively to integers of type int, real floats, complex complex.

Operators, operands, result
Operators, operands, result

If only integers are used as arguments to the operation, the result will also be an integer. Operations between floating numbersdot will result in an integer and a fraction. The only operation in which the interaction of integers gives a fractional is division.

All possible arithmetic operations are shown in the table.








Integer part of division

Remainder of division



Adding one number to another is done by the additional operator. Subtraction is done using subtraction. Multiplication of one number by another happens with multiplication. Exponentiation is done using exponenta. Division is used for division.

The modulus (%) operator returns the remainder after dividing the left operand by the right operand. If the variable a=10, the variable b=20, then b%a==0. What is the division operator with a remainder, it is easy to understand the following example. If 9/2==4.5, then 9//2 returns the result equal to 4. Floor division (//) returns an integer from the operation of dividing the left operand by the right one.

Performance of any operations is carried out directly by the numbers themselves or variables, which are assigned numerical values. The result can be another variable or one of the existing ones.

Along withintegers and real numbers in Python, there are complex numbers. They consist of real and imaginary parts. They are written as c=a+bj, where a is the real part,

c.real() a

b - imaginary.

c.imag() b

Arithmetic operations with complex numbers have the same properties as with real ones. The use of complex numbers can be represented on a plane with a rectangular coordinate system. The intersection point a of the X-axis and the Y-axis corresponds to the complex number x + yi. Thus, real numbers are located on the X-axis, and imaginary numbers are located on the vertical Y-axis.


Python operators are used to compare variables. In addition to the standard ones known from mathematical problems, there is a check by value and by type, as well as a check of inequality.

Comparison Operators
Comparison Operators





greater than or equal to


less than or equal to






Comparison operations are performed in the form a x b, where x is the comparison operator.

In programming, the "=" operator works differently than in mathematics. The correspondence of the values of each argument is determined by the “==” operator, but “=” onlyassigns a value. With the help of !=the inequality of variables is checked. This operator can be replaced with “”, which is not a standard operator in other languages like C, Java or Javascript.


Python statements assign a value to a variable.











remainder of division




getting an integer value as a result of division


Assignment is one of the central constructs in programming. With its help, variables are given some values, they can change during the program.

Assignment operators
Assignment operators

Working algorithm:

  • calculation of left side value;
  • calculation of right side value;
  • assigning one value to another - in case of type conflict, they must be cast;
  • return the result of the operation - true or false.

Assignment and mathematical operation operators work like this:

a x b, where x is an operator, means that a=a x b. So a+=b says that the value of variable a is added to the value of variable b, and their result is assigned to variable a. The same happens with other examples. For example, a=b stands for a=ab, that is, a is raised to the power of b, the result is eventually assigned to a.


Checking conditions is done using the Python ternary operator.

If-then condition
If-then condition

It consists of two or three parts:

  • if - the expression to be checked;
  • elif - optional instruction (similar to if else or elseif);
  • else - main instruction.

a=int(input()) if X: A=Y else: A=Z

The expression can be specified in one line.

A=Y if X else Z

The else and elseif parts can be omitted, the expression looks like this:

if 1: print("hello 1")

There are break and continue statements in Python. Break breaks code execution at all levels. Continue stops the current iteration, continues execution from the next point.


These Python operators interpret operands as a sequence of zeros and ones.

Bitwise Operators
Bitwise Operators

They use binary numbers, return the result as a 32-bit number.

A=00000000000000000000000000000000000=100000000000000000000000000000001 A=200000000000000000000000010 A=300000000000000000000000011=25500000000000000000000001111111111111111111111111111111111111111111111111111AR

A negative number in binary format is obtained by changing the bit to its opposite and adding 1.

314 00000000000000000000000100111010 -314 11111111111111111111111011000101 + 1=11111111111111111111110010


returns 1, only if a=b


returns 1, if a=1 or b=1, or a=b


returns 1 only if a=1 or b=1, returns 0 if a=1 and b=1


reverse bits of a variable

a << b

shifts all bits of variable a to the left by b

a >> b

shifts all bits of a to the right by b

a >>> b

shifts all bits of a to the right by b

The difference between a >> b and a >>> b is that when shifting and discarding right values, copies of the first bits are added to the left.

9 00000000000000000000000000001001 9 >> 2 00000000000000000000000000000010 -9 11111111111111111111111111110111 -9 >> 2 11111111111111111111111111111101

But for a >>> b the left values will be filled with zeros.

-9 1111111111111111111111111110111 -9 >>> 2 0011111111111111111111111111101


There are three logical operators in total.


This is:

  • and - returns true if a==b==x;
  • or - returns true if a==x or b==x;
  • not - returns false if a==b==x.


The membership operator checks if a variable is part of some sequence.

  • a in b returns true if it finds variable a in sequence b;
  • a not in b returns true if it does not find the variable a in the sequence b.


  • a is b returns true if the right and left variables point to the same object;
  • a is not b returns true if the variables do not point to the same object.


The list shows operators and expressions sorted by execution priority from lowest to highest.

Python operators and their execution precedence:

  • Lambda expressions.
  • Python conditional statements.
  • Boolean OR.
  • Boolean I.
  • Boolean NOT.
  • Identity operators, memberships, assignment operators.
  • Bitwise OR.
  • Bitwise NOT.
  • Bitwise I.
  • Bitwise bit shift operator.
  • Addition and subtraction.
  • Multiplication and division, including the operation of obtaining the remainder of a division and an integer.
  • Bitwise NOT.
  • Exponentiation.
  • Referring to an array element by index, slicing, accessing a class object,calling a function with arguments.

The first item in the list is a lambda expression. Lambda expression is used to create anonymous functions. The lambda behaves like a normal function and is declared as

def (arguments): return expression

The lambda expression is followed by operations performed by the Python ternary operator.

At the end of the list are array and function manipulation methods. Accessing an array element by index looks like this:


In this case, a is an array, i is the element index.

Slicing means passing a complete copy of an array or a selective sequence of the members of a list. The desired value range is indicated in square brackets [x:y:z]. As arguments, x is the beginning of the countdown, y is the end, and z is the step of passing through the elements of the array at each iteration. X defaults to the beginning of the list, y is the end, and z is one. If you specify z as a negative number, the list values are passed in reverse order from the end to the beginning.

Popular topic

Editor's choice