Great job! You’ve learned some important concepts in Python regarding scope.

In this lesson, we’ve covered:

  • The concept of scope and the LEGB rule.
  • What the local scope is.
  • What a nested function is and the enclosing/nonlocal scope.
  • What the global scope is.
  • How to modify behavior using the global statement.
  • How to modify behavior using the nonlocal statement.

Knowing these concepts allows for a stronger mastery of Python when working with names in programs and taking into consideration what parts of the programs they can be accessed or modified.

Keep up the great work!



The code in the workspace declares function1 which has a nested function function2. Within each function body variables are declared and the global and nonlocal keywords are used.

Inside the function2 body the globals() and locals() namespaces are printed.

Before you run the code, can you guess which namespace each variable will be in?

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?