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

Preview

Scope: where a variable lives

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

The notice board and your bag

At college there is a big notice board in the hall. Everybody can read it.

You also have a bag. Only you can see inside it.

Variables (names that hold values) work the same way. Some can be seen by the whole program. Some can only be seen inside one small part.

Key words

WordWhat it meansExample
ScopeThe part of a program where a variable can be used.inside one function
Global variableA variable made outside any function. The whole program can see it.the notice board
Local variableA variable made inside a function. Only that function can see it.your bag
FunctionA named piece of code that does one job.def double(x)
ParameterA name inside a function for a value that is passed in.price in def apply(price)
Return valueThe value a function gives back.return price - 2
VariableA name that holds a value.score

The cells students fill in are left blank in this preview.

Try it: global and local

Code: python
team = "Leeds"


def show_score():
    goals = 3
    print(team, goals)


show_score()
print(team)

# team is global: it works everywhere.
# goals is local: try print(goals) at the very end.
# Python will say: NameError.

What is scope?

Scope means the part of a program where a name can be used.

A global variable is made outside any function. Every function can read it. A local variable is made inside a function. It lives only while that function runs, then it disappears.

Global or local?

Have a go

Global or local?

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.

Why have scope?

Imagine 20 programmers, each with a variable called total. If every total were global, they would change each other's values.

Local variables keep a function's work private. A function cannot break a variable it does not own. This also makes bugs easier to find.

Try it: two variables with one name

Code: python
total = 100


def add_up():
    total = 5 + 5
    print("Inside:", total)


add_up()
print("Outside:", total)

# There are two different variables called total.
# One is local. One is global. The local one does not change the global one.

Worked example: follow the values

StepGlobal totalLocal total in add_upOutput
1 Global total is set100does not exist yet
2 add_up starts and sets its own total10010
3 print Inside10010Inside: 10
4 add_up ends100gone
5 print Outside100goneOutside: 100

The cells students fill in are left blank in this preview.

The lesson goes on

8 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.