Let’s break down Python expressions, operators, and precedence rules:

1. Expressions

  • Definition: An expression is any valid combination of values, variables, operators, and function calls that Python can evaluate to a single value.
  • Examples:
    • 5 + 3 (Evaluates to 8)
    • "Hello" + " World!" (Evaluates to “Hello World!”)
    • x * 2 (Evaluates to twice the value of the variable x)
    • len("Python") (Evaluates to 6, the length of the string “Python”)

2. Operators

Operators are special symbols that perform specific operations on values (operands). Here’s a breakdown:

  • Arithmetic Operators:

    • + (Addition)
    • - (Subtraction)
    • * (Multiplication)
    • / (Division)
    • // (Floor Division - result is rounded down to the nearest whole number)
    • % (Modulo - returns the remainder of a division)
    • ** (Exponentiation - raising a number to a power)
  • Comparison Operators:

    • == (Equal to)
    • != (Not equal to)
    • > (Greater than)
    • < (Less than)
    • >= (Greater than or equal to)
    • <= (Less than or equal to)
  • Logical Operators:

    • and (Logical AND - True if both operands are True)
    • or (Logical OR - True if at least one operand is True)
    • not (Logical NOT - Inverts the truth value of the operand)
  • Bitwise Operators (operate on binary representations of numbers):

    • & (Bitwise AND)
    • | (Bitwise OR)
    • ^ (Bitwise XOR)
    • ~ (Bitwise NOT)
    • << (Bitwise left shift)
    • >> (Bitwise right shift)
  • Assignment Operators:

    • = (Assignment)
    • += (Add and assign)
    • -= (Subtract and assign)
    • *= (Multiply and assign)
    • /= (Divide and assign)
    • //= (Floor divide and assign)
    • %= (Modulo and assign)
    • **= (Exponentiate and assign)
    • &= (Bitwise AND and assign)
    • |= (Bitwise OR and assign)
    • ^= (Bitwise XOR and assign)
    • <<= (Bitwise left shift and assign)
    • >>= (Bitwise right shift and assign)
  • Identity Operators:

    • is (True if both variables refer to the same object)
    • is not (True if both variables refer to different objects)
  • Membership Operators:

    • in (True if a value is found in a sequence)
    • not in (True if a value is not found in a sequence)

3. Operator Precedence (Order of Operations)

Python follows a specific order to evaluate operators within an expression. Higher precedence operators are evaluated before lower precedence operators:

  1. Parentheses (): Expressions inside parentheses are always evaluated first.
  2. Exponentiation **
  3. Unary Operators (+, -, ~)
  4. Multiplication, Division, Floor Division, Modulo (*, /, //, %)
  5. Addition, Subtraction (+, -)
  6. Bitwise Shift Operators (<<, >>)
  7. Bitwise AND &
  8. Bitwise XOR ^
  9. Bitwise OR |
  10. Comparison Operators (==, !=, >, <, >=, <=)
  11. Logical NOT not
  12. Logical AND and
  13. Logical OR or
  14. Assignment Operators (=, +=, -=, *=, etc.)

Important Notes:

  • Left-to-Right Evaluation: Operators with the same precedence are usually evaluated from left to right.
  • Parentheses for Clarity: Even if not strictly required by precedence rules, using parentheses can make your code much easier to read and understand.
1# Example
2result = 5 + 3 * 2 ** 2 - (8 // 4) 
3print(result)
4# 15

Let’s break down the above example how Python evaluates this expression using precedence:

  1. Parentheses: (8 // 4) evaluates to 2.
  2. Exponentiation: 2 ** 2 evaluates to 4.
  3. Multiplication: 3 * 4 evaluates to 12.
  4. Addition and Subtraction (left to right):
    • 5 + 12 evaluates to 17.
    • 17 - 2 evaluates to 15.

Therefore, the value stored in the result variable would be 15.

Read: Python Control Flow