✦ Python Foundations Functions module

Concept 18 · Variable Scope

Know where a name lives.
Inside and outside differ.

A function can use parameters and local names without replacing a same-named variable outside the function.

01 Learn02 Try03 Predict

A local variable belongs to the function where it is created.

A function can use parameters and local names without replacing a same-named variable outside the function.

message = "Outside"

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

show_message()
print(message)

The inner message is local to show_message, so the outer message still has its original value.

Change one value or function call, then explain how the result changes.

↳

Keep this in mind: Read the function definition and its calls before predicting the result.

Try it
message = "Outside"

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

show_message()
print(message)

Run the example, then change one value and observe the result.

Python Playground

Loading Python runtime…

Edit the example and run it in your browser.

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

Ask Sloby about this concept

Describe what feels unclear and Sloby will guide your reasoning.

Hi! I'm Sloby. Let's explore this Python concept together.

Why can label be used inside make_label but not automatically outside the function?

def make_label():
    label = "local"
    print(label)

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