Python Programming Global and Local Variables Tutorial with Stone River eLearning

If you have some sort of experience in working on programming languages such as Java, C or C
++, it will not be a surprise to know that you found the whole concept of global and local variables a bit confusing at first. In fact, more than 50% of beginners in any programming language find the global and local variable differentiation a bit difficult to digest. However; just like any other programming concept, global-local variables also seem pretty easy to implement once you understand how Python processes and works with these variables. Since Python has taken the whole concept of Object Oriented Programming to a new level whole together, it reflects on the basic concepts such as global-local variables as well and you will notice that Python’s understanding of this concept is a little different from how other programming languages read them.



def example():



def main():


In the above example, it is pretty clear that the variable ‘x’ is the global variable while the variable ‘z’ is a local variable to the function ‘example’. It can be noted that global variables are defined outside any function in the program while the local variables are restricted by the boundaries of the function it is defined in.

Since it is much easier to understand the concept of global and local variables, when it is explained using working examples. Therefore, this blog post will involve a few more examples than usual and it is recommended that the readers try out the examples on their systems to notice the difference.

Since local variables are restricted by the domain of a function, they cannot be directly used in any other function, however the global variables can be accessed in anywhere in the scope of the program. So how can we use the global variables in any function of the program?



def example2():






x= example2()


In the above example, the variable’ x’ is used inside the function ‘example2()’. There are a few things to note about using global variables inside any other function.


  1. First and the most important point about using global variables is that their values cannot be altered. Since global variable are available to the entire length of the program, their value remains the same as the one initialized at the beginning. Therefore, an operation such as

‘x +=1’ is not valid if ‘x’ is a global variable. You can however do this using a ‘global’ keyword, the same will be discussed later in the blog post.

  1. However; you can use the global variable in any calculation and assign the value of the calculation to a new variable. This statement can be verified in the example where global variable ‘x’ is used to perform an addition calculation and then the value is assigned to the local variable ‘y’.

Using the global variable inside a function can also be achieved by using the ‘global’ keyword. However; use of the ‘global’ keyword can create problems in the performance of the program in case of big applications and use of the global keyword is subjected to the type of program you are creating.



def example3():

global x

x +=1


Online Technology Courses Stone River eLearning