Operations In Python

Thoa Shook
4 min readFeb 1, 2020

--

Order of Operations in Python

Order of operations also called operate precedence. It is the order that an operator is executed. In Python language, the following levels of operate precedence is applied from the highest to lowest operate precedence.

Let walks through our first example: 12 //4 + 2 **4–5. In this example, the exponentiation has a highest order, it should be evaluated first, then the floor division, and subtraction. The steps are:

12 //4 + 2 ** 4 - 5
12//4 + 16 - 5
3 + 16 - 5
19 - 5
14

Let’s take a look at another example: 3 + (4 — (11 // 5))** 2 % 2. The parentheses have the highest precedence, so it will be evaluated first, then the exponential, the modulus, and finally the addition. These are the steps:

3 + (4 — (11 // 5))** 2 % 2
3 + 2**2 %2
3 + 4%2
3 + 0
3

In case you forget:

Floor division ( a // b) also called the integer division returns a quotient in which the digits after the decimal point are removed. In our example above, a normal division of 11/5 = 2.2, but in floor division the result is 2 since we technically remove the digit after the decimal point.

Modulus (a % b) also called the remainder operator or integer remainder operator works on integers and yields the remainder when the first operand is divided by the second. In our example above 4 % 2 = 0 because the remainder of normal division 4/2 is 0.

At (@) also called the matrix multiplication operator does not work with numeric types. It does work in matrix multiplication in numpy arrays.

a = [1,2,3]
b = [4,5,6]
a @ b
unsupported operand type(s) for @: ‘list’ and ‘list’

Comparison Operators in Python

Let’s compare this expression: (12+1//5)-4 > 2**3/4. Based on the operate precedence table above, we can see that comparison operators have lower precedence than arithmetic operators. We should calculate the numbers first, then evaluate the comparison. The steps are:

(12 + 0 ) - 4 > 2 ** 3 / 4
12 - 4 > 2 ** 3 /4
12 -4 > 8/4
12 - 4 > 2
8 > 2
True

Chained Comparison Operators

Evaluate this expression: 4 >8 >12 >=3. In this example, Python will check if 4 > 8 and 8 > 12 and 12 >= 3 simultaneously.

Let’s take a look at another example and how Python operate.

3** 2 > 5 <= (7 +3)
3 ** 2 > 5 <= 10
9 > 5 <= 10
9 >5 and 5 <=10
True and True
True

Boolean Operators In Python

Three boolean operators in python are: and, or , not

The and operator

The and operator is a binary operator and is placed between 2 arguments. The arguments must be either True or False. The result is True if both arguments are True, and the outcome is False if one of the arguments is False. Assume that a and b are two arguments, we can construct a True table using the and operator.

The or operator

The or operator is binary operator and is also placed between two arguments. The result is True if one of the arguments is True. It is False if both arguments are False. Let summary the results in a table.

The not operator

The not operator is a unary operator. It is placed before an argument, and inverts an argument.

Not True is False, or Not False is True. Below is the summary table.

--

--