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 will discuss global variables. Can anyone tell me what a global variable is?
Isn't it a variable that can be accessed from anywhere in the program?
Exactly! Global variables are defined outside of functions and can be accessed from any point in the code. For example, if I say x = 10 outside any function, I can use print(x) inside any function to get 10. Great memory aid to remember this: the letter G for Global reminds us that it's 'General' and can be accessed anywhere!
Got it! So, can we modify a global variable inside a function?
Good question! We will cover that shortly, but you have to use the global keyword to modify it inside a function.
What happens if you try to use a global variable that wasn't defined yet?
If you attempt to access a global variable before defining it, you will face a NameError. Remember that variables must be defined before use!
In summary, global variables are those defined in the general scope, and their accessibility is extensive throughout the program.
Now, let’s talk about local variables. Who can tell me what they are?
Are they the ones you define inside functions only?
Exactly! Local variables are created when a function is called and are destroyed once the function exits. For example, def func(): x = 5 will make x local to func(). You can’t access x outside that function.
So, what happens if I try to print x outside the function?
You will receive an error because x is not defined in the global scope. So always remember: Local is Localized!
To summarize, local variables only exist within the function scope and cannot be accessed elsewhere.
Let's discuss the global and nonlocal keywords. Why do you think they're important?
Are they used to control access to variables across functions and scopes?
Perfect! The global keyword tells Python to use the global variable instead of creating a new one. For example, if I have global x in my function, it modifies the global x.
What about nonlocal?
Great that you asked! If you have nested functions, nonlocal allows you to modify a variable from the enclosing function without declaring it global. So, just remember: Global for globals, Nonlocal for neighbors!
Can you give an example of nonlocal in action?
Certainly! If you have a nested function, you can modify the variable of the outer function using nonlocal! That way, we respect the scope rules while allowing flexibility. To recap, global changes globals, and nonlocal deals with outer function scopes.
Defined outside any function; accessible everywhere in the code.
Defined inside functions; restricted to that function's scope.
Modifies a global variable within a function.
Accesses and modifies a variable in the outer (non-global) scope.
Global variables are everywhere, Local variables are confined, keep the scopes clear, and you'll do just fine!
Imagine a town (global scope) where everyone can see the town square (global variable) but there are private rooms (local variables) in each house that no one else can enter.
Remember G for Global and L for Local; G is for General access, L is for Limited access.
G-L stands for Global-Local, distinguishing where you can access variables.
Term
Global Variable
Click to flip
Term
Local Variable
Click to flip
A variable that is defined outside of any function and accessible throughout the program.
A variable defined within a function, only accessible within that function's scope.
A keyword used in a function to refer to a global variable.
A keyword used in nested functions to refer to variables in the enclosing scopes.
This section explains global and local variables in Python, highlighting their scopes and uses within functions.
In this section, we explore the distinction between global and local variables in Python, how they are declared, and the implications of their scopes in programming. The use of global and nonlocal keywords to modify variables across different scopes is also discussed.
In programming, the concept of variable scope is crucial as it determines where and how variables can be accessed within the code. Specifically, local variables are defined within a function and are only accessible inside it, while global variables are defined outside of any function and can be accessed throughout the entire program.
x = 10, which is accessible in any function as shown in the example where print(x) will output 10 when called from the display() function.x = 5 in func(), which cannot be accessed outside that function, leading to a potential error if trying to print x elsewhere.global and nonlocal: The global keyword allows modification of global variables from within a function, while nonlocal is used in nested functions to reference variables from an enclosing function.Understanding these concepts is essential for managing variable visibility and preventing errors in Python programming.