Lockers with labels
Picture a row of lockers at college. Each locker has a name on the door. Inside is one thing.
A program stores data in the same way. Each place has a name. Inside is a value. The name lets you find the value again.
Key words
| Word | What it means | Example |
|---|---|---|
| Variable | A name that holds a value. The value can change. | score = 10 |
| Constant | A name that holds a value that never changes. | PI = 3.14 |
| Declare | Create a variable or constant and give it a first value. | age = 16 |
| Assignment | Putting a value into a variable. Python uses the = sign. | score = 0 |
| Data type | The kind of thing a value is. | integer, string |
| Integer | A whole number. | 42 |
| Float | A number with a decimal point. | 2.5 |
| String | Text inside quote marks. | "Sam" |
| Boolean | True or False. | True |
The cells students fill in are left blank in this preview.
Watch: lockers with labels
Press Play on the short film below. Watch each value go into the locker named on the left of the = sign.
Film: variables and constants
Variables and constants
Every scene, as text
- A variable is like a locker. Its name is on the door, and one value is inside.
- Declare a variable: its name, then =, then a value. Python works out the data type from the value.
- In maths, = means equal to. In Python it does not. It means: put this value into this name.
- A variable can change. score = score + 10 runs twice: 0, then 10, then 20. A locker holds one thing, so the old value is gone.
- A good name uses letters, numbers and underscores. It never starts with a number, has no spaces and says what it holds. Capital letters make a different name.
- A constant should never change. Write its name in CAPITAL LETTERS. Python does not lock it, so you must remember.
- Variable or constant? The score and the name of the player change, so they are variables. The lives you start with and the number of players on the field stay the same, so they are constants.
Try it: make a variable
name = "Sam"
age = 16
height = 1.75
is_student = True
print(name)
print(age)
print(height)
print(is_student)
# Change the values and press Run.
Make a free account to save your work and track your progress.
What is a variable?
A variable is a name that holds a value. The value can change while the program runs.
You make a variable by writing its name, then =, then the value. This is called declaring it. Python finds the data type from the value.
The variables in the code above
| Name | Value | Data type |
|---|---|---|
| name | "Sam" | String |
| age | 16 | Integer |
| height | 1.75 | Float |
| is_student | True | Boolean |
The cells students fill in are left blank in this preview.
The = sign is not "equal to"
Warning
Many people read = as "equal to", as in maths. In Python, = means "put this value into this name". It copies the value from the right into the name on the left. Two equals signs, ==, ask a question. You meet that later.
Try it: change a variable
score = 0
print(score)
score = score + 10
print(score)
score = score + 10
print(score)
# The value changes each time.
# Add one more line that adds 5.
Make a free account to save your work and track your progress.
Rules for a good name
| Rule | Good | Not allowed or poor |
|---|---|---|
| Use letters, numbers and underscores. | total_score | total-score |
| Never start with a number. | player1 | 1player |
| No spaces. | first_name | first name |
| Capital letters make a different name. | Score and score are two names | Using both by mistake |
| Choose a name that says what it holds. | ticket_price | x |
The cells students fill in are left blank in this preview.
Which names work in Python?
Have a go
Which names work in Python?
3 questionsWhat is a constant?
A constant is a name that holds a value that never changes while the program runs. A bus fare, the number of days in a week, and the value of pi are constants.
Use a constant when the same value appears many times. If the value changes one day, you fix it in one place only.
Try it: use a constant
FARE = 2.5
COLLEGE_DAYS = 5
# 2 bus trips a day: there and back.
weekly_cost = FARE * 2 * COLLEGE_DAYS
print(weekly_cost)
# Change FARE to 3 and run again.
# Every line that uses FARE now uses the new price.
Make a free account to save your work and track your progress.
Python does not lock constants
Important
Python has no special constant. By habit, programmers write the name in CAPITAL LETTERS, such as FARE. This tells every programmer: do not change this. Python will not stop you, so you must remember.
Variable or constant?
| Value | Variable or constant? | Why |
|---|---|---|
| The score in a game | Variable | It goes up and down. |
| The number of lives you start with, 3 | Constant | It is the same every game. |
| The name of the player | Variable | Each player is different. |
| The number of players a football team has on the field, 11 | Constant | It stays the same while the program runs. |
| The temperature now | Variable | It changes all the time. |
The cells students fill in are left blank in this preview.
Your turn: the pizza shop
A pizza shop sells pizzas for 8 pounds each. Delivery costs 2 pounds for the whole order. First, set the two constants at the top. Then write order_total, change_due and order_text. order_text("Sam", 3) gives "Sam ordered 3 pizzas." (with the full stop). Press Run Tests.
PRICE_PER_PIZZA = 0
DELIVERY_FEE = 0
def order_total(pizzas):
# Pizzas cost 8 each. Delivery costs 2 for the whole order.
return 0
def change_due(paid, total):
# The money you give back to the customer.
return 0
def order_text(name, pizzas):
# Text like: Sam ordered 3 pizzas.
return ""
Make a free account to save your work, or press Start lab to work through the whole lesson.
Match the word to its meaning
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go
Exit quiz
6 questionsRemember
Important
A variable is a name for a value that can change. A constant is a name for a value that must not change. Declare both with =, and choose clear names.