AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Variables, input and casting

Week 2 · Unit 7.1: Block it out · 3 lessons
1

Names for stored values

Computing 7: Unit 7.1, pp. 8–43
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.

Python
1item = "pitta bread"
2stock = 24
3price = 0.5
4print("Item:", item)
5print("In stock:", stock)
6print("Price:", price)
Output
Item: pitta bread
In stock: 24
Price: 0.5
Worked examples
Python
1stock = 24
2print("Stock:", stock)
3stock = 18
4print("Stock:", stock)
Output
Stock: 24
Stock: 18
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 nameAllowed?Reason
pocket_moneyyesletters and an underscore are fine
pocket moneynoa space is not allowed
2goalsnoa name must not start with a digit
goals2yesa digit is fine after the first letter
Goals and goalsyes, but two variablesPython 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.

Practice
1
What does this program show?
Python
1team = "Lions"
2goals = 3
3goals = 5
4print(team, "scored", goals)
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
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.
4
In a program, pet = "fish". What does print("pet") show, and what does print(pet) show? Explain the difference.