What is a Python global variable?
In programming, a variable is simply a memory location that is used to store values.
Variables are classified in accordance with their type and scope within a program. Variables based on the scope can be classified into local variables and global variables.
How to use a Python global variable?
The scope of a variable in a program depends on whether it is declared within or outside a function. Using two simple functions below we are going to learn about Python global variables.
In the code below we have two functions: the say_hi() function that takes the name of the animal variable and later prints the variable with ‘says HI!’ afterwards.
The second function is called change_animal() which is basically designed to redeclare the animal variable and assign it a new value.
Python Global Variables – Example 1
Now if we run the first function and leave out the second function, we’re going to get the output in the terminal that says ‘says HI !’.
This is because we have the animal variable declared as the global variable that we have referenced within the function.
If we uncomment the second function by just looking at the function one might speculate that the variable animal is going to be reassigned to ‘leopard’.So we should get ‘leopard says HI’.
However, we do not get such an output since the variable animal that is declared within the change_animal() function is a local variable.
Although it has the same name as the initial animal variable that is declared at the beginning of the program the variable goes into garbage collection since it is not used by the program.
The function ‘say_hi’ won’t know that anything has happened and we will still get the animal variable as the elephant.
We can change the local variable to interact with the global variable by adding a short command inside the ‘change_animal’ function.
Example 2
However, we need to be careful when using global variables this way in our code and make sure it is too difficult to understand what is going on in the program.
In this case, if we wanted to use the ‘change_animal’ function to we could do so by writing the word global and then giving it the name of the variable. If we now run the function it will actually change the value of the animal variable.
Now that the animal variable declared within the ‘change_animal’ function is actually a global function declaring it with a different value affects the animal variable declared in the global scope.
Summary
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
