runtime
06 / 250
lesson 06 Variables & Data Types · beginner

Store a value

A name holds a value. You can change what it holds, and you can fill it with a line that was typed.

You'll be able to
  • Store a value in a name and print the name
  • Replace the value a name holds
  • Read one typed line with input() on its own line
The idea

A name is a word you choose. The line item = "Train Set" stores the text Train Set in the name item. The = means store this value. It does not mean "equals" yet.

Printing the name shows the value. The quotes are not part of the value, just as they are not printed.

Why bother? A value you store once can be used on many lines. When it changes, you change one line, and every print that uses the name follows.

Run each example to see what it prints. Running examples doesn't count toward progress.

example-1.py · A name holds a value
item = "Train Set"
qty = 2
print("Item:", item)
print("Qty:", qty)

Storing a new value in the same name replaces the old one. The old value is gone. Python does not keep a history of the name. A print shows what the name holds at the moment that line runs.

example-2.py · The same name, a new value
status = "packing"
print(status)
status = "delivered"
print(status)

input("City? ") reads one typed line and gives it back as text. Put it on its own line, stored in a name:

city = input("City? ")

The words in the quotes are the prompt. They are shown, and they do not start a new line. What was typed is not repeated until you print the name. In these exercises the typed line is supplied for you, and Check also tries other typed lines.

example-3.py · Read one typed line
city = input("City? ")
print(city)

Mistakes to avoid. print("city") prints the word city: the quotes make it text, not the name. And a name has to be stored before a line uses it. Printing a name that has not been stored yet stops the program with NameError.

Review
  • A name stores the value on the right of =. Printing the name shows that value.
  • Storing again in the same name replaces the old value. A print shows the value of that moment.
  • input("prompt") reads one typed line. The prompt doesn't end its line. Next: whole numbers and decimals.
Practise this

Loading your progress…

To master this lesson, solve both challenges without hints or the answer, and pass the concept check.

Store a value — Variables & Data Types · Runtime