Bird
Raised Fist0
Pythonprogramming~20 mins

Global scope in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Global Scope Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of modifying a global variable inside a function
What is the output of this Python code?
Python
x = 10

def modify():
    global x
    x = 20

modify()
print(x)
A20
B10
CNameError
DNone
Attempts:
2 left
๐Ÿ’ก Hint
Think about how the 'global' keyword affects variable assignment inside functions.
โ“ Predict Output
intermediate
2:00remaining
Output when modifying a variable without global keyword
What will this code print?
Python
count = 5

def increment():
    count = count + 1

increment()
print(count)
AUnboundLocalError
B5
C6
DNameError
Attempts:
2 left
๐Ÿ’ก Hint
Consider what happens when you assign to a variable inside a function without declaring it global.
๐Ÿ”ง Debug
advanced
2:00remaining
Fix the code to update global list inside function
This code tries to add an item to a global list inside a function. Which option correctly fixes it?
Python
items = [1, 2, 3]

def add_item():
    items = items + [4]

add_item()
print(items)
AChange 'items = items + [4]' to 'items.append(4)' without global keyword.
BChange 'items = items + [4]' to 'items += [4]' without global keyword.
CRemove the function and add 4 directly to the list outside.
DAdd 'global items' inside the function before modifying it.
Attempts:
2 left
๐Ÿ’ก Hint
Remember that assigning to a variable inside a function creates a local variable unless declared global.
โ“ Predict Output
advanced
2:00remaining
Output of nested function modifying global variable
What does this code print?
Python
value = 1

def outer():
    def inner():
        global value
        value = 5
    inner()
    print(value)

outer()
print(value)
A1\n5
B1\n1
C5\n5
D5\n1
Attempts:
2 left
๐Ÿ’ก Hint
The inner function uses 'global' to change the variable. Think about when the print statements run.
๐Ÿง  Conceptual
expert
2:00remaining
Understanding global variable behavior with immutable types
Given this code, what is the value of 'num' after calling 'change()'?
Python
num = 10

def change():
    global num
    num += 5

change()
A10
B15
CTypeError
DUnboundLocalError
Attempts:
2 left
๐Ÿ’ก Hint
The '+=' operator modifies the global variable when declared global.

Practice

(1/5)
1. What does the global keyword do inside a Python function?
easy
A. It deletes a global variable.
B. It creates a new local variable inside the function.
C. It makes the function run faster.
D. It allows the function to modify a variable defined outside the function.

Solution

  1. Step 1: Understand the role of global keyword

    The global keyword tells Python that the variable inside the function refers to the variable defined outside (in global scope).
  2. Step 2: Effect on variable modification

    Without global, assigning a value creates a new local variable. With global, it modifies the existing global variable.
  3. Final Answer:

    It allows the function to modify a variable defined outside the function. -> Option D
  4. Quick Check:

    global keyword modifies outer variable [OK]
Hint: global lets functions change outside variables [OK]
Common Mistakes:
  • Thinking global creates a new local variable
  • Assuming global deletes variables
  • Believing global affects performance
2. Which of the following is the correct way to declare a global variable inside a function?
easy
A. def global x:
B. global = x
C. global x
D. x global

Solution

  1. Step 1: Recall correct syntax for global declaration

    The correct syntax is to write the keyword global followed by the variable name, separated by a space.
  2. Step 2: Check each option

    global x matches the correct syntax. Others are invalid Python syntax.
  3. Final Answer:

    global x -> Option C
  4. Quick Check:

    global keyword followed by variable name [OK]
Hint: Use 'global' then variable name to declare global inside function [OK]
Common Mistakes:
  • Using '=' with global keyword
  • Trying to define global like a function
  • Placing global after variable name
3. What will be the output of this code?
count = 5
def increment():
    global count
    count += 1
increment()
print(count)
medium
A. 6
B. None
C. Error
D. 5

Solution

  1. Step 1: Understand the global variable usage

    The variable count is defined globally with value 5. Inside increment(), global count allows modifying this global variable.
  2. Step 2: Trace the function call and print

    The function adds 1 to count, changing it from 5 to 6. Then print(count) outputs 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    global lets function change count to 6 [OK]
Hint: global lets function update outside variable [OK]
Common Mistakes:
  • Expecting original value 5 to print
  • Thinking global causes error here
  • Assuming function returns None
4. Find the error in this code:
total = 10
def add():
    total += 5
add()
print(total)
medium
A. Missing global declaration inside add()
B. Syntax error in function definition
C. total is not defined globally
D. print statement is incorrect

Solution

  1. Step 1: Identify variable scope issue

    Inside add(), total += 5 tries to modify total. Without global total, Python treats total as local but it's used before assignment, causing an error.
  2. Step 2: Fix by adding global declaration

    Adding global total inside add() tells Python to use the global total variable, fixing the error.
  3. Final Answer:

    Missing global declaration inside add() -> Option A
  4. Quick Check:

    Modify global variable needs global keyword [OK]
Hint: Add global keyword to modify global variable inside function [OK]
Common Mistakes:
  • Ignoring need for global keyword
  • Thinking total is local automatically
  • Assuming no error occurs
5. You want to count how many times a function is called using a global variable. Which code correctly updates the global counter?
hard
A. calls = 0 def func(): calls += 1 func() print(calls)
B. calls = 0 def func(): global calls calls += 1 func() print(calls)
C. calls = 0 def func(): calls = calls + 1 func() print(calls)
D. calls = 0 def func(): global calls calls = calls func() print(calls)

Solution

  1. Step 1: Understand global variable modification

    To update a global variable inside a function, you must declare it with global inside the function.
  2. Step 2: Analyze each option

    calls = 0 def func(): calls += 1 func() print(calls) tries to increment without global, causing error. calls = 0 def func(): global calls calls += 1 func() print(calls) correctly uses global calls and increments. calls = 0 def func(): calls = calls + 1 func() print(calls) tries to read and assign without global, causing UnboundLocalError. calls = 0 def func(): global calls calls = calls func() print(calls) declares global but does not increment.
  3. Final Answer:

    calls = 0 def func(): global calls calls += 1 func() print(calls) -> Option B
  4. Quick Check:

    global keyword needed to update global variable [OK]
Hint: Use global keyword to update global counter inside function [OK]
Common Mistakes:
  • Forgetting global keyword causes UnboundLocalError
  • Assigning without global causes UnboundLocalError when reading variable
  • Declaring global but not updating variable