Back to Blog

Python adheres to the same principle, using a rule commonly abbreviated as PEMDAS:

What if Einstein didn’t understand the Order of Operations?

Today, we’re going to discuss an essential concept in Python programming: the Order of Operations. This topic isn’t just relevant for beginners; even experienced developers can sometimes run into issues if they don’t fully grasp these rules. Why is this so crucial? Similar to mathematics, Python has specific rules for determining which calculations are performed first in an expression. Ignoring these rules can have a significant impact on the outcome of your code.

A Simple Example

Let’s consider a straightforward arithmetic problem: what is 16 minus 3 times 2? If you evaluate this expression from left to right, you might assume that the answer is 26. However, the correct answer is 10. This discrepancy arises because, in mathematics, there’s a set of guidelines called the Order of Operations, which dictates that multiplication and division should be performed before addition and subtraction.

PEMDAS in Python

Python adheres to the same principle, using a rule commonly abbreviated as PEMDAS:

- P: Parentheses
- E: Exponents
- MD: Multiplication and Division (from left to right)
- AS: Addition and Subtraction (from left to right)

Let’s break this down with some examples (do them in your head😏)

Parentheses

Any calculation inside parentheses is performed first. If there are nested parentheses, the innermost ones are evaluated first.

result1 = (2 + 4) — 5 # Output: 1

Exponents

Exponents are tackled after parentheses. Python performs any calculations involving powers, such as squaring, cubing, etc.

result2 = 4 + 2 ** 3 # Output: 12

Multiplication and Division

Multiplication and division come next. If an expression contains multiple instances of these operations, Python evaluates them from left to right. Extra points if you tell me why the answer is a float.

result3 = 2 + 4 / 2 * 5 # Output: 12.0

Addition and Subtraction

Finally, addition and subtraction are the last operations to be carried out, also evaluated from left to right.

result4 = (2 + 3) ** 2 + 5 ** 210 # Output: 40

The Importance of Order

Understanding the Order of Operations is crucial when coding, as it helps you craft expressions that yield the intended results. Failure to keep these rules in mind may produce outcomes far removed from your expectations.

So, did you get the same answers as the examples provided?

🎉 Ready to Take Your Python Skills to the Next Level? 🎉

If you’ve found this article helpful, you’ll love my upcoming Udemy course, “Easy Python Programming for Absolute Beginners.” With close to 10 hours of lectures, you’ll not only learn the basics but also engage in fun and challenging game-building exercises!

🎁 Limited-Time Offer 🎁

First things first — here’s an offer you can’t resist. Get lifetime access to the course for $9.99 by using the code E50D2D1B057FF695B1A2 at checkout (Course LINK). But act quickly; this offer expires in 4 days.

About Dr. Lawrence Gray

Senior ML Educator & Python Advocate

Senior ML Educator at John Deere, former Director of ML Engineering, and Georgetown Professor. Passionate about making Python and AI accessible to everyone. I teach Python to Fortune 500 professionals and help career changers break into AI.

Learn More About Dr. Gray →

Comments (0)

Leave a Comment
Maximum 1000 characters

No comments yet. Be the first to share your thoughts!