- Python Basics
- Python Home
- Python Environment Setup
- Python History
- Python Applications
- Python Versions
- Python Features
- Python Basic Syntax
- Python print()
- Python Comments
- Python Identifiers
- Python Keywords
- Python Data Types
- Python Variables
- Python Variable Types
- Python Operators
- Python import Statement
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python Numbers
- Python Strings
- Python Tuples
- Python Lists
- Python Dictionary
- Python Functions
- Python Variable Scope
- Python Modules
- Python Date & Time
- Python File I/O
- Python Exception Handling
- Python Advanced
- Python Classes & Objects
- Python Regular Expressions
- Python CGI Programming
- Python Network Programming
- Python Send E-mail
- Python Multi-threading
- Python XML Processing
- Python MySQL Database
- Python GUI Programming
- Python Event Handling
- Python More Topics
- Python Enumeration
- Python Sets
- Python Operator Precedence
- Python operator Module
- Python Ternary Operator
- Python Built-in Functions
- Python range() Function
- Python break Vs continue
- Python pass Statement
- Python pass Vs continue
- Python end (end=)
- Python sep (sep=)
- Python abs()
- Python all()
- Python as Keyword
- Python close()
- Python fileno()
- Python flush()
- Python isatty()
- Python os Module
- Python readable()
- Python Examples
- Python Examples
- Python Test
- Python Online Test
- Give Online Test
- All Test List

« Python Tutorial Python Examples »

In Python, Operator precedence determines the order of operation in an expression with multiple operands and operators. Operator precedence provides the priority of operators that are used in expression. That is, operator precedence determines which operation carried out first, then which operation carried out second, and so on.

That is, if there are multiple operands and operators in an expression, or if we need to create an expression with multiple operands and operators, then we need operator precedence table to understand about the precedence/priority of operators to do the thing in accurate way. Therefore I've created the operator precedence table to provide you the easy way to understand the things.

**Note - **Operator at top row has the highest precedence. Whereas operator at bottom (last) row has the lowest precedence.

For example, operators at 3^{rd} row has higher precedence than operators at 4^{th} row but lower precedence than operators
at 2^{nd} row. Similarly operators at 8^{th} row has higher precedence than operators at 9^{th} row but
lower precedence than operators at 7^{th} row

Operator | Name | Syntax | Precedence |
---|---|---|---|

** | Exponent | a ** b | Highest Precedence |

+, -, ˜ | Unary Plus, Unary Minus, Bitwise NOT | +a, -a, ˜a | |

*, /, //, % | Multiplication, Division, Floor division, Modulus | a*b, a/b, a//b, a%b | |

+, - | Addition, Subtraction | a + b, a - b | |

<<, >> | Bitwise Left Shift, Bitwise Right Shift | a << 2, a>>3 | |

& | Bitwise AND | a & b | |

| | Bitwise OR | a | b | |

^ | Bitwise XOR | a ^ b | |

==, !=, >, >=, <, <= | Comparison Operators | a == b, a != b, a > b, a < b, a >= b, a <= b | |

is, is not | Identity Operators | a is b, a is not b | |

in, not in | Membership Operators | element in a, element not in a | |

not | Logical NOT | not(a > b) | |

and | Logical AND | a > b and a < c | |

or | Logical OR | a > b or a < c | Lowest Precedence |

Here is an example shows how the precedence of operator decides the priority of operation to be evaluate in an expression:

```
a, b, c, d = 2, 3, 4, 5
res = a + b ** c / d
print(res)
```

The output produced by above program is shown in the snapshot given below:

From above example program, the expression gets evaluated in this way:

a + b ** c / d = a + 3 ** 4 / d = a + 81 / d = a + 81 / 5 = a + 16.2 = 2 + 16.2 = 18.2

I've not taken some of the operators in the expression given above, because if I do so, then the final answer will be in the form of True/False. Therefore to create a more understandable expression, I've done the things as shown above. Now the expression given below uses most categories of operator. But before evaluating the expression, the statement given below:

a, b, c, d, e, f = 2, 3, 4, 5, 6, 7

initializes 2 to a, 3 to b, 4 to c, 5 to d, 6 to e, and 7 to f. That is, **a = 2**, **b = 3**, **c = 4**, **d = 5**,
**e = 6**, and **f = 7**. Now let's evaluate the expression based on these variables:

a + b // c ** d != e is not f = a + b // 4 ** 5 != e is not f = a + b // 1024 != e is not f = a + 3 // 1024 != e is not f = a + 0 != e is not f = 2 + 0 != e is not f = 2 != e is not f = 2 != 6 is not f = True is not f = True is not 7 = True

Therefore, True is the final output, you'll see after evaluating the above expression one by one with keeping operator precedence in mind.

« Python Tutorial Python Examples »

© Copyright 2021. All Rights Reserved.