8.4 - Scope and Lifetime of Variables
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.
Practice Questions
Test your understanding with targeted questions
Define a local variable in a function and try to print it outside the function. What do you observe?
💡 Hint: Try printing the variable after the function call.
Create a global variable and declare it outside any functions. Then print it within a function. What happens?
💡 Hint: Just define it on the top level of your script.
4 more questions available
Interactive Quizzes
Quick quizzes to reinforce your learning
What is a local variable?
💡 Hint: Think about where the variable is defined.
True or False: A global variable can be accessed inside any function.
💡 Hint: Remember where global variables exist.
1 more question available
Challenge Problems
Push your limits with advanced challenges
Design and implement a Python script using a combination of local and global variables. Explain how local and global variables interact within your script.
💡 Hint: View this as testing the space they occupy in your code.
Create a function that adds to a global counter each time it's called, using the global keyword. Demonstrate by calling the function multiple times and printing the counter.
💡 Hint: Think about how often you call the function and what it does during each call.
Get performance evaluation
Reference links
Supplementary resources to enhance your learning experience.