How to use Python Global Variables – with example

  • Reading time:15 mins read

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.


python global variable


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’.


python global variable


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.


python global variable

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.


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

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!





Not sure if programming is for you? With CodeBerry you’ll like it.