Python Operator Precedence
Before knowing about the operator precedence in Python, we have to know about the operators in Python. So let's have a look at it.
According to one definition, the operator is a symbol that executes a specific operation between two operands. Operators are the core component upon which logic is built in a program in a specific programming language. Here is a list of the various operators that Python provides:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
For example,
print(1+2)
Here, + is the operator, and 1,2 are operands.
Arithmetic Operators:
Using arithmetic operators, arithmetic operations are performed among two operands. The Addition(+), Subtraction (-), Multiplication (*), Division (/), Reminder(%), and floor division (/) operators are all included, as well as the Exponent (**) operator.
Comparison Operators:
The results of comparison operators are True or False Boolean values based on how well the two operands' values compare.
The comparison operators are listed in the following way:
== ,!= ,<= ,>= ,< ,>
Assignment Operators:
The assignment operators are used to transfer the value of the right expression to the left operand. The assignment operators are described in the following way:
= , =+ , -= , *= , %= , **= , //=
Bitwise Operators:
To compare (binary) numbers, we use the following bitwise operators:
- & (binary AND)
- | (binary OR)
- ^(binary XOR)
- ~ (negation)
- << (left shift)
- >>(right shift)
Logical operators:
Logical operators are frequently used in the evaluation of expressions to make decisions. Python supports the following logical operators:
and, or, not
Membership Operators:
Python membership operators can be used to determine whether a value is a member of a particular data structure. If the value is present in the data structure, the answer is true; or else, it returns false.
in , not in are the membership operators.
Identity Operators:
Objects are compared using identity operators to determine whether they are the same object in the same memory location rather than whether they are equal.
is , is not are the identity operators.
Operator Precedence:
In Python, an expression comprises operators, variables, values, etc. The Python interpreter evaluates each operator in an expression that contains multiple operations in accordance with an ordered hierarchy known as operator precedence.
(PEMDAS) Python Operators Precedence Rule :
Python's operator precedence for arithmetic expressions adheres to the PEMDAS rule. The order of operators' precedence is listed below, from high to low.
Parentheses will be assessed first, followed by exponentiation, and so on.
- P – Parentheses
- E – Exponentiation
- M – Multiplication
- D – Division
- A – Addition
- S – Subtraction
In the case of tie means, the associativity rule is applied if the expression contains two operators with equal precedence.
Precedence Table of Python Operators:
Understanding the order in which operators are examined is essential because it indicates which operator should be taken into account first. The precedence tables for the Python operators are listed below.
Operator | Description |
** | The exponent operator is prioritized over all other operators used in the expression. |
~ ,+, - | negation, unary plus, and the minus |
*,/,%,// | Multiplication, Division, modulo, floor division |
+ ,- | Binary plus and minus |
<< ,>> | Left shift, Right shift |
& | Binary AND |
| , ^ | OR, Binary XOR |
<,<=,>,>= | Comparison Operators |
<,>,!=,== | Equality Operators |
= , =+ , -= , *= , %= , **= , //= | Assignment Operators |
is, is not | Identity Operators |
in, not in | Membership Operators |
not, and , or | Logical Operators |
In the case of tie means, the associativity rule is applied if the expression contains two operators with equal precedence.
Associativity Rule:
Except for exponentiation(**), all operators adhere to the left-to-right associativity. It denotes that the expression will be evaluated going from left to right.
Let us take an Example:
50+10-12/3*7
- In this instance, Multiplication and Division have equal precedence, but furthermore, their evaluation will take into account their left-to-right associativity.
- Let's try to break down this expression and solve it using the precedence and associativity rules.
- The parenthesis ( is encountered by the interpreter. Therefore, it will be assessed first.
- There are 4 operators after that(+,-,*,/)
- Precedence of (/,*) is greater than the (+,-)
- In our example, none of the operators are at different levels in the hierarchy table, so we are unable to use the operator precedence.
- For operators with the same precedence, the associativity rule will be used.
- From left to right, this expression will be evaluated.
- 12/3=4, then expression becomes 50+10-4*7
- 4*7=28, then the expression is 50+10-28
- 60-28
- 32 is the final value for the expression.