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

Tuesday

Variables are named boxes
// Real Python, one line at a time
⏱ about 15 min

Tuesday: Variables are named boxes

Rocket is counting cargo crates in the loading bay. "Twelve crates!" he calls. He writes the number on a scrap of paper, and then the paper floats away. Weightless rooms are tricky.

Raven catches it near the ceiling. "What do you notice about this number?" she asks. "It does not say what it counts," Rocket admits. "It could be crates or cups or anything."

Nova blinks her big blue eyes. "Would you like a hint? A number is easier to find when it has a name."

Store a value with a name

A variable is a name that stores a value. Think of a box with a label on it.

The name goes on the left. The value goes on the right. The = sign means store, not equals.

crates = 12
dock = "Bay 2"
print(crates)
print(dock)
The screen shows
12
Bay 2

Line 1 stores the number 12 in a box named crates. Line 2 stores the string "Bay 2" in a box named dock. Then print shows what is inside each box.

Quotes change everything

Look at these two print lines. One has quote marks and one does not.

crates = 12
print("crates")
print(crates)
LineThe screen showsWhy
print("crates")cratesIn quotes, it is just a string of letters.
print(crates)12No quotes, so Python looks inside the box named crates.
  • A variable name has no spaces. Use an underscore instead, like fuel_tank.
  • A variable name cannot start with a digit. crew2 is fine, but 2crew is not.
  • Python uses small letters for most names.
QUOTES OR NO QUOTES?
  • Read the question.
  • Tap your answer.
The program says tanks = 4. What does print(tanks) show?
The program says tanks = 4. What does print("tanks") show?
Which line stores 9 in a variable named pods?
Variable nameIs it a good Python name?
fuel_tank?
fuel tank?
crew2?
2crew?
WHY THIS EXERCISEPython can only find a box if its name follows the rules.
crew = "Raven"
job = "explorer"
print(job)
Read the program above. Which word shows on the screen?
WHY THIS EXERCISEprint(job) opens the box named job. Reading a program like Python does is called tracing.
Try it
Find three things in your room you could count.
Give each one a good variable name, like pencils or books.
ship = "Comet"
seats = 6
print(seats)
print(ship)
print("ship")
What does line 3 show?
What does line 4 show?
What does line 5 show?
Which variable stores a string?
NAME THE BOX
  • Read the question.
  • Tap your answer.
Which is a good variable name?
speed = 8 runs. What does print(speed) show?
Which line stores "Mars" in a variable named planet?
In pods = 5, which part is the name of the box?

Nice work! You can store a value and find it again. Tomorrow you will change what is inside a box.

← Monday