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
Using the Global Keyword in Python
๐ Scenario: Imagine you are creating a simple program to count how many times a button is clicked. The count needs to be updated inside a function but stored outside so it can be used anywhere in the program.
๐ฏ Goal: You will build a program that uses a global variable to keep track of the count and updates it inside a function using the global keyword.
๐ What You'll Learn
Create a global variable called click_count with initial value 0
Create a function called increment_click that increases click_count by 1
Use the global keyword inside the function to modify the global variable
Print the value of click_count after calling the function twice
๐ก Why This Matters
๐ Real World
Global variables are useful when you want to keep track of information that many parts of your program need to access and update, like scores in a game or settings in an app.
๐ผ Career
Understanding how to use global variables and the <code>global</code> keyword helps you manage data across different parts of your code, a skill important in many programming jobs.
Progress0 / 4 steps
1
Create the global variable
Create a global variable called click_count and set it to 0.
Python
Hint
Think of click_count as a box outside any function that holds the number 0.
2
Define the function to update the count
Define a function called increment_click that uses the global keyword to access click_count and adds 1 to it.
Python
Hint
Inside the function, tell Python you want to use the global click_count by writing global click_count.
3
Call the function twice
Call the function increment_click two times to increase the count.
Python
Hint
Just write increment_click() two times, one after the other.
4
Print the final count
Print the value of click_count to show how many times the function was called.
Python
Hint
Use print(click_count) to show the number 2 on the screen.
Practice
(1/5)
1.
What does the global keyword do in Python?
easy
A. It creates a new local variable inside a function.
B. It allows a function to modify a variable defined outside it.
C. It deletes a variable from the global scope.
D. It imports a module globally.
Solution
Step 1: Understand variable scopes
Variables defined outside functions are global, inside functions are local by default.
Step 2: Role of global keyword
The global keyword tells Python to use the global variable inside the function, allowing modification.
Final Answer:
It allows a function to modify a variable defined outside it. -> Option B
Quick Check:
global lets function change outside variable [OK]
Hint: Global lets functions change outside variables directly [OK]
Common Mistakes:
Thinking global creates new local variables
Confusing global with import statements
Assuming global deletes variables
2.
Which of the following is the correct way to modify a global variable count inside a function?
count = 0
def increment():
?
count += 1
easy
A. def count
B. local count
C. global count
D. nonlocal count
Solution
Step 1: Identify the need to modify global variable
The function wants to increase the global variable count.
Step 2: Use correct keyword to access global variable
Using global count inside the function tells Python to use the global count, not create a local one.
Final Answer:
global count -> Option C
Quick Check:
Use 'global' to modify global variables inside functions [OK]
Hint: Use 'global' before variable to modify it inside function [OK]
Common Mistakes:
Using 'local' which is not a Python keyword
Using 'nonlocal' which applies to enclosing functions, not globals
Trying to define variable with 'def'
3.
What is the output of this code?
value = 5
def change():
global value
value = 10
change()
print(value)
medium
A. 5
B. Error
C. None
D. 10
Solution
Step 1: Analyze the function change()
The function declares value as global and sets it to 10.
Step 2: Effect of calling change()
Calling change() updates the global value from 5 to 10.
Final Answer:
10 -> Option D
Quick Check:
global lets function update outside variable [OK]
Hint: global changes outside variable, so print shows updated value [OK]