As your Python programs become more complex, expressions often contain multiple operators such as arithmetic, comparison, logical, assignment, and bitwise operators. When Python encounters several operators in the same expression, an important question arises: Which operation should Python perform first?
The answer is determined by operator precedence. Operator precedence defines the order in which Python evaluates operators within an expression. Without precedence rules, Python would not know whether to perform multiplication before addition, comparison before logical operations, or bitwise operations before arithmetic calculations.
Consider the following expression:
result = 5 + 3 * 2
Does Python calculate 5 + 3 first or 3 * 2 first?
Python follows operator precedence rules and evaluates multiplication before addition.
3 * 2 = 6
5 + 6 = 11
Therefore, the value of result becomes:
11
Operator precedence helps ensure that expressions produce consistent and predictable results. However, if you want Python to evaluate expressions in a different order, you can use parentheses, which always have the highest priority.
Understanding operator precedence is essential because almost every Python program contains expressions involving multiple operators. It helps you avoid logical errors, write readable code, and understand how Python evaluates complex expressions.
After completing this lesson, you will be able to:
Operator precedence is the set of rules that determines the order in which Python evaluates operators in an expression.
Operators with higher precedence are evaluated before operators with lower precedence.
For example:
result = 10 + 4 * 2
Python evaluates multiplication first because it has higher precedence than addition.
4 * 2 = 8
10 + 8 = 18
Output:
18
If Python ignored precedence rules and evaluated from left to right, the answer would be incorrect.
10 + 4 = 14
14 * 2 = 28
This demonstrates why operator precedence is necessary for producing correct and predictable results.
Operator precedence ensures that Python evaluates expressions consistently according to well-defined mathematical and programming rules.
Benefits of understanding operator precedence include:
The following table shows the general order in which Python evaluates operators, from highest precedence to lowest precedence.
| Precedence | Operators | Description |
|---|---|---|
| Highest | () |
Parentheses |
| 2 | ** |
Exponentiation |
| 3 | +x, -x, ~x |
Unary Operators |
| 4 | *, /, //, % |
Multiplication, Division, Floor Division, Modulus |
| 5 | +, - |
Addition, Subtraction |
| 6 | <<, >> |
Bitwise Shift |
| 7 | & |
Bitwise AND |
| 8 | ^ |
Bitwise XOR |
| 9 | | |
Bitwise OR |
| 10 | ==, !=, <, >, <=, >=, is, is not, in, not in |
Comparison, Identity, Membership |
| 11 | not |
Logical NOT |
| 12 | and |
Logical AND |
| Lowest | or |
Logical OR |
Although it is helpful to understand this precedence table, experienced Python programmers often use parentheses to make expressions easier to read.
Parentheses have the highest precedence in Python. Any expression inside parentheses is evaluated before the remaining parts of the expression.
result = (5 + 3) * 2
print(result)
Output:
16
Python first evaluates the expression inside the parentheses.
(5 + 3) = 8
8 * 2 = 16
result = 5 + (8 / 2)
print(result)
Output:
9.0
Python first calculates:
8 / 2 = 4.0
5 + 4.0 = 9.0
result = 10 - 2 * 3
print(result)
Output:
4
Evaluation order:
2 * 3 = 6
10 - 6 = 4
result = (10 - 2) * 3
print(result)
Output:
24
Evaluation order:
10 - 2 = 8
8 * 3 = 24
A retail company calculates the total bill after applying a discount. Parentheses ensure the discount is applied before adding shipping charges.
price = 500
discount = 50
shipping = 40
total = (price - discount) + shipping
print(total)
Output:
490
The discounted price is calculated first, followed by the shipping charge.
An analyst calculates the average sales for two regions before comparing them with another value.
sales = (250 + 350) / 2
print(sales)
Output:
300.0
Parentheses ensure that the total sales are calculated before dividing by the number of regions.
In this section, you learned what Python operator precedence is and why it is essential for evaluating expressions correctly. You explored the complete operator precedence table, understood how higher-precedence operators are evaluated before lower-precedence operators, and learned how parentheses override the default evaluation order. Through practical programming examples, business scenarios, and Data Analytics applications, you saw how parentheses improve both correctness and readability. In the next section, you will learn about associativity and explore how arithmetic, comparison, logical, and bitwise operators interact when multiple operators have the same or different precedence levels.
Operator precedence tells Python which operator should be evaluated first. However, when two operators have the same precedence, Python follows another rule called associativity.
Associativity determines the direction in which operators of the same precedence are evaluated.
Most Python operators are evaluated from left to right. However, a few operators, such as the exponentiation operator (**), are evaluated from right to left.
Arithmetic operators such as multiplication, division, addition, and subtraction are evaluated from left to right when they have the same precedence.
result = 20 / 5 * 2
print(result)
Output:
8.0
Evaluation:
20 / 5 = 4
4 * 2 = 8
result = 10 - 3 + 2
print(result)
Output:
9
Evaluation:
10 - 3 = 7
7 + 2 = 9
The exponentiation operator (**) is evaluated from right to left.
result = 2 ** 3 ** 2
print(result)
Output:
512
Python evaluates the expression as:
3 ** 2 = 9
2 ** 9 = 512
It is not evaluated as:
(2 ** 3) ** 2
= 8 ** 2
= 64
Arithmetic operators are among the most frequently used operators in Python. Understanding their precedence helps you write accurate mathematical expressions.
| Precedence | Operators |
|---|---|
| Highest | () |
| 2 | ** |
| 3 | +x, -x |
| 4 | *, /, //, % |
| Lowest | +, - |
result = 4 + 6 * 3
print(result)
Output:
22
Evaluation:
6 * 3 = 18
4 + 18 = 22
result = (4 + 6) * 3
print(result)
Output:
30
result = 18 // 4 + 2
print(result)
Output:
6
Evaluation:
18 // 4 = 4
4 + 2 = 6
Comparison operators have higher precedence than logical operators. Therefore, Python evaluates comparisons before applying logical operations.
print(10 > 5 and 6 < 8)
Output:
True
Evaluation:
10 > 5
↓
True
6 < 8
↓
True
True and True
↓
True
print(5 == 5 or 8 < 3)
Output:
True
Evaluation:
5 == 5
↓
True
8 < 3
↓
False
True or False
↓
True
print(not 8 > 3)
Output:
False
Evaluation:
8 > 3
↓
True
not True
↓
False
Bitwise operators also follow a specific precedence order. Understanding this order is useful when combining multiple bitwise operations in a single expression.
| Operator | Precedence |
|---|---|
<<, >> |
Highest among bitwise operators |
& |
Next |
^ |
Next |
| |
Lowest among bitwise operators |
result = 5 | 3 & 1
print(result)
Output:
5
Evaluation:
3 & 1
↓
1
5 | 1
↓
5
marks = 85
result = marks >= 40 and marks <= 100
print(result)
Output:
True
price = 1000
discount = 200
tax = 50
total = (price - discount) + tax
print(total)
Output:
850
salary = 60000
experience = 4
eligible = salary > 50000 and experience >= 3
print(eligible)
Output:
True
result = 6 & 3 | 1
print(result)
Output:
3
Evaluation:
6 & 3
↓
2
2 | 1
↓
3
result = 2 + 3 * 4 ** 2
print(result)
Output:
50
Evaluation:
4 ** 2 = 16
3 * 16 = 48
2 + 48 = 50
In this section, you learned about associativity and how Python evaluates operators with the same precedence. You explored left-to-right and right-to-left associativity, studied the precedence of arithmetic, comparison, logical, and bitwise operators, and practiced evaluating complex expressions step by step. In the next section, you will explore real-world applications, Data Analytics examples, common mistakes, best practices, and quick practice questions to strengthen your understanding of Python operator precedence.
Operator precedence is used in almost every Python program. Whether you are building business applications, websites, games, automation scripts, or Data Analytics projects, Python automatically follows precedence rules to evaluate expressions correctly.
An e-commerce application calculates the final bill after applying a discount and then adding shipping charges.
price = 1200
discount = 150
shipping = 80
total = (price - discount) + shipping
print(total)
Output:
1130
The parentheses ensure that the discount is applied before the shipping charge is added.
A school checks whether a student has passed all required conditions.
marks = 75
attendance = 90
result = marks >= 40 and attendance >= 75
print(result)
Output:
True
Python evaluates the comparison operators before applying the logical and operator.
A bank determines loan eligibility based on salary and credit score.
salary = 60000
credit_score = 760
eligible = salary >= 50000 and credit_score >= 700
print(eligible)
Output:
True
An online store calculates the discounted price including tax.
price = 1000
discount = 200
tax = 50
final_price = (price - discount) + tax
print(final_price)
Output:
850
A company awards bonuses only to employees who satisfy multiple conditions.
experience = 6
performance = 92
bonus = experience >= 5 and performance >= 90
print(bonus)
Output:
True
Operator precedence is frequently used in Data Analytics for filtering data, validating records, calculating metrics, and preparing datasets before analysis.
sales = (2500 + 3200 + 2800) / 3
print(sales)
Output:
2833.3333333333335
The parentheses ensure that the total sales are calculated before division.
age = 30
income = 70000
eligible = age >= 18 and income >= 50000
print(eligible)
Output:
True
price = 450
stock = 20
available = price < 500 and stock > 0
print(available)
Output:
True
x = 10
y = 20
z = 5
result = x + y * z
print(result)
Output:
110
Multiplication is evaluated before addition.
value = 6 | 2 & 1
print(value)
Output:
6
Python first evaluates 2 & 1, then performs the Bitwise OR operation.
and, or) with bitwise operators (&, |).**).Predict the output before running each program.
print(5 + 3 * 2)
print((5 + 3) * 2)
print(10 > 5 and 8 < 4)
print(2 ** 3 ** 2)
print(5 | 3 & 1)
Determine the output of the following expression by applying Python's operator precedence rules.
result = (8 + 2) * 3 - 4 / 2
print(result)
Try solving it manually before executing the code. Identify which operations Python performs first and explain each step.
In this section, you explored real-world applications of Python operator precedence in business applications and Data Analytics. You learned how precedence rules affect mathematical calculations, logical conditions, comparisons, and bitwise operations. You also reviewed common beginner mistakes, best practices, and practice questions to strengthen your understanding. In the final section, you will complete the lesson with a summary, key takeaways, FAQs, coding exercises, interview questions, a mini project, and an introduction to Python Conditional Statements (if, elif, and else).
In this lesson, you learned how Python determines the order in which operators are evaluated using operator precedence. Whenever an expression contains multiple operators, Python follows predefined precedence rules to ensure that calculations produce consistent and predictable results.
You explored the complete operator precedence table and learned that operators with higher precedence are evaluated before those with lower precedence. You also discovered that parentheses have the highest precedence and can be used to override Python's default evaluation order, making expressions both accurate and easier to read.
You studied associativity, which determines the evaluation order when operators have the same precedence. Most operators are evaluated from left to right, while the exponentiation operator (**) is evaluated from right to left.
Throughout the lesson, you practiced evaluating arithmetic, comparison, logical, and bitwise expressions. You also explored real-world business examples and Data Analytics scenarios where understanding operator precedence helps avoid logical errors and improves code readability.
() have the highest precedence.**) is evaluated from right to left.Operator precedence is the set of rules that determines the order in which Python evaluates operators in an expression.
It ensures that expressions are evaluated consistently and produce correct results without ambiguity.
Parentheses (()) have the highest precedence because expressions inside them are evaluated first.
Associativity determines the evaluation direction when two or more operators have the same precedence.
The exponentiation operator (**) is evaluated from right to left.
Yes. Arithmetic operations are completed before comparison operations are evaluated.
Parentheses improve code readability and make the intended evaluation order clear to other programmers.
Python uses associativity rules to determine the evaluation order.
Use parentheses for complex expressions, understand the precedence table, and test expressions step by step.
Yes. It is essential for writing correct formulas, filtering data, validating conditions, and performing accurate calculations.
Create a Python program that calculates an employee's final salary using operator precedence.
Your program should:
This mini project helps you apply operator precedence, arithmetic operators, comparison operators, and logical operators in a practical business scenario.
Congratulations! You have successfully completed the lesson on Python Operator Precedence. You now understand how Python evaluates expressions, how operator precedence and associativity work together, and why parentheses are essential for writing clear and accurate code.
In the next lesson, you will begin one of the most important topics in Python programming: Conditional Statements. You will learn how to make decisions in your programs using if, elif, and else statements, allowing your applications to execute different blocks of code based on specific conditions.