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, 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.
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
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:
The global
keyword is employed inside a function when it is necessary to modify a global variable. For example:
This 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.
Dive deep into the subject with an immersive audiobook experience.
Signup and Enroll to the course for listening the Audio Book
x = 10 # Global def show(): x = 5 # Local print(x) show() # Output: 5 print(x) # Output: 10
In programming, variables can have different scopes based on where they are declared. Variables declared inside a function are called local variables. They can only be accessed within that function and do not exist outside of it. On the other hand, global variables are declared outside any function and can be accessed from anywhere in the code. In the provided example, x
is assigned the value of 10 as a global variable. Inside the function show()
, a new local variable x
is created with the value of 5. When we call show()
, it prints the local x
, which is 5
. After the function call, when we print x
again outside the function, it refers to the global variable which is still 10
.
Think of global variables as a public library, where anyone can go and find information (the global variable). Local variables, however, are like private notes that someone takes for a specific meeting (local variable). Only that person can read those notes, and after the meeting is over, those notes have no value to anyone else.
Signup and Enroll to the course for listening the Audio Book
To modify a global variable inside a function.
x = 10 def modify(): global x x = 20 modify() print(x) # Output: 20
When you want to change the value of a global variable inside a function, you need to use the global
keyword. This tells Python that you mean the global variable, not a new local variable. In the example, x
starts off as 10
. Inside the function modify()
, we declare x
as a global variable using global x
. This allows us to change x
to 20
. After calling modify()
, if we print x
, it shows the updated value of 20
because we altered the global variable.
Imagine you have a family recipe book (the global variable) that everyone knows. If you want to change one of the recipes when you're cooking alone (inside the function), you need to make sure you’re updating the family recipe book and not just writing a note for yourself. The global
keyword is like a reminder that you are modifying the family recipe book—the updates need to be recognized by everyone.
Learn essential terms and foundational ideas that form the basis of the topic.
Key Concepts
Local Variables: Variables that can only be used within the function they are defined in.
Global Variables: Variables that can be accessed and modified from any part of the program.
global Keyword: A keyword that allows modification of global variables inside functions.
See how the concepts apply in real-world scenarios to understand their practical implications.
Example of a local variable: def func(): x = 5
makes x
accessible only within func()
.
Example of using the global
keyword: def modify(): global x; x = 20
updates the global variable x
.
Use mnemonics, acronyms, or visual cues to help remember key information more easily.
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).
Review key concepts with flashcards.
Review the Definitions for terms.
Term: Local Variable
Definition:
A variable declared inside a function that is accessible only within that function.
Term: Global Variable
Definition:
A variable declared outside of all functions, accessible throughout the entire module.
Term: global Keyword
Definition:
A keyword used inside a function to declare that a variable refers to a global variable.
global x
tells Python that we are referring to the global variable x
, right?"