GCSE · Computer Science · Edexcel · Spec 1CP2

Global vs local variables

Quick bet: number = 5 in the main program, number = 10 inside a subprogram. After it runs, what does the main program print?

Computer Science · Algorithms

Trace table — dry run the code

Step through both versions. Before each Next, guess which column is about to change.

1def change():
2 number = 10
3 print(number)
4number = 5
5change()
6print(number)
7# ---- Version 2: same program, one extra line ----
8def change():
9 global number
10 number = 10
11 print(number)
12number = 5
13change()
14print(number)
global number
local number
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Spot the broken line

Reaching into a subprogram

This program is meant to print a prize and then a total. One line makes it crash. Which line is it?

A student's program — which line goes wrong?

Your turn: predict, then check

Fresh program, same trap. Commit to an answer before you look.

triple() has no global line, so amount is not a global variable inside it. What does this program output, line by line? amount = 100 def triple(n): amount = n * 3 print(amount) triple(100) print(amount)

Why bother avoiding globals?

What is wrong with global variables?

A teammate says: "In this project, let's avoid global variables where we can." Globals are generally seen as bad practice - but why?

Which reason is closest to what you think?
How sure are you?

The alternative

Pass in, return, store

Rebuild a program that raises a score by 1 without using a global variable. The value goes in as a parameter, comes back with return, and the main program stores it. Fill the gaps.

  1. def increase_score(score, answer):The score comes in as a parameter. (answer is passed in too, but this simple version only adds 1.)
  2. missing step
Which line is step 2?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

One name, two different variables - and the one word that makes them the same.

What you need to know

  • The scope of a variable is where in a program it can be accessed and modified: either global scope or local scope.
  • A local variable only exists in the part of the program where it is declared, so the main program cannot access one made inside a subprogram.
  • A global variable can be accessed and modified from anywhere in the program, even inside subprograms.
  • Have a goMaya says: "A local variable is just a global variable that got shy." Using what you have read so far, correct her in one sentence.

    A local variable can only be reached inside its subprogram; a global one can be reached from anywhere in the program.

    The difference is where the variable can be accessed (its scope), not shyness, size or how it is stored.

  • In Python, assigning to a name inside a subprogram makes a new local variable, unless you declare it global.
  • Have a goThe main program sets level = 1. A subprogram with no global line then runs level = 9. Which variable did that line change: the main program's level, or a new one?

    A new local level. The main program's level is still 1.

    Assigning to a name inside a subprogram makes a new local variable unless it is declared global.

  • A local variable can share its name with a global one, but they are two different variables.
  • Writing global number inside the subprogram makes that line update the global variable instead.
  • Have a goSame program as before, but now the subprogram starts with global level before level = 9. After it runs, does the main program's level still hold 1?

    No. It now holds 9.

    The global declaration makes that line update the global variable instead of creating a local one.

  • Global variables are generally bad practice: their value is hard to track, and functions using them are harder to test alone.
  • Instead, pass values in as parameters, return the result, and store it in the main program.

The big picture

A variable's scope is the part of a program where you can reach it. Local variables live inside one subprogram, globals can be reached anywhere, and in Python an assignment inside a subprogram makes a new local variable unless you say global.

Key points

1Scope = the part of a program where a variable can be reached. Every variable is either global or local.
2Local: reachable only inside the subprogram that makes it. The main program cannot use it - in Python that causes an error.
3Global: reachable anywhere, even inside subprograms, for as long as the program runs.
4Python rule: assign to a name inside a subprogram and you get a local variable, unless the subprogram says global first.
5Better habit: parameter in, return out, store the result in the main program.

Worked example

Problem

Work out which variables are global and which are local, then say what this program outputs. level = 3 def show(): bonus = level * 2 print(bonus) show() print(level)

⚠ Watch out

Thinking a variable with the same name is the same variable everywhere. Inside a subprogram, an assignment like number = 10 makes a new local variable unless it is declared global, so the main program's number is not touched.

🧠

Memory hook

Same name, two variables. Inside a subprogram, assigning makes a new local one - unless you write global first.

✓

Check yourself

The main program sets lives = 3. A subprogram with no global line runs lives = 1. What does the main program's lives hold afterwards, and which word would change that?

Flashcards

(13)
What is the scope of a variable?
The section of the program where the variable can be accessed and modified. It is either global scope or local scope.
What is a local variable?
A variable that is defined and visible only in a specific part of a program, such as inside a subprogram. It can only be accessed and modified in the block of code where it is declared.
What is a global variable?
A variable that can be accessed and modified from anywhere in the program, even from inside subprograms, for the duration of the program's execution.
Python: a variable is assigned inside a subprogram. Local or global?
Local, unless it is declared global.
What does writing global number inside a subprogram do?
It makes assignments to number inside the subprogram update the global variable, instead of creating a new local one.
The main program sets number = 5. A subprogram (no global line) sets number = 10. What does the main program then print, and why?
5. The subprogram's number is a new local variable, so the global number keeps its value.
Same program, but the subprogram starts with global number. What is printed inside and outside?
10 in both places, because that line updated the global variable.
Can the main program print a variable that exists only inside a function?
No. The main program cannot access it, and in Python trying to causes an error.
If a local and a global variable share a name, are they the same variable?
No. They are two different variables, so changing one leaves the other alone.
Give two reasons global variables are generally seen as bad practice.
Their value can be hard to track if other areas of the program modify it, and a function that accesses them is harder to test in isolation.
What does it mean to test a function "in isolation"?
Testing it on its own, without other parts of the program.
What can you use instead of a global variable to share a value with a subprogram?
Pass the value in as a parameter, pass the result back with return, and store it in the main program.
A subprogram is called as increase_score(score). Why might the main program's score not change?
The returned value was not stored. The change only reaches the main program with score = increase_score(score, ...).

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 21 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.