←Previous page

03OPERATORS · ORDER

Which sign goes first?

A line can have many signs. Python follows an order, like a line at the bakery. Parentheses jump to the front.

IN REAL LIFE

Order matters in real life.

Some jobs must happen before others.

Socks, then shoes

Shoes first, then socks? That does not work.

Bake, then decorate

The frosting goes on after the cake is baked.

Math class

In math, you multiply before you add. Python does the same.

PYTHON'S ORDER

Who goes first?

TurnSignsExampleAnswer
1( )(2 + 3) * 420
2**2 * 3 ** 218
3* / // %2 + 3 * 414
4+ -10 - 2 + 19
5== != < > <= >=2 + 3 > 4True

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

Work it out 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

Every math sign has a shortcut.

You know these

score += 1 and lives -= 1 from Changing boxes. Python has no ++: write score += 1.

Double it

stars *= 2 means stars = stars * 2.

Split it

cake //= 2 keeps the whole half of cake.

RUN IT

Price of the snacks.

Guess first. Then run real Python and check.

1

Guess the result

apples = 2 price = 3 print(apples * price + 1) print(apples * (price + 1))

What will Python show?

snacks.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

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

Put the signs in order.

Try each tiny question. A wrong answer becomes something to review.

0Completed
10Not tried
0Review
01

Count the stickers

Ana buys 2 packs of 5 stickers and gets 3 free stickers.

Which line counts them right?

02

Multiply first

print(2 + 3 * 4)

What will Python show?

03

Parentheses first

print((2 + 3) * 4)

What will Python show?

04

Power first

print(2 * 3 ** 2)

What will Python show?

05

Left to right

print(10 - 4 - 2)

What will Python show?

06

Compare last

2 + 3 > 4

What does Python answer?

07

Triple the stars

stars = 4 stars *= 3 print(stars)

What will Python show?

08

Where do the ( ) go?

PyBot wants half of 6 + 4.

Which line gives 5.0?

09

Fix PyBot's code

PyBot shares 10 + 2 candies with 3 friends, so each gets 4. The code shows 10. Add parentheses so it shows 4.

Goal output
4
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

( ) first, then **, then * / // %, then + -, and comparisons last.

Next: PyBot uses True and False to choose a path.