Lazo Lab Sign up

Python Fundamentals / Functions and errors

Lesson 32 of 38

Scope

Variables made inside a function only exist inside it (local scope). Pass values in as parameters and return results instead of relying on globals.

Key points

  • Local variables vanish after the call
  • Avoid changing globals
  • Pass in, return out
def f():
    secret = 42
f()
# print(secret) would fail: NameError
Watch a video on thisOpens YouTube search results for “Scope” in a new tab

Quiz · +10 XP

Can you use a function's local variable outside the function?

Log in to save progress and earn XP.