Recall & Review
beginner
What is global scope in Python?
Global scope means a variable is defined outside any function and can be accessed anywhere in the program.
Click to reveal answer
beginner
Where can a variable declared in the global scope be used?
It can be used inside functions, outside functions, and in any part of the program after its declaration.
Click to reveal answer
intermediate
What keyword do you use inside a function to modify a global variable?
You use the
global keyword to tell Python you want to change the global variable inside the function.Click to reveal answer
intermediate
What happens if you assign a value to a variable inside a function without using
global?Python treats it as a new local variable, and the global variable remains unchanged.
Click to reveal answer
beginner
Example: What will this code print?
count = 5
def add():
global count
count += 1
add()
print(count)It will print
6 because the function modifies the global variable count using the global keyword.Click to reveal answer
What does the
global keyword do inside a function?✗ Incorrect
The
global keyword tells Python to use the variable from the global scope, allowing modification.If a variable is defined outside any function, what is its scope?
✗ Incorrect
Variables defined outside functions have global scope and can be accessed anywhere in the program.
What will happen if you assign a value to a variable inside a function without declaring it global?
✗ Incorrect
Without
global, assignment inside a function creates a new local variable.Can a global variable be accessed inside a function without the
global keyword?✗ Incorrect
You can read a global variable inside a function without
global, but to modify it you need the keyword.Which of these is true about global variables?
✗ Incorrect
Global variables exist throughout the program and can be accessed anywhere after they are declared.
Explain what global scope means and how it affects variable access in Python.
Think about where the variable is defined and where you can use it.
You got /3 concepts.
Describe how to modify a global variable inside a function and why the
global keyword is needed.Consider what Python does by default when you assign a variable inside a function.
You got /3 concepts.