Junior Codes
Inventor Studio

Local and Global Scope in Python for Kids

Understand why variables inside a function stay inside it.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Local scope, explained simply

A variable created inside a function is local: it only exists while that function is running, and the rest of the program cannot see it. Scope is the name for where a variable can be seen.

This is a good thing. It means two functions can both use a variable called flower without getting in each other’s way, just as two families can each have a child called Sam.

If you need a value from inside a function, return it. That keeps functions tidy and independent.

Example: Two gardens

flower = "rose"

def north_garden():
    flower = "tulip"
    print("North:", flower)

north_garden()
print("Outside:", flower)

Output

North: tulip
Outside: rose

Try this

  • Create a variable only inside a function, then try to print it outside. Read the NameError.
  • Write a second garden function with its own flower and call both.
  • Change the function to return its flower and store the result outside.

Practise it in Wonderwild

“Two gardens, two names”

Mission 27 in Inventor Studio, one of 36 Python missions in Wonderwild.

Your child writes and runs real Python in the browser while Pip reacts to every line. Nothing to install. For ages 7+.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Questions parents and kids ask

What is a global variable?+

One created outside every function, at the top level of your program. Functions can read it, but creating a variable with the same name inside a function makes a separate local one.

Should kids use the global keyword?+

Rarely. Passing values in with parameters and getting them back with return is clearer and leads to fewer bugs.

Prefer learning with a teacher?

Our live Python for Young Developers course teaches these ideas in small online classes led by a software engineer, with projects and feedback. Every live course also includes 12 months of full Wonderwild access for practice.

See the Python for Young Developers course