←Previous page

02CHANGE + MATH

A box can change.

A box can take its own value, change it, and keep the new one. That is how games count points.

USE THE OLD VALUE

Read the right side first.

score = score + 1 looks strange. Python first works out the right side: the old score plus 1. Then it puts the answer back in the same box.

score = 3→score = score + 1→
score4

3 + 1 is 4. Now the box keeps 4.

THE SHORT WAY

+= adds to the box.

Add to it

score += 1 means the same as score = score + 1.

Take away

lives -= 1 takes one life away from the box.

Start again

score = 0 throws the old value away and puts in 0.

MATH WITH BOXES

Boxes can do math together.

apples = 4 and pears = 3. Python opens both boxes and uses the numbers inside.

Add

apples + pears is 7.

Subtract

apples - pears is 1.

Multiply

apples * 2 is 8. The star means times.

Divide

apples / 2 is 2.0. Dividing always gives a decimal.

JOIN TEXT

+ glues text together.

Let's go slowly, one small step at a time.

  1. Text lives inside quotes

    Quotes tell Python: this is text, keep it just as it is.

    "cat""Hi""Ana"
  2. + glues two pieces

    Python puts the second piece right after the first one, like train cars.

    "sun"+"flower""sunflower"
  3. Python does not add spaces

    You choose the space. Put it inside the quotes.

    "Hi"+"Ana"HiAnaStuck together!
    "Hi␣"+"Ana"Hi AnaNow there is a space.
  4. A box can join too

    name has no quotes, so Python opens the box and uses what is inside.

    name"Ana"
    "Hi␣"+nameHi AnaPython opens the box
  5. Quotes change everything

    Same + sign, different job.

    TEXT: + glues"2"+"3""23"
    NUMBERS: + adds2+35

RUN IT

Count PyBot's stars.

Guess first. Then run real Python and check.

1

Guess the result

score = 3 score = score + 1 print(score)

What will Python show?

score.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: Change + 1 to + 10. Then try the short way: score += 10.

YOUR TURN

Change the boxes.

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

0Completed
10Not tried
0Review
01

Add to the box

stars = 5 stars = stars + 2 print(stars)

What will Python show?

02

The short way

coins = coins + 1

Which line does the same job?

03

Lose a life

lives = 3 lives -= 1 print(lives)

What will Python show?

04

Triple the steps

steps = 2 steps = steps * 3 print(steps)

What will Python show?

05

Two boxes, one answer

apples = 4 pears = 3 print(apples + pears)

What will Python show?

06

Say hello

name = "Ana" print("Hi " + name)

What will Python show?

07

Where is the space?

print("Hi" + "Ana")

What will Python show?

08

Numbers in quotes

print("2" + "3")

What will Python show?

09

Fix PyBot's code

PyBot found a star, but its score is still 0. Fix the middle line so the box keeps the new score.

Goal output
1
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A box can use its old value to make a new one.

score = score + 1 adds one. Next, PyBot meets every math sign.