4.7. Order of Operations¶
Consider this example: 2 + 3 * 4 - 1
. Depending on the order we combine the
numbers, we might get an answer of:
19
15
13
11
Whenever more than one operator appears in an expression, we must follow a set of rules to determine the correct answer. These rules are called the order of operations, and they make sure that solving math problems stays the same all over the world.
Which operators get done first? Here is a summary of the rules for the math operators, from highest importance (applied first) to lowest (applied last). If you prefer a song instead of a table, listen to this composition from NASA engineer Dajae Williams.
Level |
Operation Category |
Examples |
---|---|---|
(Highest) |
Parentheses: |
|
Exponent: (first value)second value |
|
|
Multiplication and Division: |
|
|
(Lowest) |
Addition and Subtraction: |
|
If two operators have equal importance, like +
and -
, solve them in
order from left to right.
Example
The expression 2 + 3 * 4 - 1
will be completed in this order:
3 * 4
is calculated first, since*
has a higher importance than+
and-
.The
+
expression is calculated next (+
and-
are equal in importance, but the+
is further to the left).Finally, the
-
expression is evaluated, giving us the final result.
Following this one step at a time:
0 1 2 3 | 2 + 3 * 4 - 1 # Evaluate 3 * 4 first.
2 + 12 - 1 # Evaluate 2 + 12 next.
14 - 1 # Evaluate 14 - 1 next.
13 # Final answer.
|
What if we wanted to evaluate the +
and -
before the *
? To make
this happen, use parentheses: (2 + 3) * (4 - 1)
.
0 1 2 3 | (2 + 3) * (4 - 1) # Evaluate (2 + 3) first.
5 * (4 - 1) # Evaluate 4 - 1 next.
5 * 3 # Evaluate 5 * 3 next.
15 # Final answer.
|
4.7.1. Don’t Panic¶
Python automatically applies the order of operations for you!
However, if you ever notice that your program is not calculating the answers
you expect, you and Python probably disagree on how to apply the order of
operations. Python will always calculate based on these order of operations. If you
expect a different result from a set of operations, you may need to change how you’ve written your code.
Do you need to add a set of parentheses, ()
, so that an addition operation is executed before multiplication?
4.7.2. Check Your Understanding¶
Question
What is the value for the following expression?
5 - 2 * 6 / 3 + 1
- 9
- 7
- 2
- 0
Question
What is the output of the code below?
print(1 + 5 % 3)
- 0
- 1
- 2
- 3