GoalStore values in variables, change them, and show them with print.
New words
variable · a named place in the computer’s memory that stores a value, which can change while the program runsassignment · storing a value in a variable with the = signvalue · the data (a word or a number) that a variable holds
Explanation
A program often needs to remember things. It stores them in variables. A variable has a name and a value. In Python you create one by assignment, for example stock = 24. Words are written in quotation marks, but numbers are not. A variable name has no spaces, and it is case sensitive, so stock and Stock are different. To show a variable, put its name inside print without quotation marks. Commas join items, and print puts a space between them.
A variable can change. The new value replaces the old one, and the old one is lost. Here the shop sells six loaves, so the stock becomes 18.
Python
1drink = "tea"2print("drink")
3print(drink)
Output
drink
tea
Quotation marks matter. With them, Python shows the letters inside. Without them, Python looks for a variable with that name and shows its value.
Variable name
Allowed?
Reason
pocket_money
yes
letters and an underscore are fine
pocket money
no
a space is not allowed
2goals
no
a name must not start with a digit
goals2
yes
a digit is fine after the first letter
Goals and goals
yes, but two variables
Python is case sensitive
Some names are good and some are not. A name should say what the variable holds.
Class activity
Cups and slips (no computer): label three paper cups with variable names such as team, goals and city. Write values on slips of paper. A partner reads out a program, one line at a time, for example goals = 3 or goals = 5. You put the slip into the right cup. When a cup already has a slip, throw the old one away. Then your partner says print(goals) and you read the slip. Play three rounds. If you have a computer or tablet, type one of the programs into IDLE with an adult.
It shows Lions scored 5. The value 3 was replaced by 5, so only 5 is left in goals. The commas make print put a space between the items.
2
Python shows this message. What is wrong, and how do you fix it?
Python
1city = "Cairo"2print("Welcome to", City)
Output
NameError: name 'City' is not defined
The variable was created as city but print uses City with a capital C. Python is case sensitive, so it is a different name that does not exist. Fix: print("Welcome to", city).
3
Write a program with three variables: pet (the word tortoise), legs (4) and weight (2.5). Show each on its own line, with a label.
Line 1: pet = "tortoise". Line 2: legs = 4. Line 3: weight = 2.5. Then print("Pet:", pet), print("Legs:", legs) and print("Weight:", weight), one on each line.
4
In a program, pet = "fish". What does print("pet") show, and what does print(pet) show? Explain the difference.
print("pet") shows pet, because the quotation marks make it plain text. print(pet) shows fish, because without quotation marks Python looks up the variable and shows its value.