Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python operator precedence and associativity

Operator precedence and associativity in Python determine the order in which operators are evaluated when an expression contains multiple operators. Understanding these rules is essential to correctly interpret and evaluate complex expressions.

  • Operator Precedence:

Operator precedence is the order in which operators are evaluated when an expression contains multiple operators. Operators with higher precedence are evaluated before operators with lower precedence.

Here is a list of Python operators sorted by their precedence, from highest to lowest:

  • Parentheses: ( )
  • Exponentiation: **
  • Bitwise NOT, Unary plus and minus: ~, +, -
  • Multiplication, Division, Floor Division, Modulus: *, /, //, %
  • Addition, Subtraction: +, -
  • Bitwise shift: <<, >>
  • Bitwise AND: &
  • Bitwise XOR: ^
  • Bitwise OR: |
  • Comparison operators: ==, !=, <, <=, >, >=
  • Identity operators: is, is not
  • Membership operators: in, not in
  • Logical NOT: not
  • Logical AND: and
  • Logical OR: or

Example:

a = 2 + 3 * 4
print(a)  # Output: 14 (3 * 4 is evaluated first, followed by 2 + 12)

b = 2 ** 3 * 4
print(b)  # Output: 32 (2 ** 3 is evaluated first, followed by 8 * 4)
  • Associativity:

Associativity determines the order in which operators with the same precedence are evaluated. Python operators are generally left-associative, meaning they are evaluated from left to right.

Exceptions to this rule are the exponentiation operator (**) and the assignment operators, which are right-associative.

Example of left-associative operators:

a = 2 + 3 - 1
# Both + and - have the same precedence, so they are evaluated from left to right
print(a)  # Output: 4 (2 + 3 is evaluated first, followed by 5 - 1)

Example of right-associative operators:

a = 2 ** 3 ** 2
# The ** operator is right-associative, so it is evaluated from right to left
print(a)  # Output: 512 (3 ** 2 is evaluated first, followed by 2 ** 9)
  • Using parentheses to control the order of evaluation:

To explicitly control the order of evaluation, you can use parentheses. Expressions inside parentheses are evaluated before the surrounding expression.

Example:

a = (2 + 3) * 4
print(a)  # Output: 20 (2 + 3 is evaluated first, followed by 5 * 4)

In conclusion, understanding operator precedence and associativity in Python is essential for correctly interpreting and evaluating expressions with multiple operators. These rules determine the order in which operators are evaluated and can help you write cleaner, more efficient code. To explicitly control the order of evaluation, you can use parentheses to group expressions.

  1. Changing precedence with parentheses in Python expressions:

    • Description: Parentheses can be used to explicitly define the order of evaluation, overriding the default precedence.
    • Code:
    result = (2 + 3) * 4  # Result: 20 (overrides the default precedence)
    
  2. Handling multiple operators and expressions in Python:

    • Description: Python follows the order of precedence when handling multiple operators in an expression. Parentheses can be used to control the order.
    • Code:
    result = 2 + 3 * 4  # Result: 14 (multiplication has higher precedence)
    
  3. Precedence of arithmetic operators in Python:

    • Description: Arithmetic operators have a specific precedence order, with multiplication and division having higher precedence than addition and subtraction.
    • Code:
    result = 2 + 3 * 4 / 2  # Result: 8.0 (follows the precedence of multiplication and division)
    
  4. Operator precedence and Boolean expressions in Python:

    • Description: In Boolean expressions, logical operators have precedence over comparison operators. Parentheses can be used to control the order of evaluation.
    • Code:
    x = 5
    y = 10
    result = x < y and y > 0  # Result: True (logical AND has higher precedence)
    
  5. Common mistakes and pitfalls related to operator precedence in Python:

    • Description: Mistakes may occur when assumptions about precedence are incorrect. It's crucial to use parentheses for clarity and to avoid pitfalls.
    • Code:
    # Incorrect assumption about precedence
    result = 2 + 3 * 4  # Corrected: Use parentheses to explicitly state the desired order