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: roseTry 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.
