Python Variable Scope Quiz

  • Questions: 15
  • Easy
  • Pass mark: 70%

Scope decides where a variable can be seen and changed: a counter that starts again on every call, a fuel level shared by the whole program, a mission status kept inside a function within another function. This free quiz has 15 short questions on how Python finds a name, when a variable is local or global, and what the global and nonlocal keywords change.

What the quiz covers

  • Local variables: created on every call and gone when the function ends
  • Reading and changing global variables, and the global keyword
  • Why a list can be changed without global, and where UnboundLocalError comes from
  • Blocks such as if and for, which have no scope of their own, and list comprehensions, which do
  • Nested functions, the nonlocal keyword and the order in which Python looks up a name

How it works

  • 15 questions: 11 with one correct answer, 4 where you select every correct answer
  • After each question you see the correct answer and a short explanation
  • No time limit and no account needed
  • Score 70% or more (11 of 15) to pass

You can try each idea in a runnable example: local and global variables, the global keyword and scope in nested functions. After this one, try the Python Functions Quiz or the Python *args and **kwargs Quiz.