Socks, then shoes
Shoes first, then socks? That does not work.
03OPERATORS · ORDER
A line can have many signs. Python follows an order, like a line at the bakery. Parentheses jump to the front.
IN REAL LIFE
Some jobs must happen before others.
Shoes first, then socks? That does not work.
The frosting goes on after the cake is baked.
In math, you multiply before you add. Python does the same.
PYTHON'S ORDER
| Turn | Signs | Example | Answer |
|---|---|---|---|
| 1 | ( ) | (2 + 3) * 4 | 20 |
| 2 | ** | 2 * 3 ** 2 | 18 |
| 3 | * / // % | 2 + 3 * 4 | 14 |
| 4 | + - | 10 - 2 + 1 | 9 |
| 5 | == != < > <= >= | 2 + 3 > 4 | True |
A tip: Two signs on the same turn go left to right. Not sure? Add parentheses. They make the order clear for Python and for people.
STEP BY STEP
Python solves one sign at a time, in its order.
2 + 3 * 4 > 10Four values, three signs. * has the first turn.2 + 12 > 103 * 4 is 12. Now + has its turn.14 > 102 + 12 is 14. The comparison goes last.True14 is more than 10.LOOK FIRST
score += 1 and lives -= 1 from Changing boxes. Python has no ++: write score += 1.
stars *= 2 means stars = stars * 2.
cake //= 2 keeps the whole half of cake.
RUN IT
Guess first. Then run real Python and check.
apples = 2
price = 3
print(apples * price + 1)
print(apples * (price + 1))What will Python show?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Remove the parentheses on line 4. Does the answer change?
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
Which line counts them right?
print(2 + 3 * 4)What will Python show?
print((2 + 3) * 4)What will Python show?
print(2 * 3 ** 2)What will Python show?
print(10 - 4 - 2)What will Python show?
2 + 3 > 4What does Python answer?
stars = 4
stars *= 3
print(stars)What will Python show?
Which line gives 5.0?
PyBot shares 10 + 2 candies with 3 friends, so each gets 4. The code shows 10. Add parentheses so it shows 4.
4OUTPUT
Want a fresh start? Clear only this page.
Next: PyBot uses True and False to choose a path.