Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to dive into an important topic: the scope and lifetime of variables. Can someone tell me what they think a local variable might be?
I think a local variable is one that you can only use inside a function.
Yeah, it's confined to that function, right?
Exactly! Local variables are declared inside a function and cannot be accessed outside. For instance, if I define a variable x within a function, it won’t be available outside. Let's take a look at this example.
"Example:
Now that we understand local and global variables, let's talk about modifying global variables. What do you think happens if I want to change the value of a global variable inside a function?
I guess it would just throw an error since it’s not defined inside the function.
Yeah, I’ve experienced that before. So, how do we avoid that?
"Good point! To modify a global variable, we use the global keyword. Let me show you how it works.
Local's a function's domain, global holds its reign.
Once in the land of Pythonia, the local variable declared its home within a castle named 'Function,' while the global variable roamed freely across the kingdom, always reachable and ready to help!
Think 'L' for Local (at Function's door) and 'G' for Global (wandering all over).
L.A.G.: Local (Accessible) only inside Function, Global (Accessible) everywhere!
Term
What is a local variable?
Click to flip
Term
What is a global variable?
Click to flip
Term
What is the purpose of the global keyword?
Click to flip
A variable declared inside a function that is accessible only within that function.
A variable declared outside of all functions, accessible throughout the entire module.
A keyword used inside a function to declare that a variable refers to a global variable.
This section covers the concepts of variable scope and lifetime in Python, explaining the differences between local and global variables, as well as using the global keyword.
In this section, we explore the scope and lifetime of variables in Python, distinguishing between local and global variables. It details how local variables are confined to functions while global variables are accessible throughout the program. Additionally, the use of the global keyword to modify global variables inside functions is explained, providing clarity on variable accessibility.
In Python, the scope and lifetime of variables are crucial concepts that dictate where a variable can be accessed and how long it persists in memory. The section is divided into two main subtopics:
x = 10 # Global
def show():
x = 5 # Local
print(x)
show() # Output: 5
print(x) # Output: 10The global keyword is employed inside a function when it is necessary to modify a global variable. For example:
x = 10
def modify():
global x
x = 20
modify()
print(x) # Output: 20This keyword tells Python that we want to refer to the global variable x, thereby allowing us to update its value.
Understanding these concepts is essential for managing variable data effectively in Python programming, enabling better control of data throughout the application.