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)
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 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:
( )
**
~
, +
, -
*
, /
, //
, %
+
, -
<<
, >>
&
^
|
==
, !=
, <
, <=
, >
, >=
is
, is not
in
, not in
not
and
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 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)
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.
Changing precedence with parentheses in Python expressions:
result = (2 + 3) * 4 # Result: 20 (overrides the default precedence)
Handling multiple operators and expressions in Python:
result = 2 + 3 * 4 # Result: 14 (multiplication has higher precedence)
Precedence of arithmetic operators in Python:
result = 2 + 3 * 4 / 2 # Result: 8.0 (follows the precedence of multiplication and division)
Operator precedence and Boolean expressions in Python:
x = 5 y = 10 result = x < y and y > 0 # Result: True (logical AND has higher precedence)
Common mistakes and pitfalls related to operator precedence in Python:
# Incorrect assumption about precedence result = 2 + 3 * 4 # Corrected: Use parentheses to explicitly state the desired order