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

Preview

Variables and constants

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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 value, such as a whole number or text.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.

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.

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"

data-callouts.warning.label

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.

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

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

9 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.