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.
- 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
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.
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.
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.
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.
- 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.