Operator Precedence in Python


TL ; DR:

  • When we string operations together, Python must know which one to do first. This is called operator precedence.


  • * and / are performed before + and -:

  • print(1 + 2 * 3) # Prints 7
    print(6 - 4 / 2) # Prints 4
    print(5 * 4 - 2 / 2) # Prints 19
    

  • We can add () to force an operation to be performed first:

    print((1 + 2) * 3) # Prints 9
    print((6 - 4) / 2) # Prints 1
    print(5 * (4 - 2) / 2) # Prints 5
    




Full lesson:

When we string operations together, Python must know which one to do first. This is called operator precedence.

This is the hierarchy from highest precedence to lowest precedence:

  • Parentheses are always respected
  • Exponentiation (raise to a power)
  • Multiplication, Division and Remainder
  • Addition and Subtraction
  • Left to right


First example:

print(1 + 2 * 3) # Output: 7

Multiplication is executed before addition, so:

1 + 2 * 3 = 1 + 6 = 7

Second example:

print((1 + 2) * 3) # Output: 9

What's inside parentheses is executed before multiplication, so:

(1 + 2) * 3 = 3 * 3 = 9

Third example:

print(6 / 4 * 3 + 5) # Output: 9.5

Multiplication and division come before addition. Also, multiplication and division have the same priorty so they are executed from left to right:

6 / 4 * 3 + 5 = 1.5 * 3 + 5 = 4.5 + 5 = 9.5

Forth example:

print(6 / ((4 + 2) / 3)) # Output: 3

Computer sees parentheses so it looks at it like this:

6 / ((4 + 2) / 3) = 6 / x, where x = (4 + 2) / 3

I have to compute x first. Parantheses are executed before division:
(4 + 2) / 3 = 6 / 3 = 2

I can replace x with 2:
6 / ((4 + 2) / 3) = 6 / 2 = 3


Assignment
Follow the Coding Tutorial and let's practice with operator precedence!


Hint
Look at the examples above if you get stuck.


Introduction

In Python, when we string multiple operations together, the order in which these operations are executed is determined by operator precedence. Understanding operator precedence is crucial for writing correct and efficient code. It ensures that expressions are evaluated in the intended order, avoiding unexpected results.

Operator precedence is particularly useful in scenarios involving complex mathematical calculations, logical operations, and when combining multiple operators in a single expression.

Understanding the Basics

Operator precedence defines the rules that govern the order in which different operations are performed in an expression. For example, in the expression 1 + 2 * 3, the multiplication is performed before the addition due to higher precedence of the multiplication operator.

Here are some basic rules of operator precedence in Python:

Main Concepts

Let's delve deeper into the key concepts of operator precedence with examples:

Example 1: Basic Precedence

print(1 + 2 * 3) # Output: 7

In this example, multiplication is performed before addition:

1 + 2 * 3 = 1 + 6 = 7

Example 2: Using Parentheses

print((1 + 2) * 3) # Output: 9

Parentheses change the order of operations, so addition is performed before multiplication:

(1 + 2) * 3 = 3 * 3 = 9

Example 3: Multiple Operations

print(6 / 4 * 3 + 5) # Output: 9.5

Multiplication and division are performed before addition, and they are evaluated from left to right:

6 / 4 * 3 + 5 = 1.5 * 3 + 5 = 4.5 + 5 = 9.5

Example 4: Nested Parentheses

print(6 / ((4 + 2) / 3)) # Output: 3

Parentheses are evaluated first, followed by division:

6 / ((4 + 2) / 3) = 6 / 2 = 3

Examples and Use Cases

Let's explore more examples and real-world use cases:

Example 5: Complex Expression

print(2 + 3 * 4 ** 2 / (1 + 1)) # Output: 26.0

Exponentiation is performed first, followed by multiplication, division, and addition:

2 + 3 * 4 ** 2 / (1 + 1) = 2 + 3 * 16 / 2 = 2 + 48 / 2 = 2 + 24 = 26.0

Example 6: Logical Operations

print(True or False and False) # Output: True

Logical AND has higher precedence than logical OR:

True or False and False = True or (False and False) = True or False = True

Common Pitfalls and Best Practices

Here are some common mistakes to avoid and best practices to follow:

Advanced Techniques

Let's explore some advanced techniques related to operator precedence:

Example 7: Combining Logical and Arithmetic Operations

print((2 + 3) * 4 > 10 and 5 < 10) # Output: True

Combining logical and arithmetic operations requires careful attention to precedence:

(2 + 3) * 4 > 10 and 5 < 10 = 20 > 10 and 5 < 10 = True and True = True

Code Implementation

Here are some well-commented code snippets demonstrating the correct use of operator precedence:

# Example 1: Basic Precedence
print(1 + 2 * 3) # Output: 7

# Example 2: Using Parentheses
print((1 + 2) * 3) # Output: 9

# Example 3: Multiple Operations
print(6 / 4 * 3 + 5) # Output: 9.5

# Example 4: Nested Parentheses
print(6 / ((4 + 2) / 3)) # Output: 3

# Example 5: Complex Expression
print(2 + 3 * 4 ** 2 / (1 + 1)) # Output: 26.0

# Example 6: Logical Operations
print(True or False and False) # Output: True

# Example 7: Combining Logical and Arithmetic Operations
print((2 + 3) * 4 > 10 and 5 < 10) # Output: True

Debugging and Testing

Here are some tips for debugging and testing code related to operator precedence:

Thinking and Problem-Solving Tips

Here are some strategies for approaching problems related to operator precedence:

Conclusion

In this lesson, we covered the importance of operator precedence in Python and how it affects the evaluation of expressions. By understanding and applying the rules of operator precedence, you can write more accurate and efficient code. Remember to use parentheses to make the order of operations explicit and always test your expressions to ensure they produce the expected results.

Mastering operator precedence is a fundamental skill that will help you in various programming scenarios, from simple calculations to complex logical operations. Keep practicing and exploring further applications to deepen your understanding.

Additional Resources

Here are some additional resources for further reading and practice: