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."
Read this program slowly. Line 2 uses the old value of fuel to make a new one.
fuel = 20 fuel = fuel + 15 print(fuel)
| Line | What Python does | fuel after the line |
|---|---|---|
| fuel = 20 | Store 20 in fuel | 20 |
| fuel = fuel + 15 | Work out 20 + 15, then store 35 | 35 |
| print(fuel) | Show what is in fuel | 35 |
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.
| Sign | Meaning | Example | Result |
|---|---|---|---|
| + | add | 6 + 4 | 10 |
| - | take away | 6 - 4 | 2 |
| * | times | 6 * 4 | 24 |
water = 50 water = water - 8 print(water)
pods = 3 pods = pods + 4 pods = pods * 2 print(pods)
lamps = 10 lamps = lamps - 3 lamps = lamps * 2 print(lamps)
| Statement | True 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. | ? |
You traced a changing variable like a real programmer. Tomorrow you will find out what + does with strings.