Precedence and Associativity of Operators in Python

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.

PrecedenceOperatorsDescriptionAssociativity
1()ParenthesesLeft to right
2x[index], x[index:index]Subscription, slicingLeft to right
3await xAwait expressionN/A
4**ExponentiationRight to left
5+x, -x, ~xPositive, negative, bitwise NOTRight to left
6*, @, /, //, %Multiplication, matrix, division, floor division, remainderLeft to right
7+Addition and subtractionLeft to right
8<<, >>ShiftsLeft to right
9&Bitwise ANDLeft to right
10^Bitwise XORLeft to right
11|Bitwise ORLeft to right
12in, not in, is, is not, <, <=, >, >=, !=, ==Comparisons, membership tests, identity testsLeft to Right
13not xBoolean NOTRight to left
14andBoolean ANDLeft to right
15orBoolean ORLeft to right
16if-elseConditional expressionRight to left
17lambdaLambda expressionN/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 logicaland” 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.

Hridhya Manoj

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

Leave a Comment