✦ Python Foundations Functions module

Concept 18 · Variable Scope

Know where a name lives.
Inside and outside differ.

Functions create their own working area for names. Learn where Python can see a name during a function call and where that name is no longer available.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

A name can be used only where Python can see it.

Concepts 15–17 followed a function call from definition through input and return. Now ask where the names used during that call are available. Use this beginner model: name → where it is created → where it can be used.

message = "Outside"

def show_message():
    message = "Inside"
    print(message)

show_message()
print(message)

This prints Inside, then Outside. The two spellings of message do not automatically mean one variable. The inner name belongs to show_message(); the outer name belongs to the surrounding program.

Scope answers a practical question: where can Python find this name? A name created outside a function belongs to the surrounding program. A parameter or a name created in a function body belongs to that current function call.

↳

Keep the two areas separate: OUTSIDE FUNCTION: message = "Outside". CALL show_message(): create and use the local message, then print it. END CALL: those local working names are no longer used. BACK OUTSIDE: print(message) uses the outside name.

Parameters are local names too
def greet(name):
    message = "Hello " + name
    print(message)

greet("Ana")

During this call, name refers to "Ana" and message refers to "Hello Ana". Concept 16 showed that an argument gives a value to a parameter; that parameter name is available inside this function call.

A function can read an outside name
language = "Python"

def show_language():
    print(language)

show_language()

If Python does not find a name created locally in the function, it can read a matching name from the surrounding program. Here no local language exists, so the function reads the outside value.

The same spelling inside can hide the outside name
color = "blue"

def change_color():
    color = "green"
    print(color)

change_color()
print(color)

Inside the function, color is "green". Outside it is still "blue". Creating the local color does not automatically replace the outside one; while Python works inside the function, the local name hides the outside name with the same spelling.

A local name is not automatically available outside
def make_label():
    label = "Local"
    print(label)

make_label()
# print(label) would not find this local name outside

label works while Python is inside make_label(). After the call, outside code cannot simply expect print(label) to work, because Python cannot find that local name there.

Use return to send a value outside
def make_label():
    label = "Python"
    return label

course_label = make_label()
print(course_label)

label is still local to make_label(). Concept 17 gives the useful bridge: return sends the value "Python" back, and outside code stores it in its own name, course_label. Think: local name → return value → outside name.

Each call gets its own local values
def greet(name):
    message = "Hello " + name
    print(message)

greet("Ana")
greet("Marko")

In the first call, name is "Ana" and message is "Hello Ana". In the second, they are "Marko" and "Hello Marko". Do not imagine a parameter or local variable as one permanent global box.

A practical scope-reading checklist

  1. Locate yourself in the programAre you reading outside a function or inside the current function call?
  2. Find locally created namesWas this name created in the current function body?
  3. Check for a parameterIs the name a parameter of the current function call?
  4. Then look outside for a readable nameIf no local name exists, is there an outside name Python can read?
  5. Return a value that is needed outsideWhen outside code needs a useful value, return it instead of trying to reach for a local name.

Common beginner mistakes

  • Assuming the same spelling means the same variableThe same name can belong to the surrounding program and to a function call. Check where each name was created.
  • Expecting a local variable to exist outside the functionA name created in a function body is for that function call; outside code cannot automatically use it after the call ends.
  • Thinking a local change automatically changes the outside nameAssigning a local name with the same spelling does not automatically replace the outside name.
  • Forgetting that parameters belong to the function callAn argument supplies a value for a parameter during one call. Read that parameter as a name available inside that call.
  • Trying to use a local name outside instead of returning a valueIf outside code needs a result, use return and store the returned value in an outside name.
  • Losing track of which scope a name belongs toPause at each use of a name and ask whether Python is currently working inside or outside the function.

Python Playground

Loading Python runtime…

Before running the code, predict both outputs. Then change the inside value, change the outside value, and notice which change affects each print.

variable_scope.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read the function definition and each call before choosing an answer.

color = "blue"

def change_color():
    color = "green"
    print(color)

change_color()
print(color)
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.