DEV Community

Cover image for Understanding Operator Precedence in Python
BALAKRISHNA S
BALAKRISHNA S

Posted on

Understanding Operator Precedence in Python

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)

Top comments (2)

 
balakrishnasajja profile image
BALAKRISHNA S

Hey, if you're finding it hard to get back into coding, start by doing a little bit each day, like 10-15 minutes. Make your coding tasks really easy, and try to work on stuff that excites you, like a fun project

Collapse
 
balakrishnasajja profile image
BALAKRISHNA S • Edited

what kind of help you want?