Back to Python
2026-04-126 min read

Precedence (Python Programming)

Learn Precedence (Python Programming) step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on Python Operator Precedence! Understanding operator precedence is crucial for writing clean, efficient, and bug-free code in Python. In this lesson, we will delve into why operator precedence matters, discuss its implications, and provide examples to help you master this essential concept.

Why This Matters

Operator precedence plays a significant role in Python programming as it determines the order of operations when multiple operators are used in an expression. Understanding operator precedence helps you write clearer code, avoid logical errors, and make your code easier to read for others. It's essential for writing maintainable and scalable programs, especially when dealing with complex expressions.

The Importance of Readability

Clear and readable code is crucial for collaboration, debugging, and long-term maintenance. By understanding operator precedence, you can write expressions that are easy to understand at a glance, reducing the likelihood of errors and making it easier for others to follow your code.

Avoiding Logical Errors

Misunderstanding operator precedence can lead to logical errors in complex expressions. For example, consider the following expression:

a = 2 + 3 * 4 - 5
print(a) # Output: 14, not 7 as expected

In this case, the multiplication operation is performed before the addition, resulting in an incorrect result. To fix this, use parentheses to specify the order of operations:

a = (2 + 3) * 4 - 5
print(a) # Output: 7

Prerequisites

Before diving into Python operator precedence, it is assumed that you have a basic understanding of:

  1. Variables and data types in Python
  2. Basic arithmetic operators (+, -, *, /, %)
  3. Comparison operators (==, !=, <, >, <=, >=)
  4. Logical operators (and, or, not)
  5. Assignment operator (=)
  6. Control structures such as if, for, and while statements
  7. Functions and modules in Python

Core Concept

In Python, operators are categorized into several groups based on their precedence. Operators with higher precedence are evaluated before those with lower precedence. Here's a summary of the most commonly used operators and their precedence:

  1. Parentheses (): Highest precedence; they can be used to override the default operator precedence.
  2. Unary operators (e.g., +, -, ~): Second-highest precedence; they operate on a single operand. Examples include incrementing or decrementing variables using +=, -=, and so on.
  3. Multiplication and division operators (*, /, %, **): They have the same precedence and are evaluated from left to right. The modulus operator % computes the remainder of division. The exponentiation operator ** raises a number to a power.
  4. Addition and subtraction operators (+, -): They have the same precedence and are evaluated from left to right.
  5. Comparison operators (==, !=, <, >, <=, >=): They compare two operands and return a Boolean value.
  6. Logical operators (and, or, not): They combine Boolean expressions and evaluate to True or False. The not operator negates its operand, while and returns True if both operands are True, and or returns True if at least one of the operands is True.
  7. Assignment operator (=): It assigns a value to a variable.

Understanding Precedence with Examples

Let's consider the following example:

a = 2 + 3 * 4
print(a)

Since multiplication has higher precedence than addition, the expression 3 * 4 is evaluated first, resulting in 12. Then, 2 + 12 is calculated, and the final result is 14, which is printed. If we wanted to change the order of operations, we could use parentheses:

a = (2 + 3) * 4
print(a)

Now, the addition operation is performed first, resulting in 5. Then, the result is multiplied by 4, and the final value of 20 is printed.

Worked Example

Let's consider a more complex example involving multiple operators:

a = 2 + 3 * 4 - 5 * 6 / 7 + 8
print(a)

In this case, multiplication and division have higher precedence than addition and subtraction. Here's the order of operations:

  1. 5 * 6 is calculated first, resulting in 30.
  2. Then, 7 is divided by 30, which results in 0.23333333333333334.
  3. Next, the result is added to 8, yielding 8.233333333333333.
  4. The addition operation (2 + 3) is performed next, resulting in 5.
  5. Finally, the sum of 5 and 8.233333333333333 is printed: 13.233333333333333.

Common Mistakes

  1. Misunderstanding operator precedence can lead to logical errors in complex expressions. For example:
a = 5 > 3 and 2 < 4
print(a) # Output: True, not False as expected

In this case, the and operator evaluates both conditions, even though the second condition is unnecessary because the first condition is already True. To fix this, we can simplify the expression:

a = 5 > 3 and 4 < 10
print(a) # Output: True
  1. Forgetting that comparison operators return Boolean values can lead to unexpected results when using logical operators. For example:
a = 5 == 3 or 2 != 4
print(a) # Output: True, not False as expected

In this case, the or operator evaluates both conditions because the first condition is False. To fix this, we can simplify the expression:

a = 5 != 3 or 2 != 4
print(a) # Output: True

Common Mistakes: Parentheses and Order of Operations

  1. Forgetting to use parentheses when necessary can lead to incorrect results due to the default operator precedence:
a = 2 + 3 * 4 - 5
print(a) # Output: 14, not 7 as expected

To fix this, use parentheses to specify the order of operations:

a = (2 + 3) * 4 - 5
print(a) # Output: 7
  1. Using unnecessary parentheses can also lead to errors or less readable code:
a = ((2 + 3) * 4) - 5
print(a) # Output: 10, not an error but less readable than the correct version

In this case, the parentheses are unnecessary because multiplication has higher precedence than addition. To make the code more readable, remove the inner parentheses:

a = 2 + 3 * 4 - 5
print(a) # Output: 7

Practice Questions

  1. What is operator precedence in Python? Why is it important?
  2. Given the expression x = (2 + 3) * 4 - 5, what is the value of x?
  3. Simplify the following expression to make it more readable: a = not (b < c) and d > e
  4. Write a Python program that calculates the factorial of a number using recursion, but ensure that the code is easy to understand by using appropriate variable names and indentation.
  5. Given the expression x = 2 + 3 * (4 - 5) / 6, what is the value of x?
  6. Simplify the following expression: a = not ((b > c) or (d < e)) and f > g
  7. Write a Python program that calculates the sum of all numbers from 1 to 100 using a for loop, but ensure that the code is easy to understand by using appropriate variable names and indentation.
  8. Given the expression x = (2 + 3) * (4 - 5) / (6 + 7), what is the value of x?
  9. Simplify the following expression: a = not ((b > c) or (d < e)) and f >= g
  10. Write a Python program that calculates the product of all pairs of numbers from a list of numbers using nested loops, but ensure that the code is easy to understand by using appropriate variable names and indentation.

FAQ

Why are parentheses important in Python?

Parentheses help clarify the order of operations when multiple operators are used in an expression, ensuring that the code behaves as intended. They can also be used to group sub-expressions for clarity or to override the default operator precedence.

How can I remember the precedence of operators in Python?

A common mnemonic is "Please Do Not Throw Sausage Pizza Away," where each word corresponds to a category of operators from highest to lowest precedence: Parentheses, Division and Multiplication, Negation, Addition and Subtraction, Comparison, Logical Operators, and Assignment.

Is it possible to change the operator precedence in Python?

Yes, you can use parentheses to override the default operator precedence, allowing you to specify the order of operations explicitly. However, it's generally recommended to use parentheses sparingly to maintain code readability.

Precedence (Python Programming) | Python | XQA Learn