In this tutorial, you’ll learn what is a global variable in Python and how to use them effectively.
Goals of this lesson:
- Understand what is a global variable in Python with examples
- Use global variables across multiple functions
- Learn how to use the
globalkeyword to modify the global variables - Learn to use global variables across Python modules/files
- Understand the use of
globals()function - Use global variables inside a nested function
Table of contents
What is a Global Variable in Python
In Python, a variable declared outside the function or in global scope is known as a global variable. We can use global variables both inside and outside the function.
The scope of a global variable is broad. It is accessible in all functions of the same module.