Scope Basics
Local vs Global
LEGB and Namespace
100

Define the scope of a variable in one sentence.

The scope of a variable defines where it can be accessed and how long it exists

100

True or False: A local variable defined inside a function can be accessed directly by other functions. Explain your answer in one sentence.

 False

100

What does the LEGB rule stand for in Python? Choose the correct expansion:
A. Local → Environment → Global → Built-in
B. Local → Enclosing → Global → Built-in
C. Local → Enclosing → Global → Base
D. Local → External → Global → Built-in

B

200

List the two typical kinds of scope mentioned in the lesson

Local and global

200

What Python keyword allows a function to modify a global variable declared outside the function?

global

200

Name the three main categories of namespaces described in the lesson

Built-in, global, local

300

Explain why understanding variable scope helps prevent program errors (one short paragraph)

Prevents unintended changes and accessing undefined variables; ensures correct data interactions

300

Consider a global variable named countcount and a function that defines a local variable also named countcount. Which variable will be used inside the function by default? Explain briefly.

The local variable shadows the global variable inside the function.

300

In which namespace does Python search first when resolving a variable name? What comes next, and then last?

Local, then global, then built-in

400

Describe what happens to a variable in a function when the function finishes execution

Local variables are destroyed (their local namespace is deleted) when the function returns

400

Rewrite the behavior: If you want a function to update the global yearyear variable by adding 1, which statement must you include inside the function? (Answer with the exact keyword and a short explanation.)

include the line: global year — this tells Python you mean the global variable.

400

Identify built-in names from this short list: print, school_name, input, student_nameprint, school_name, input, student_name — which are built-in?

Built-in names: print, input

500

Give one real-world analogy (2–3 sentences) that explains the difference between local and global variables

Local variables are like a backpack you carry into a room — only items inside the backpack are available while you're inside; global variables are like items on a shared table available to everyone

500

Provide a short code snippet (3–6 lines) that demonstrates creating a global variable and modifying it inside a function using the correct keyword. (Students should be able to write this in class.)

def add_one():
  global global_var
  global_var = global_var + 1

500

 Explain what a namespace is and why it avoids naming conflicts (2–3 sentences).

A namespace maps names to objects; it separates name contexts so the same name can exist in different namespaces without conflict.