Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

2.2 (1 of 3): Variables and constants

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
VariableA name that holds a value. The value can change.score = 10
ConstantA name that holds a value that never changes.PI = 3.14
DeclareCreate a variable or constant and give it a first value.age = 16
AssignmentPutting a value into a variable. Python uses the = sign.score = 0
Data typeThe kind of thing a value is.integer, string
IntegerA whole number.42
FloatA number with a decimal point.2.5
StringText inside quote marks."Sam"
BooleanTrue 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

  1. A variable is like a locker. Its name is on the door, and one value is inside.
  2. Declare a variable: its name, then =, then a value. Python works out the data type from the value.
  3. In maths, = means equal to. In Python it does not. It means: put this value into this name.
  4. 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.
  5. 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.
  6. A constant should never change. Write its name in CAPITAL LETTERS. Python does not lock it, so you must remember.
  7. 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

Code: python
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

NameValueData type
name"Sam"String
age16Integer
height1.75Float
is_studentTrueBoolean

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

Code: python
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

RuleGoodNot allowed or poor
Use letters, numbers and underscores.total_scoretotal-score
Never start with a number.player11player
No spaces.first_namefirst name
Capital letters make a different name.Score and score are two namesUsing both by mistake
Choose a name that says what it holds.ticket_pricex

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

What 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

Code: python
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?

ValueVariable or constant?Why
The score in a gameVariableIt goes up and down.
The number of lives you start with, 3ConstantIt is the same every game.
The name of the playerVariableEach player is different.
The number of players a football team has on the field, 11ConstantIt stays the same while the program runs.
The temperature nowVariableIt 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.

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.