← Back to course
Code Crew G6+ / Week 02 / Monday
1/6
Week 02 Β· Variables and input

Monday

Names for values
// Programs that remember and ask
⏱ about 30 min

Monday: Names for values

The fuel gauge on Mission Control's big screen keeps changing. Rocket has written the number on six sticky notes this morning, and each note is already out of date.

"I keep crossing numbers out," he says, holding up a crumpled pile. "There has to be a better way to keep track."

Raven looks at the pile and then at the gauge. "What do you notice?" she asks. "The label never changes. It always says fuel. Only the number beside it changes."

Nova projects a glowing label with an arrow pointing to a number. "I found a pattern," she says. "Give the value a name. When the value changes, the name just points to the new one."

Rocket sweeps the sticky notes into the recycling bin and opens the editor.

Variables

A variable is a name for a value. The line fuel = 100 makes a variable called fuel and stores 100 in it.

In Python, the = sign does not mean equals. It means store: work out the right side, then give that value the name on the left.

That is why fuel = fuel - 30 makes sense. Python takes the old value of fuel, subtracts 30, and stores the answer back in fuel.

# fuel.py
fuel = 100
print("Fuel:", fuel)
fuel = fuel - 30
print("Fuel:", fuel)
fuel = fuel - 25
print("Fuel:", fuel)
Fuel: 100
Fuel: 70
Fuel: 45

Notice what print("Fuel:", fuel) does. It shows the text in quotes as written. For fuel without quotes, it shows the stored value.

Trace it: crew = 3, then crew = crew + 2, then crew = crew * 2. What does print(crew) show?
WHY THIS EXERCISEEach line uses the value crew has right then, so 3 becomes 5, and 5 becomes 10.

Naming rules

  • Names use letters, digits and the underscore _. No spaces.
  • A name cannot start with a digit.
  • Capital letters matter: Fuel and fuel are two different names.
  • Python programmers write names in lowercase with underscores, like fuel_level.
  • Pick names that say what the value means: crew_size beats x.
NameAllowed in Python?
fuel_level?
2nd_stage?
crew size?
stage2?
WHY THIS EXERCISENames may contain digits and underscores, but they cannot start with a digit or contain a space.
At your computer
1. Make a new file in IDLE and type fuel.py.
2. Save it in your mission folder and press F5. Check the three lines.
3. Add two lines at the end: fuel = fuel + 50 and print("Fuel:", fuel).
4. Predict the new last line, then run it.
5. In the shell, type crew = 3 and press Enter. Then type crew and press Enter to see its value.

Practice: trace the boxes

A variable holds whatever value it was given last. Changing one variable never changes another variable, even if it was made from the first one.

Programmers trace a program with a small table: one column for each variable, one row for each line.

# boxes.py
a = 5
b = a + 2
a = 10
print(a, b)
Lineab
a = 55(not made yet)
b = a + 257
a = 10107
TRACE BOXES.PY
  • Read the question.
  • Tap your answer.
What does print(a, b) show?
score = 4, then score = score * 3. What is score now?
Which name best holds the number of tanks left?
x = 2, then y = x * 5, then x = y - 1. What is x?
In the same program, what is y?
speed = 30, then speed = speed + speed. What is speed?
StatementTrue or false?
In Python, = means store the value on the right in the name on the left.?
After b = a + 2, changing a also changes b.?
fuel = fuel - 10 is a mistake because fuel cannot equal fuel minus 10.?
A variable can be given a new value as many times as you like.?
WHY THIS EXERCISEThe = sign stores a value, so a name can be updated any time, and other names keep their own values.

Variables let your programs remember. Tomorrow you will meet the four kinds of values Python uses most.