Understanding Operator Precedence in Python

BALAKRISHNA S - Sep 4 '23 - - Dev Community

Introduction:

In Python, operators have different levels of precedence, which determines the order in which they are evaluated in an expression. Understanding operator precedence is crucial for writing correct and efficient code. This script provides a list of operators in Python, ordered from highest to lowest precedence.

List of operators in Python, ordered by precedence level

operators = [
    "Parentheses: ()",
    "Exponentiation: **",
    "Unary plus and minus: +x, -x",
    "Multiplication, division, and remainder: *, /, //, %",
    "Addition and subtraction: +, -",
    "Bitwise shift operators: <<, >>",
    "Bitwise AND: &",
    "Bitwise OR: |",
    "Bitwise XOR: ^",
    "Comparison operators: <, <=, >, >=, ==, !=",
    "Logical NOT: not",
    "Logical AND: and",
    "Logical OR: or"
]
Enter fullscreen mode Exit fullscreen mode

Print the list of operators with their precedence levels

print("Operator Precedence in Python:")
for operator in operators:
print(operator)

. . . . . . . . . .
Terabox Video Player