In Python, the operator precedence determines the order in which operators are evaluated in an expression. Ensuring that your source code performs the intended calculations is crucial. Misunderstanding the operator precedence can lead to subtle bugs and unexpected behaviour in your programs. This article explained everything about the operator Precedence in Python and its examples.
Operators Precedence in Python
The Operator’s precedence in Python determines the order in which different operators in an expression are evaluated. Operators with higher precedence are evaluated before those with lower precedence. This hierarchy of precedence ensures that expressions are evaluated consistently.
The following program demonstrates the Python Operator Precedence:
Example 1: 2 + 3 * 4 = 14
Example 2: 2 ** 3 * 4 = 32
Example 3: (2 + 3) * 4 = 20
Example 4: -2 ** 3 = -8
Example 5: True or False and False = True
Example 6: 5 < 10 == 10 = True
Example 7: 4 & 3 | 2 = 2
Example 8: x if x > 0 else -x = 5
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Python Operators Precedence Table
The following table describes the Operator’s Precedence:
Operators
Description
()
Parentheses for grouping
‘**”
Exponentiation (right associative)
‘+x’, ‘-x’ , ‘~x’
Unary plus, unary minus, bitwise NOT
‘*’ , ‘/’, ‘%’, ‘//’
Multiplication, division, modulo, floor division
‘+’, ‘-’
Addition, Subtraction
‘<<’, “>>”
Bitwise left shift, bitwise right shift
‘&’
Bitwise AND
‘^’
Bitwise XOR
‘
‘
‘==”, ‘!=’ ,’ >’ ‘<’ , ‘>=’ , ‘<=’
Equality, inequality, comparison
‘not’
Logical Not
‘or’
Logical OR
‘if-else’
Conditional expression (ternary operator)
‘=’, ‘+=’, ‘-=’ , ‘*=’ , ‘/=’, etc
Assignment operators and augmented assignments
‘,’
The comma (used for tuple packing and function arguments)
‘:’
It is used in slices, annotations, and dictionary key-value pairs
Python Operators Precedence Rule – PEMDAS
Python operator precedence follows the PEMDAS rule for arithmetic expression. The precedence of operators is listed below in a high-to-low manner.
P – Parentheses
E – Exponentiation
M – Multiplication
D – Division
A – Addition
S – Subtraction
Associativity Rule
In Python, All the operators follow the left-to-right pattern except the exponentiation (**) operator. In other words, the evaluation will proceed from left to right.
For Example : (99 + 55 – 9 /3 * 5)
In this example, the multiplication and division operators have equal precedence. However, they are evaluated according to the left-to-right associativity.
The following program demonstrates the Associativity Rule :
All the comparison operations in Python, such as <,>,==, >=, <=, != have the same priority. The lower arithmetic, shifting and bitwise operations. Unlike in C, Python always follows the conventional mathematical interpretation for expressions like a < b < c.
The following program demonstrates the comparison Operation in Java:
In Python, operator precedence refers to the order in which Python evaluates or performs operators in an expression. It defines how more than one operator is applied within proximity. Keeping things clear and accurate in the code is always better from right to left.
To be certain of the clarity and correctness of source code, parentheses should be used explicitly to define the intended order of operations in all situations where there might be a mixing of operators with different precedence levels.
We know the rules of precedence and apply them by writing expressions evaluated as intended, which may then be used to maintain readable and reliable source code.
FAQs
What is operator precedence?
Operator precedence refers to the rules determining how different operators are evaluated in an expression. Operators with higher precedence are executed before those with lower precedence.
What is associativity?
The associativity determines the order in which operations of the same precedence level are evaluated. Most operators in Python are left-associative, meaning they are evaluated from left to right. However, exponentiation (‘**’) is right-associative and is evaluated from right to left.
What is the precedence of comparison operators?
The comparison operators in Python (eg ‘==’, ‘!=’ ‘<’, ‘>’, ‘<=’, ‘>=’) have lower precedence than arithmetic operators. They are evaluated after arithmetic operations and before logical operators.
How does Python handle the precedence of ‘is’ and ‘is not’ operators?
The ‘is’ and ‘is not’ operators, used for identity comparisons, have the same precedence level as comparison operators. They are evaluated after arithmetic operators and before logical operations.
How does precedence affect the use of the ‘del’ keyword?
The ‘del’ keyword has a lower precedence than most other operators. For example, in ‘del a[1+2]’, the addition is evaluated first before the deletion.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.