PEMDAS and BODMAS Execution Ordering Rules
🏷️ Numbers and Mathematical Operations / Operator Precedence
When working with mathematical expressions in Python, the order in which operations are performed matters significantly. Without a consistent set of rules, the same expression could yield different results depending on how it is interpreted. Python follows two widely recognized conventions for operator precedence: PEMDAS (common in the United States) and BODMAS (common in other parts of the world). Both represent the same underlying logic, just with different acronyms.
This guide will help you understand how Python evaluates expressions step by step, ensuring you can predict and control the outcome of your calculations.
⚙️ What Are PEMDAS and BODMAS?
PEMDAS and BODMAS are acronyms that describe the order in which mathematical operations are evaluated. They are essentially the same set of rules, just named differently.
- PEMDAS stands for: Parentheses, Exponents, Multiplication, Division, Addition, Subtraction
- BODMAS stands for: Brackets, Orders (powers/roots), Division, Multiplication, Addition, Subtraction
The key difference is that PEMDAS lists Multiplication before Division, while BODMAS lists Division before Multiplication. However, in Python (and standard mathematics), Multiplication and Division have equal precedence and are evaluated from left to right. The same applies to Addition and Subtraction.
🧮 The Precedence Hierarchy in Python
Python evaluates expressions using the following hierarchy, from highest priority to lowest:
- Parentheses / Brackets
() - Exponents / Orders
** - Multiplication and Division
*,/,//,% - Addition and Subtraction
+,-
When operators share the same precedence level, Python evaluates them from left to right.
🕵️ Step-by-Step Evaluation Example
Let's walk through a complex expression to see how Python applies these rules.
Expression: 3 + 4 * 2 ** 2 / (5 - 1)
Step 1: Parentheses / Brackets
- Evaluate (5 - 1) first → 4
- Expression becomes: 3 + 4 * 2 ** 2 / 4
Step 2: Exponents / Orders
- Evaluate 2 ** 2 → 4
- Expression becomes: 3 + 4 * 4 / 4
Step 3: Multiplication and Division (left to right)
- First, 4 * 4 → 16
- Then, 16 / 4 → 4.0
- Expression becomes: 3 + 4.0
Step 4: Addition and Subtraction
- 3 + 4.0 → 7.0
Final Result: 7.0
📊 Comparison Table: PEMDAS vs BODMAS
| Aspect | PEMDAS | BODMAS |
|---|---|---|
| Full Name | Parentheses, Exponents, Multiplication, Division, Addition, Subtraction | Brackets, Orders, Division, Multiplication, Addition, Subtraction |
| First Priority | Parentheses () |
Brackets () |
| Second Priority | Exponents ** |
Orders ** |
| Third Priority | Multiplication and Division (left to right) | Division and Multiplication (left to right) |
| Fourth Priority | Addition and Subtraction (left to right) | Addition and Subtraction (left to right) |
| Practical Difference | None in Python — both evaluate Multiplication and Division equally | None in Python — both evaluate Division and Multiplication equally |
🛠️ Practical Examples for Engineers
Here are some common scenarios you might encounter when writing Python scripts for automation or data processing.
Example 1: Without Parentheses
- Expression: 10 + 5 * 2
- Python evaluates Multiplication first: 5 * 2 = 10
- Then Addition: 10 + 10 = 20
- Result: 20
Example 2: With Parentheses
- Expression: (10 + 5) * 2
- Python evaluates Parentheses first: 10 + 5 = 15
- Then Multiplication: 15 * 2 = 30
- Result: 30
Example 3: Mixed Operators
- Expression: 100 / 4 * 2
- Python evaluates left to right: 100 / 4 = 25.0, then 25.0 * 2 = 50.0
- Result: 50.0
Example 4: Using Exponents
- Expression: 2 ** 3 + 4 * 2
- Python evaluates Exponent first: 2 ** 3 = 8
- Then Multiplication: 4 * 2 = 8
- Then Addition: 8 + 8 = 16
- Result: 16
⚠️ Common Pitfalls to Avoid
- Assuming left-to-right for all operators: Only operators with the same precedence are evaluated left to right. Multiplication always happens before Addition, regardless of position.
- Forgetting that Division returns a float: In Python 3, the
/operator always returns a float, even if the division is exact. Use//for integer division. - Overcomplicating with unnecessary parentheses: While parentheses clarify intent, too many can make expressions harder to read. Use them strategically.
- Confusing
**with*: The exponent operator**is not the same as multiplication*. For example,2 ** 3is8, not6.
✅ Best Practices for Writing Expressions
- Use parentheses to make order explicit: Even if Python's precedence rules would evaluate correctly, parentheses improve readability. For example, write
(a + b) * cinstead of relying on the fact thata + b * cwould be evaluated differently. - Break complex expressions into steps: Instead of writing a single long expression, assign intermediate results to variables. This makes debugging and maintenance easier.
- Test with simple values first: Before using an expression in production code, test it with known inputs to verify the output matches your expectations.
- Be mindful of integer vs float division: Use
//when you need an integer result, and/when you need a decimal result.
🔍 Quick Reference: Operator Precedence Table
| Precedence Level | Operators | Description |
|---|---|---|
| 1 (Highest) | () |
Parentheses / Brackets |
| 2 | ** |
Exponentiation |
| 3 | *, /, //, % |
Multiplication, Division, Floor Division, Modulus |
| 4 (Lowest) | +, - |
Addition, Subtraction |
By understanding PEMDAS and BODMAS, you gain full control over how Python evaluates your mathematical expressions. This foundational knowledge will help you write accurate calculations, debug unexpected results, and build confidence in your Python programming skills.
This topic explains how Python decides which mathematical operation to perform first when multiple operators appear in a single expression.
🧮 Example 1: Basic multiplication before addition
This example shows that multiplication happens before addition, even when addition is written first.
result = 3 + 4 * 2
print(result)
📤 Output: 11
🧮 Example 2: Parentheses override default order
This example shows that parentheses force addition to happen before multiplication.
result = (3 + 4) * 2
print(result)
📤 Output: 14
🧮 Example 3: Division and subtraction together
This example shows that division happens before subtraction in a mixed expression.
result = 10 - 6 / 2
print(result)
📤 Output: 7.0
🧮 Example 4: Exponents before multiplication
This example shows that exponentiation (power) is evaluated before multiplication.
result = 2 * 3 ** 2
print(result)
📤 Output: 18
🧮 Example 5: Practical calculation with mixed operators
This example shows a real-world calculation where order of operations matters for correct results.
total_cost = 5 + 3 * 4 + 2
average_cost = total_cost / 3
print(total_cost)
print(average_cost)
📤 Output: 19
📤 Output: 6.333333333333333
📊 Comparison Table: PEMDAS vs BODMAS
| Rule Name | Full Meaning | Same Order? |
|---|---|---|
| PEMDAS | Parentheses, Exponents, Multiplication/Division, Addition/Subtraction | Yes |
| BODMAS | Brackets, Orders, Division/Multiplication, Addition/Subtraction | Yes |
Both rules describe the same execution order — only the naming differs by region.
When working with mathematical expressions in Python, the order in which operations are performed matters significantly. Without a consistent set of rules, the same expression could yield different results depending on how it is interpreted. Python follows two widely recognized conventions for operator precedence: PEMDAS (common in the United States) and BODMAS (common in other parts of the world). Both represent the same underlying logic, just with different acronyms.
This guide will help you understand how Python evaluates expressions step by step, ensuring you can predict and control the outcome of your calculations.
⚙️ What Are PEMDAS and BODMAS?
PEMDAS and BODMAS are acronyms that describe the order in which mathematical operations are evaluated. They are essentially the same set of rules, just named differently.
- PEMDAS stands for: Parentheses, Exponents, Multiplication, Division, Addition, Subtraction
- BODMAS stands for: Brackets, Orders (powers/roots), Division, Multiplication, Addition, Subtraction
The key difference is that PEMDAS lists Multiplication before Division, while BODMAS lists Division before Multiplication. However, in Python (and standard mathematics), Multiplication and Division have equal precedence and are evaluated from left to right. The same applies to Addition and Subtraction.
🧮 The Precedence Hierarchy in Python
Python evaluates expressions using the following hierarchy, from highest priority to lowest:
- Parentheses / Brackets
() - Exponents / Orders
** - Multiplication and Division
*,/,//,% - Addition and Subtraction
+,-
When operators share the same precedence level, Python evaluates them from left to right.
🕵️ Step-by-Step Evaluation Example
Let's walk through a complex expression to see how Python applies these rules.
Expression: 3 + 4 * 2 ** 2 / (5 - 1)
Step 1: Parentheses / Brackets
- Evaluate (5 - 1) first → 4
- Expression becomes: 3 + 4 * 2 ** 2 / 4
Step 2: Exponents / Orders
- Evaluate 2 ** 2 → 4
- Expression becomes: 3 + 4 * 4 / 4
Step 3: Multiplication and Division (left to right)
- First, 4 * 4 → 16
- Then, 16 / 4 → 4.0
- Expression becomes: 3 + 4.0
Step 4: Addition and Subtraction
- 3 + 4.0 → 7.0
Final Result: 7.0
📊 Comparison Table: PEMDAS vs BODMAS
| Aspect | PEMDAS | BODMAS |
|---|---|---|
| Full Name | Parentheses, Exponents, Multiplication, Division, Addition, Subtraction | Brackets, Orders, Division, Multiplication, Addition, Subtraction |
| First Priority | Parentheses () |
Brackets () |
| Second Priority | Exponents ** |
Orders ** |
| Third Priority | Multiplication and Division (left to right) | Division and Multiplication (left to right) |
| Fourth Priority | Addition and Subtraction (left to right) | Addition and Subtraction (left to right) |
| Practical Difference | None in Python — both evaluate Multiplication and Division equally | None in Python — both evaluate Division and Multiplication equally |
🛠️ Practical Examples for Engineers
Here are some common scenarios you might encounter when writing Python scripts for automation or data processing.
Example 1: Without Parentheses
- Expression: 10 + 5 * 2
- Python evaluates Multiplication first: 5 * 2 = 10
- Then Addition: 10 + 10 = 20
- Result: 20
Example 2: With Parentheses
- Expression: (10 + 5) * 2
- Python evaluates Parentheses first: 10 + 5 = 15
- Then Multiplication: 15 * 2 = 30
- Result: 30
Example 3: Mixed Operators
- Expression: 100 / 4 * 2
- Python evaluates left to right: 100 / 4 = 25.0, then 25.0 * 2 = 50.0
- Result: 50.0
Example 4: Using Exponents
- Expression: 2 ** 3 + 4 * 2
- Python evaluates Exponent first: 2 ** 3 = 8
- Then Multiplication: 4 * 2 = 8
- Then Addition: 8 + 8 = 16
- Result: 16
⚠️ Common Pitfalls to Avoid
- Assuming left-to-right for all operators: Only operators with the same precedence are evaluated left to right. Multiplication always happens before Addition, regardless of position.
- Forgetting that Division returns a float: In Python 3, the
/operator always returns a float, even if the division is exact. Use//for integer division. - Overcomplicating with unnecessary parentheses: While parentheses clarify intent, too many can make expressions harder to read. Use them strategically.
- Confusing
**with*: The exponent operator**is not the same as multiplication*. For example,2 ** 3is8, not6.
✅ Best Practices for Writing Expressions
- Use parentheses to make order explicit: Even if Python's precedence rules would evaluate correctly, parentheses improve readability. For example, write
(a + b) * cinstead of relying on the fact thata + b * cwould be evaluated differently. - Break complex expressions into steps: Instead of writing a single long expression, assign intermediate results to variables. This makes debugging and maintenance easier.
- Test with simple values first: Before using an expression in production code, test it with known inputs to verify the output matches your expectations.
- Be mindful of integer vs float division: Use
//when you need an integer result, and/when you need a decimal result.
🔍 Quick Reference: Operator Precedence Table
| Precedence Level | Operators | Description |
|---|---|---|
| 1 (Highest) | () |
Parentheses / Brackets |
| 2 | ** |
Exponentiation |
| 3 | *, /, //, % |
Multiplication, Division, Floor Division, Modulus |
| 4 (Lowest) | +, - |
Addition, Subtraction |
By understanding PEMDAS and BODMAS, you gain full control over how Python evaluates your mathematical expressions. This foundational knowledge will help you write accurate calculations, debug unexpected results, and build confidence in your Python programming skills.
Interactive Views
You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.
This topic explains how Python decides which mathematical operation to perform first when multiple operators appear in a single expression.
🧮 Example 1: Basic multiplication before addition
This example shows that multiplication happens before addition, even when addition is written first.
result = 3 + 4 * 2
print(result)
📤 Output: 11
🧮 Example 2: Parentheses override default order
This example shows that parentheses force addition to happen before multiplication.
result = (3 + 4) * 2
print(result)
📤 Output: 14
🧮 Example 3: Division and subtraction together
This example shows that division happens before subtraction in a mixed expression.
result = 10 - 6 / 2
print(result)
📤 Output: 7.0
🧮 Example 4: Exponents before multiplication
This example shows that exponentiation (power) is evaluated before multiplication.
result = 2 * 3 ** 2
print(result)
📤 Output: 18
🧮 Example 5: Practical calculation with mixed operators
This example shows a real-world calculation where order of operations matters for correct results.
total_cost = 5 + 3 * 4 + 2
average_cost = total_cost / 3
print(total_cost)
print(average_cost)
📤 Output: 19
📤 Output: 6.333333333333333
📊 Comparison Table: PEMDAS vs BODMAS
| Rule Name | Full Meaning | Same Order? |
|---|---|---|
| PEMDAS | Parentheses, Exponents, Multiplication/Division, Addition/Subtraction | Yes |
| BODMAS | Brackets, Orders, Division/Multiplication, Addition/Subtraction | Yes |
Both rules describe the same execution order — only the naming differs by region.