Completed
-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
2.3.2 Operator precedence
Operator precedence determines the order in which operators are evaluated in an expression. When multiple operators are used in an expression, Python follows a set of rules to decide the order of operations. Operators with higher precedence are evaluated first.
If operators have the same precedence, their associativity (whether they are evaluated from left to right or right to left) decides the order.
Operator Precedence Hierarchy
Here’s the general hierarchy of operator precedence in Python, from highest to lowest:
-
Parentheses (())
- Parentheses have the highest precedence. Any expression inside parentheses is evaluated first, regardless of the operators involved.
- Example: 2 * (3 + 4) will first evaluate (3 + 4) = 7, then multiply it by 2.
-
Exponentiation (**)
- Exponentiation has a higher precedence than multiplication or addition.
- Example: 2 + 3 ** 2 will first calculate 3 ** 2 = 9, then add 2, resulting in 11.
-
Unary plus and minus (+, -) and Bitwise NOT (~)
- These operators modify a single operand, such as changing the sign of a number or inverting bits.
- Example: -5 (negation) and ~5 (bitwise negation).
-
Multiplication, Division, Floor Division, Modulus (*, /, //, %)
- These operators have higher precedence than addition or subtraction.
- Example: 5 + 2 * 3 will first multiply 2 * 3 = 6, then add 5, resulting in 11.
-
Addition and Subtraction (+, -)
- Addition and subtraction have the same level of precedence and are evaluated after multiplication and division.
- Example: 5 + 2 - 3 is evaluated from left to right as (5 + 2) - 3 = 4.
-
Comparison operators (==, !=, <, <=, >, >=)
- These operators compare two values and return True or False.
- Example: 5 < 10 results in True.
-
Logical NOT (not)
- not has higher precedence than and or or.
- Example: not True and False is evaluated as (not True) and False = False.
-
Logical AND (and)
- and has higher precedence than or.
- Example: True and False or True is evaluated as (True and False) or True = True.
-
Logical OR (or)
- or has the lowest precedence.
- Example: False or True and False is evaluated as False or (True and False) = False.
Associativity
- Most operators in Python are left-associative, meaning they are evaluated from left to right.
- Example: 5 - 3 - 1 is evaluated as (5 - 3) - 1 = 1.
- The exponentiation operator (**) is right-associative, meaning it is evaluated from right to left.
- Example: 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512.
Example: Understanding Operator Precedence
Consider the following expression:
result = 5 + 2 * 3 ** 2 / 2 - 1
Here’s how Python evaluates it based on operator precedence:
- Exponentiation (3 ** 2): 3 ** 2 = 9
- Multiplication (2 * 9): 2 * 9 = 18
- Division (18 / 2): 18 / 2 = 9.0
- Addition (5 + 9.0): 5 + 9.0 = 14.0
- Subtraction (14.0 - 1): 14.0 - 1 = 13.0
Thus, the final value of result is 13.0.
Example with Parentheses:
Using parentheses can alter the precedence:
result = (5 + 2) * 3 ** 2 / 2 - 1
- Parentheses (5 + 2): (5 + 2) = 7
- Exponentiation (3 ** 2): 3 ** 2 = 9
- Multiplication (7 * 9): 7 * 9 = 63
- Division (63 / 2): 63 / 2 = 31.5
- Subtraction (31.5 - 1): 31.5 - 1 = 30.5
Thus, the final value of result is 30.5.
Summary
- Operator precedence determines the order of operations in an expression.
- Parentheses () have the highest precedence and are always evaluated first.
- Exponentiation ** is evaluated before multiplication, division, and modulus.
- Multiplication, division, and modulus are evaluated before addition and subtraction.
- Logical operators (and, or, not) have lower precedence compared to arithmetic and comparison operators.
Commenting is not enabled on this course.