Operator Precedence and Associativity of Operators
Operator Precedence in Python
Operators are the key elements for performing any operation in any programming language. Operators tell the equation about the operation to be performed. But what if we have a complex equation where more than one operator is paced between two operands and numbers of operands are more than 2. Ex equation = a+b-c*d/e. For these let’s have a look at Operator Precedence and Associativity of Operators.
Different Operators and their Precedence
S.no | Operators | Name of the Operator | Associativity order |
---|---|---|---|
1 | (, ) | Brackets, Parenthesis | Left to Right |
2 | ** | Exponent | Right to left |
3 | ~ | Bit-wise NOT | Left to Right |
4 | *, /, %, // | Multiplication, Division, Modulo, Floor division | Left to Right |
5 | +, – | Addition, Subtraction | Left to Right |
6 | >>, << | Bit-wise right and left shift | Left to Right |
7 | & | Bit-wise AND | Left to Right |
8 | ^ | Bit-wise XOR | Left to Right |
9 | | | Bit-wise OR | Left to Right |
10 | =, !=, >, <, >=, <= | Comparison Operator | Left to Right |
11 | =, +=, -=, *=, /=, %=, **=, //= | Assignment Operator | Right to left |
12 | is, is not | Identity Operator | Left to Right |
13 | in, not in | Membership Operator | Left to Right |
14 | and, or, not | Logical Operator | Left to Right |
-> Above table shows the precedence and associativity of the operators, Precedence of the operator decrease as we move down in the table.
Precedence in Operators
Precedence of the operator is to define which operator will be given higher priority to solve the equation. If in an equation more than one operator is present than the above table will be referred to perform operations. Different operators have different precedence and different usage. Advantages of following standard operator precedence table are:
- Same result.
- Follow universal grammar
- Simplicity
- Powerful approach
But what if we have 2 operators of the same precedence. These where associativity comes in the game.
Associativity in Operators
Associativity of the operator is defined as in what order the operator is given Precision table should be used. Suppose in an equation we have both addition and subtraction operator, then which of the will be given higher precedence. Ex a+b-c, Some advantaged of following associativity table are:
- Simplify operator usage
- Powerful approach
- Universal fact
To understand this we refer the associativity table so we can solve more complex equations by following global approach.
FAQ's
How does precedence work in Python?
When python code is interpreted, the interpreter evaluates the operator with the highest precedence first. To check for the precedence of the operator one should refer to the precision table of operators.
Which operator has highest priority?
Operators present at the top of the precision table are considered as the operator with the highest precision. And if the operator lies under the same precedence then we refer the associativity table
Does Python use Bodmas?
Every mathematical operation is done by using mathematical rules. BODMAS [Brackets Order Division Multiplication Addition Subsraction]
What is the associativity of operators with the same precedence in Python?
Operators having the same precedence are differentiated by referring associative table. Suppose in an equation both addition and subtraction are used, then the addition is performed first and then the subtraction of operands is performed.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment