PY-101 ยท Functions
Scope
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Scope is where a variable can be seen. Variables created inside a function are local: they exist only while the function runs. Variables created outside are global; functions can read them, but assigning to the name inside a function creates a new local variable instead.
- Tell local from global variables
- Know why a function cannot see another function's variables
- Prefer return over global
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
bonus = 100def salary(base):total = base + bonusreturn totalpay = salary(5000)print("Pay:", pay)print("Bonus is still", bonus)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Pay: 5100
Bonus is still 100 ๐ฏ Fix the code so score becomes 15. Make add_points return the new score and assign it back, without using the global keyword.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
def add_points(current, points): return current + points, then score = add_points(score, 10)
- The problemTwo functions both use a variable called total. If they shared it, one would wreck the other's maths. Scope keeps each function's workings private.
- The Python idea
x = 1 # globalScope is where a variable can be seen. - In real lifeLarge codebases avoid global variables because any function could change them, making bugs very hard to trace.
Start with a question
Two functions both use a variable called total. If they shared it, one would wreck the other's maths. Scope keeps each function's workings private.
Most confusing beginner bugs with functions are scope bugs. Understanding it makes functions predictable.
Explain it like I'm 10
Each function has its own desk. What it writes on its desk stays there. Everyone can read the noticeboard on the wall (globals), but writing on your desk does not change the noticeboard.
The technical explanation
A variable assigned inside a function is local. It disappears when the function returns.
A function can read a global variable, but x = ... inside the function makes a new local x.
The global keyword lets a function change a global, but it makes code harder to follow. Returning values is cleaner.
Syntax
x = 1 # global
def f():
y = 2 # local
return x + y Real-world scenario
Large codebases avoid global variables because any function could change them, making bugs very hard to trace.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Lambda functions โQuestions people ask
What is the LEGB rule in Python?
The order Python looks for a name: Local, Enclosing function, Global, Built-in. The first place that has the name wins.
Why is using global bad?
Any function could change the value, so bugs are hard to trace. Passing values in and returning them out keeps functions independent and testable.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is the LEGB rule in Python?
The order Python looks for a name: Local, Enclosing function, Global, Built-in. The first place that has the name wins.
beginner Why is using global bad?
Any function could change the value, so bugs are hard to trace. Passing values in and returning them out keeps functions independent and testable.
intermediate Where does a local variable exist?
Only inside its function. Locals live inside the function call.
intermediate Can a function read a global variable?
Yes. Reading works without any keyword.
Quick revision
Remember
- Tell local from global variables
- Know why a function cannot see another function's variables
- Prefer return over global
- If you need a function to change a value, return the new value and assign it outside:
score = add_points(score, 10). Avoid the global keyword.
Syntax
x = 1 # global
def f():
y = 2 # local
return x + y Official documentation
- Scopes and namespaces (docs.python.org)