Python statements and their execution priority

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

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

a = b + c 

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

Python Arithmetic Operators

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

Operators, operands, result

If only integers are used as arguments to the operation, the result will also be integer. Operations between floating-point numbers 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 given in the table.

Addition

+

Subtraction

-

Multiplication

*

Division

/

The whole part of the division

//

Remainder of the division

%

Exponentiation

**

Adding one number to another performs the additional operator. Subtraction is performed using subtraction. Multiplication of one number by another occurs with multiplication. Extending is done using exponenta. For division, division is used.

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

Any operations are carried out directly by the numbers themselves or by variables that are assigned numerical values. The result may be another variable or one of the existing ones.

Along with integers and real numbers, Python has complex numbers. They consist of the real and imaginary parts. They are written in the form 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. Using 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 on the vertical Y axis.

Comparison

Operators in Python 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

more

>

smaller

<

more or equal

> =

less than or equal to

<=

equality

==

inequality

! = or <>

Comparison operations are performed in the form of axb, where x is the comparison operator.

In programming, the = operator does not work as it does in mathematics. The correspondence of the values ​​of each argument is determined by the operator β€œ==”, but β€œ=” only assigns a value. Using! = Checks the inequality of variables. This operator can be replaced as β€œ<>”, which is not a standard operator in other languages ​​like C, Java or Javascript.

Assignment

Python statements assign a value to a variable.

assignment

=

addition

+ =

subtraction

- =

multiplication

* =

division

/ =

remainder of the division

% =

exponentiation

** =

getting integer value as a result of division

// =

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

Assignment Operators

Work algorithm:

  • calculation of left-side value;
  • calculation of the right-hand value;
  • assignment of one value to another - in the event of a conflict of types, they must be converted;
  • return the result of the operation - true or false.

Assignment operators and mathematical operations work according to this principle:

axb, where x is the operator, means that a = ax b. Thus, a + = b indicates that the value of the variable a is added to the value of the variable b, and their result is assigned to the variable a. The same thing happens with other examples. For example, a ** = b stands for a = a ** b, that is, a is raised to the power of b, the result is eventually assigned to a.

Conditions

Condition checking is done using the Python ternary operator.

Condition if

It consists of two or three parts:

  • if - the checked expression;
  • elif - an optional instruction (similar to if else or elseif);
  • else is the main instruction.
 a = int(input()) if X: A = Y else: A = Z 

An expression can be specified on one line.

 A = Y if X else Z 

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

 if 1: print("hello 1") 

In Python, there are break and continue statements. Break interrupts code execution at all levels. Continue stops the current iteration, continues execution from the next point.

Bitwise

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

Bitwise operators

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

 a = 0 #00000000000000000000000000000000 a = 1 #00000000000000000000000000000001 a = 2 #00000000000000000000000000000010 a = 3 #00000000000000000000000000000011 a = 255 #00000000000000000000000011111111 

A negative number in binary format is obtained by replacing the bit with the opposite and adding 1.

 314 #00000000000000000000000100111010 -314 #11111111111111111111111011000101 + 1 = 11111111111111111111111011000110 

&

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

~ a

reverses the bits of a variable

a << b

shifts all bits of the variable a left by the value of b

a >> b

shifts all bits a to the right by b

a >>> b

shifts all bits a to the right by b

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

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

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

 -9 #11111111111111111111111111110111 -9 >>> 2 #00111111111111111111111111111101 

brain teaser

There are three logical operators in total.

Yes no or

It:

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

Affiliation

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

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

Identity

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

Priorities

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

Operators in Python and their execution priority:

  • Lambda expression.
  • Python conditional statements .
  • Boolean OR.
  • Boolean I.
  • Boolean NOT.
  • Identity operators, accessories, assignment operators.
  • Bitwise OR.
  • Bit NOT.
  • Bit I.
  • Bitwise shift operator.
  • Addition and Subtraction.
  • Multiplication and division, including the operation of obtaining the remainder of division and integer.
  • Bit NOT.
  • Exponentiation.
  • Accessing an array element by index, slicing, accessing a class object, calling a function with arguments.

The first item on the list is a lambda expression. Lambda expression is used to create anonymous functions. The lambda behaves like an ordinary function, and is declared as

 def <lambda>(arguments): return expression 

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

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

 a[i] 

In the case under consideration, a is an array, i is the index of the element.

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


All Articles