Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Listen to a student-teacher conversation explaining the topic in a relatable way.
Today, class, we're discussing global variables. Can anyone tell me what a global variable is?
Isn't a global variable a variable defined outside of functions that can be used anywhere in the code?
Exactly! Global variables are accessible throughout your code, but if we want to change them inside a function, we need to use the `global` keyword. Who can explain what happens without it?
If we don't use `global`, the function will think we're trying to create a new local variable.
Correct! And remember, the acronym 'GLOBE' can help us remember that Global variables can be accessed Locally only with the `global` keyword and that they are dynamic objects.
So, how do we use the `global` keyword in our functions?
Let’s look at a simple example to clarify this.
Here’s a quick example. Let's say we have a variable `x` set to 10. If we want to change it inside a function, we would write: `global x; x = 20`. Can anyone write a small program using this concept?
"Sure! I’ll write:
Now let’s talk about the potential pitfalls of using global variables. Why do you think they might be risky?
They can make the code harder to debug, right?
Yes! They can introduce side effects that are hard to track. Can someone give an example?
If multiple functions modify the same global variable, it can lead to unexpected outcomes depending on the order they’re called.
Exactly! As a rule of thumb, limit global variable usage to situations where absolutely necessary. Always ask if there's a way to pass variables as parameters instead. Let’s reinforce our learning with a quick recap.
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
The global
keyword enables functions to modify variables that are defined outside their scope, allowing the functions to access and update global variables. Understanding this concept is crucial for managing variable scope effectively.
In Python, scopes determine the accessibility of variables throughout the program. Aside from local variables that are accessible only within their function, Python also allows the use of global variables, which can be accessed from anywhere, including inside functions. When we want to modify a global variable within a function, we need to use the global
keyword.
global
Keyword: When you declare a variable as global inside a function using the global
keyword, you tell Python that you want to reference the globally scoped variable instead of creating a new local variable. This allows you to perform operations on the global variable directly.In this example, the function modify()
uses the global
keyword to modify the global variable x
, changing its value from 10 to 20. Without declaring x
as global, the function would attempt to create a local variable x
instead, leaving the global variable unchanged.
Thus, mastering the global
keyword is essential for understanding variable scope and lifetime in Python functions and for effectively modifying global state when necessary.
Dive deep into the subject with an immersive audiobook experience.
Signup and Enroll to the course for listening the Audio Book
To modify a global variable inside a function.
In Python, by default, when you try to assign a value to a variable that is defined outside of a function, Python will treat that variable as a local variable. To modify a variable that exists outside the function's scope, you need to use the global
keyword. This tells Python that you want to use the global version of the variable instead of creating a new local one inside the function.
Think of a global variable as a big box in a room (the global scope) that everyone in the room can see and use. If a child (the function) goes to the box and sees a toy (the variable), they can play with it. But if they just try to put away a toy that is already in the box, they might not be able to if they don't specify that they are using the box. Using the global
keyword is like saying, 'I'm going to put the toy back in the big box we all can use!'
Signup and Enroll to the course for listening the Audio Book
x = 10
def modify():
global x
x = 20
modify()
print(x) # Output: 20
In the provided code example, a variable x
is defined globally with a value of 10. The function modify()
uses the global
keyword to indicate that it is referring to the global variable x
, not creating a new local one. Inside the function, x
is modified to 20. When modify()
is called, it changes the value of global x
. Thus, when we print x
after calling the function, it outputs 20.
Imagine there is a community board (the global variable) where everyone can post messages. If someone wants to change a message on the board (modify the global variable), they need permission to do so (use the global
keyword). In our example, by saying global x
, we are given permission to write over the existing message, changing it from '10' to '20.'
Learn essential terms and foundational ideas that form the basis of the topic.
Key Concepts
Global Variables: Variables declared outside of all functions are global and can be accessed anywhere within the script.
Using global
Keyword: When you declare a variable as global inside a function using the global
keyword, you tell Python that you want to reference the globally scoped variable instead of creating a new local variable. This allows you to perform operations on the global variable directly.
x = 10
def modify():
global x
x = 20
modify()
print(x) # Output: 20
In this example, the function modify()
uses the global
keyword to modify the global variable x
, changing its value from 10 to 20. Without declaring x
as global, the function would attempt to create a local variable x
instead, leaving the global variable unchanged.
Thus, mastering the global
keyword is essential for understanding variable scope and lifetime in Python functions and for effectively modifying global state when necessary.
See how the concepts apply in real-world scenarios to understand their practical implications.
Example of a global variable: x = 10
that can be accessed everywhere in the script.
Function example: def modify(): global x; x = 20
changes the global variable x inside a function.
Use mnemonics, acronyms, or visual cues to help remember key information more easily.
If you want to change a global state, use global
before it's too late!
Imagine a pirate named Captain Global, who carried a treasure map (the variable). Everyone in the crew (functions) wants to find the treasure, but only if they mention Captain Global can they change the map!
Remember: G for Global, L for Local; use the Global keyword to keep things pivotal!
Review key concepts with flashcards.
Review the Definitions for terms.
Term: Global Variable
Definition:
A variable that is declared outside all functions and can be accessed and modified anywhere in the code.
Term: Local Variable
Definition:
A variable that is declared within a function and can only be accessed within that function.
Term: global Keyword
Definition:
A keyword in Python that allows a function to modify a global variable.