Table of Contents
The arithmetic operator will take precedence over the logical operator. Initially, python will check the arithmetic operators. Relational operators will be checked next. At the end, logical operators will be evaluated.
Therefore, if multiple operators are present in an expression, then the higher precedence will be checked first. However, the associativity will be given more importance while considering the order of evaluation in the operator. Let us look into this article to learn more about the precedence and Associativity of operators in Python.
What is Operator Precedence in Python?
In Python, an expression is of variables, operators, and values. The Python interpreter will come across an expression that has various operations and that will be evaluated with the ordered hierarchy. This process is defined as operator precedence.
The table below has provided all the operators from the highest precedence to the lowest precedence.
Precedence | Operators | Description | Associativity |
---|---|---|---|
1 | () | Parentheses | Left to right |
2 | x[index], x[index:index] | Subscription, slicing | Left to right |
3 | await x | Await expression | N/A |
4 | ** | Exponentiation | Right to left |
5 | +x, -x, ~x | Positive, negative, bitwise NOT | Right to left |
6 | *, @, /, //, % | Multiplication, matrix, division, floor division, remainder | Left to right |
7 | +, – | Addition and subtraction | Left to right |
8 | <<, >> | Shifts | Left to right |
9 | & | Bitwise AND | Left to right |
10 | ^ | Bitwise XOR | Left to right |
11 | | | Bitwise OR | Left to right |
12 | in, not in, is, is not, <, <=, >, >=, !=, == | Comparisons, membership tests, identity tests | Left to Right |
13 | not x | Boolean NOT | Right to left |
14 | and | Boolean AND | Left to right |
15 | or | Boolean OR | Left to right |
16 | if-else | Conditional expression | Right to left |
17 | lambda | Lambda expression | N/A |
18 | := | Assignment expression (walrus operator) | Right to left |
Precedence of Python Operators
It is commonly used in the expression that has more than one operator with different precedence for identifying which operator will be required to do first.
Example
10 + 20 * 30
output
10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30
Python code for the above example
# Precedence of ‘+’ & ‘*’
expr =10+20*30
print(expr)
Output
610
Precedence of logical operators in Python
The if block will be run in the code provided below even though the age is 0. The precedence of logical “and” will be greater than the logical “or“.
# Precedence of ‘or’ & ‘and’
name =”Alex”
age =0
ifname ==”Alex”orname ==”John”andage >=2:
print(“Hello! Welcome.”)
else:
print(“Good Bye!!”)
Output
Hello! Welcome.
Another way to execute the ‘else‘ block is the use of parenthesis (). It will be considered as their precedence and the highest among all the operators.
# Precedence of 'or' & 'and'
name = "John"
age = 0
if (name == " John" or name == " Albert") and age >= 2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
Output
Good Bye!!
Associativity of the Python Operators
The expression has two or more operators with the same precedence, and then the operator associativity will determine it as either to be left or reft or from right to left.
Example
‘*’ and ‘ /’ consist of the same precedence and the associativity will be from Left to Right in the code below.
# Left-associative operators
a = 10 + 5 - 2 # Addition and subtraction are left-associative
print(a) # Output: 13
b = 2 * 3 / 2 # Multiplication and division are left-associative
print(b) # Output: 3.0
# Right-associative operators (only one example in Python, exponentiation)
c = 2 ** 3 ** 2 # Exponentiation is right-associative
print(c)
Output
13
3.0
512
Operators’ Precedence and Associativity in Python
In Python, Operators’ precedence and Associativity are considered the two important characteristics of operators that will be used to check the order of subexpression in the absence of brackets.
Example
100 + 200 / 10 - 3 * 10
100 + 200 / 10 - 3 * 10 is calculated as 100 + (200 / 10) - (3 * 10)
and not as (100 + 200) / (10 - 3) * 10
Python code for the example above
expression = 100 + 200 / 10 - 3 * 10
print(expression)
Output
90.0
What are non-associative operators?
In Python, most of the operators have associativity and this indicates that it can be used to evaluate from left to right or right to left and have the same precedence.
Therefore, few operators are considered as non- associative and this means that they cannot be chained together.
a = 5
b = 10
c = 15
a = b = (a < b) += (b < c)
Output
a = b= (a < b) += (b < c)
^^
SyntaxError: invalid syntax
Conclusion
To conclude, this article has listed about the operator precedence and associativity in Python. Moreover, several examples are also provided for a better understanding of the operator’s precedence. This can allow the students to improve their knowledge and skills regarding the topic.
Precedence and Associativity of Operators in Python- FAQs
Q1. Is Python right associative?
Ans. The associativity will be either from left to right or right to left.
Q2. What is precedence in Python?
Ans. It denotes the order of precedence. Python has divided the operators into several categories such as Arithmetic operators and assignment operators.
Q3. What is the highest precedence?
Ans. Parentheses will have the highest precedence. Higher precedence operators will be performed before the lower precedence operations.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together