← Back to course
Code Crew G4 / Week 01 / Wednesday
3/6
Week 01 Β· Values and print

Wednesday

Change a value
// Real Python, one line at a time
⏱ about 15 min

Wednesday: Change a value

The fuel gauge on the shuttle reads 20 units. Rocket hooks up the fuel line and pumps in 15 more. "Let's update the program!" he says. "I will just add a new line."

Raven looks at his screen. "What do you notice about the old number?" she asks. "Is it still 20, or is it something new now?"

Nova projects a small glowing box with the word fuel on it. "Interesting," she says. "Would you like a hint? Python works out the right side of the = sign first."

Right side first, then store

Read this program slowly. Line 2 uses the old value of fuel to make a new one.

fuel = 20
fuel = fuel + 15
print(fuel)
LineWhat Python doesfuel after the line
fuel = 20Store 20 in fuel20
fuel = fuel + 15Work out 20 + 15, then store 3535
print(fuel)Show what is in fuel35

The new value replaces the old one. A box holds only one value at a time.

A table like this is called a trace table. It shows each variable after each line.

Math signs in Python

SignMeaningExampleResult
+add6 + 410
-take away6 - 42
*times6 * 424
water = 50
water = water - 8
print(water)
What number does the program above show?
WHY THIS EXERCISEwater = water - 8 uses the old value to make the new one. Programs do this all the time.
pods = 3
pods = pods + 4
pods = pods * 2
print(pods)
After line 1, pods holds
After line 2, pods holds
After line 3, pods holds
ONE BOX, ONE VALUE
  • Read the question.
  • Tap your answer.
A program says x = 5. The next line says x = 9. What does print(x) show?
Try it
Ask a grown-up to say a starting number.
Then they say add 3 or times 2, three times in a row.
Keep a trace table on paper as you go.
lamps = 10
lamps = lamps - 3
lamps = lamps * 2
print(lamps)
After line 1, lamps holds
After line 2, lamps holds
After line 3, lamps holds
RIGHT SIDE FIRST
  • Read the question.
  • Tap your answer.
x = 4 runs. Then x = x + x runs. What does print(x) show?
cups = 2 runs. Then cups = 7 runs. What does print(cups) show?
Which sign means times in Python?
StatementTrue or false?
score = 3, then score = score + 1, makes score 4.?
In Python, 6 * 2 is 8.?
A variable can hold two values at once.?
fuel = fuel - 5 uses the old value of fuel.?
WHY THIS EXERCISERight side first, then store. That rule explains every changing variable.
A program says seeds = 9. Then seeds = seeds - 4 runs. What number is in seeds now?
WHY THIS EXERCISEThe new value, 5, replaces the old value, 9.

You traced a changing variable like a real programmer. Tomorrow you will find out what + does with strings.

← Tuesday