A global variable is declared outside all functions and is accessible throughout the entire program; a local variable is declared inside a function and exists only for the duration of that function call. Understanding scope — which variable name is visible where — prevents some of the most frustrating bugs in programming.

What is the difference between a global and a local variable?

The simplest way to see the difference is to look at where the variable is created:

Feature Global variable Local variable
Declared Outside all functions, at the top level of the program Inside a function body
Visible from Anywhere in the program Only within the function where it is created
Lifetime Exists for the whole run of the program Created when the function is called; destroyed when the function returns
Risk Can be accidentally changed from many places Safer — nothing outside the function can touch it

Think of a global variable as a notice board in a school corridor — every classroom (function) can read it. A local variable is a sticky note on your own desk — nobody else can see it when they walk into the room.

What is scope in programming?

Scope is the region of code where a variable name is defined and can be used. If you try to use a variable outside its scope, you will get a NameError in Python.

There are two main scopes in most programming languages:

  • Global scope — the top-level area outside any function. Variables declared here are globally scoped.
  • Local scope — the inside of a function. Variables declared here are locally scoped to that function.

Scope rules prevent one part of a large program accidentally overwriting a variable that another part depends on. Without scope, writing any program longer than a few lines would be extremely error-prone.

How do global and local variables behave in Python?

# Global variable — created outside all functions
score = 0

def add_points(points):
    # Local variable — only visible inside this function
    bonus = 10
    total = points + bonus
    return total

print(score)     # Works: score is global
print(bonus)     # ERROR: NameError — bonus is local to add_points

When add_points is called, Python creates the local variables bonus and total and places them on the call stack. When the function returns, those variables are deleted. The global score is unaffected.

What happens when a local variable shares a name with a global one?

Python prioritises the local variable. The global one is not touched:

colour = "red"   # global

def paint():
    colour = "blue"  # local — a completely separate variable
    print(colour)    # prints "blue"

paint()
print(colour)        # prints "red" — global unchanged

This is called variable shadowing — the local colour shadows (hides) the global colour inside the function. This is one of the most common causes of subtle bugs: a programmer thinks they have changed the global, but they have only created a new local variable with the same name.

How do you modify a global variable from inside a function?

Python requires an explicit global declaration before you can write to a global variable from inside a function:

lives = 3   # global

def lose_life():
    global lives      # tells Python: "lives" refers to the global
    lives = lives - 1

lose_life()
print(lives)   # prints 2

Without the global keyword, the assignment lives = lives - 1 would either create a new local variable or throw an UnboundLocalError. The global keyword is the programmer's explicit statement: "I know what I am doing — modify the global."

Why do programmers generally prefer local variables?

Local variables make programs much easier to reason about and test. When a function uses only local variables and its parameters, its behaviour depends entirely on what is passed in — not on hidden state elsewhere. This quality is called referential transparency.

Situation Recommendation
Value only needed inside one function Local variable
Value shared and read by many functions Global variable (or pass as parameter)
Value that multiple functions must update Strongly prefer passing as a parameter and returning the result
Configuration constants (e.g. MAX_ATTEMPTS = 3) Global constant (convention: UPPER_CASE name)

Professional programmers treat global variables (as opposed to constants) with caution. Every function that can modify a global creates a hidden dependency that makes the program harder to debug.

What is a common bug caused by scope confusion?

A classic beginner mistake is forgetting that a function cannot see variables defined in another function:

def calculate():
    result = 42

def display():
    print(result)   # NameError! result is local to calculate()

calculate()
display()

The fix is either to make result global (not ideal), return it from calculate and pass it to display, or combine the logic. GCSE exam questions often ask you to trace such bugs or explain why a NameError occurs.

Frequently asked questions

Can a function read a global variable without the global keyword?

Yes — a function can read a global variable without declaring it. The global keyword is only required when you want to write (assign) to the global variable from inside the function. Attempting to both read and then assign without global will raise an UnboundLocalError because Python sees the assignment and treats the name as local throughout the whole function body.

What is the difference between a global variable and a global constant?

Both are declared at the top level (global scope). The difference is intent and convention: a constant is never reassigned after its initial value is set, and its name is written in UPPER_CASE by convention (e.g. MAX_SCORE = 100). A global variable is expected to change. In Python there is no language-level enforcement of constants — UPPER_CASE is purely a convention that tells other programmers "do not change this".

Is it always bad practice to use global variables?

Not always. Short scripts, single-function programs, and constants used across an entire program are fine uses of the global scope. The problem arises when global mutable variables are shared across many functions in a large program, making the flow of data hard to follow. At KS3 and GCSE level, you will be expected to know the difference and understand why local variables are generally safer, but simple programs with one or two globals are perfectly acceptable.

How does scope work in pseudocode at GCSE?

Most GCSE pseudocode conventions (AQA, OCR) assume a variable declared inside a subroutine is local to it, and a variable declared outside any subroutine is global. Exam questions may ask you to identify whether a variable is in scope, or to correct code that references a variable outside its scope. The principle is identical to Python — local variables exist only within the subroutine that declares them.


Explore scope, functions, and every other GCSE programming concept with personalised Socratic guidance at aitutors.me.