8.4.1 - Local vs Global 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 attempt to print it outside the function. What do you expect to happen?
💡 Hint: Think about where the variable is declared.
Create a script with both a local variable and a global variable. Demonstrate how changing the global variable affects the local variable (if at all).
💡 Hint: Remember to define your global variable outside of any functions.
1 more question available
Interactive Quizzes
Quick quizzes to reinforce your learning
What is a local variable?
💡 Hint: Think about where the variable is created.
True or False: A global variable is accessible only within the function it was defined.
💡 Hint: Remember where global variables are defined.
Get performance evaluation
Challenge Problems
Push your limits with advanced challenges
Define a global variable in your script. Then write a function that takes an argument with the same name as the global variable. Discuss the outcome when both are used in computation.
💡 Hint: Use print statements to understand which variable is being accessed.
Create a Python program with both local and global variables and demonstrate how changing the global variable within a function affects the output outside the function.
💡 Hint: Look at how variables are accessed and modified within functions.
Get performance evaluation
Reference links
Supplementary resources to enhance your learning experience.