What is operator precedence in Python?
Overview
Operator precedence in Python simply refers to the order of operations. Operators are used to perform operations on variables and values.
Python classifies its operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical/Bitwise operators
- Identity operators
- Membership operators
Arithmetic Operators
Common arithmetic operators in Python include:
- Addition
+ - Subtraction
- - Multiplication
* - Division/modulus
/or// - Exponentiation
** - Brackets/Parenthesis
()
Order of operations
The order of operations of the arithmetic operators can be remembered using the acronym BEDMAS.
The acronym stands for the following words, which tell us which operator comes first:
- B= Bracket
- E = Exponentiation
- D = Division
- M = Multiplication
- A = Addition
- S = Subtraction
Through the acronym, we can see that the bracket/parenthesis operator comes before the exponentiation operation in Python, according to the order of operations.
The same logic applies to each of the following operators, down to the subtraction operator.
Example
To fully grasp BEDMAS and the order of preference of the operators, let’s take a look at the example below:
X = (5 + 3) * 2 ** 2print(X)
From this program, we can see that there are three operators:
- Bracket (B)
- Exponentiation (E)
- Multiplication (M)
According to operator precedence, Python first deals with the numbers in the bracket operator (B): (5 + 3) = 8.
We then proceed to the exponentiation operator (E): 2 ** 2 = 4.
Finally, the results of both the bracket (8) and exponentiation (4) operators are then executed using the multiplication operator (M): 8 * 4 = 32.
In short, Python followed the order of operators outlined in BEDMAS.
Example
The same logic applies to the examples below:
x = 3 + 8 * 2** 3print(x)y= (4 * 2) + 6 / 3 - 2print(y)
The value of x in the example above follows E,M,A order of operations while the value of y follows B,D,S order.
Assignment Operators
Common assignment operators in Python include:
- Assign operator
= - Add AND operator
+= - Subtract AND operator
-= - Multiply AND operator
*= - Divide AND operator
/= - Modulo AND operator
%= - Exponent AND operator
**=
Order of operation
Since these operators are not associative, there is no order of operation for them. They are always performed or declared exclusively.
Example
# using the assigm operatorx = 6y = 2# to generate the Add AND operator i.e x + y = 8x += yprint(x)# to generate the subtract AND operator i.e x - y = 8 - 2 = 6x -= yprint(x)# to generate the multiply AND ooperator i.e x * y = 6 * 2 = 12x *= yprint(x)# to generate the divide AND operator i.e x / 2 = 12 / 2 = 6x /= yprint(x)# to generate the modulo AND operator 1.e x %= 2 = 6 % 2 = 0x %= yprint(x)
Comparison Operators
Comparison operators include:
- Equal to
== - Not equal to
!= - Greater than
> - Less than
< - Greater than or equal to
>= - Less than or equal to
<=
Order of operation
Since these operators are not associative, there is no order of operation for them. They are always performed or declared exclusively.
Example
The program below introduces each of the comparison operators:
x = 6y = 2if x == y:print('x is equal to y')else:print('x is not equal to y')if x != y:print('x is not equal to y')else:print('x is equal to y')if x > y:print('x is greater than y')else:print('x is not greater thab y')if x < y:print('x is less than y')else:print('x is not less than y')if x >= y:print('x is greater than or equal to y')else:print('x is not greater than or equal to y')if x <= y:print('x is less than or equal to y')else:print('x is not less than or equal to y')
Logical operators
Logical operators in Python include:
- Logical AND
- Logical OR
- Logical NOT
Order of operations
The order of operations for logical operators from the highest to the lowest is as follows:
- NOT
- AND
- OR
# Precedence of 'or' & 'and'name = "Theophilus"age = 0if name == "Theophilus" or name == "John" and age >= 2 :print("Hello! Welcome.")else :print("Good Bye!!")
The example above shows how the precedence of the logical and is greater than the logical or.
Identity operators
The identity operators in Python include:
- Is
- Is not
The order of precedence of the identity operators in Python from left to right would be: is and is not.
Membership operators
The order of precedence of the membership operators in Python from left to right is in, not in.
Example
In the code below, we check whether or not the values of x = 2 and y = 4 are available in list by using in and not in operators.
x = 2y = 4list = [1, 2, 3, 6, 7 ];if ( x in list ):print("Yes x is in the list")else:print("x can not be found on the list")if ( y not in list ):print("y is not in the given list")else:print("y is in the given list")
The example above shows how the precedence of in is greater than the not in.
Summary
The table below shows the summary of the different operator and their precedencies in python
| Operators | Examples | Operator Precedence(from left to right) |
|---|---|---|
| Arithmetic operators | Addition+, Subtraction-, Multiplication *, Division/modulo/or//,Exponential **, Bracket() |
(),**, /or//,*, +,- |
| Assignment operators | Assign operator =, Add AND operator+=, Subtract AND operator -=, Multiply AND operator *=, Divide AND operator /=, Modulo AND operator %=, Exponent AND operator **= |
No order of operation |
| Comparison operators | equal to ==, not equal to !=, greater than >, less than <, greater than or equal to >=, less than or equal to <= |
No order of operation |
| Logical operators | Logical AND, Logical OR, Logical NOT | NOT, AND, OR |
| Identity operators | Is, Is not | Is, Is not |
| Membership operators | in, not in | in, not in |