0
0
Pythonprogramming~5 mins

Global scope in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AMakes a variable private
BCreates a new local variable
CDeletes a global variable
DAllows modifying a variable defined outside the function
If a variable is defined outside any function, what is its scope?
AGlobal scope
BLocal scope
CBlock scope
DFunction scope
What will happen if you assign a value to a variable inside a function without declaring it global?
AIt creates a new local variable
BIt deletes the global variable
CIt causes a syntax error
DIt changes the global variable
Can a global variable be accessed inside a function without the global keyword?
ANo, it cannot be accessed
BYes, but it cannot be modified
CYes, and it can be modified
DNo, it causes an error
Which of these is true about global variables?
AThey are only accessible inside functions
BThey are created using the <code>local</code> keyword
CThey exist throughout the program after declaration
DThey cannot be changed once set
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.