What Is the Global Variable In Python?
In the programming world, a global variable in Python means having a scope throughout the program, i.e., a global variable value is accessible throughout the program unless shadowed.
A global variable in Python is often declared as the top of the program. In other words, variables that are declared outside of a function are known as global variables.
You can access global variables in Python both inside and outside the function.
X = “sampleGlobalValue”
How to Create Global Variables in Python?
To create a global variable in Python, you need to declare the variable outside the function or in a global scope.
How to Access the Global Variable Inside and Outside of the Function?
In the example depicted above, you saw a global variable declared and accessed both inside and outside of the function.
So, you are accessing the value both inside and outside of the function, which is fine, but what happens if you try to modify the global scope variable value inside a function?
See the example mentioned below to understand better.
As it is evident, this throws an error. When you try to modify the global variable value inside a function, it will throw UnboundLocalError, because while modifying Python treats x as a local variable, but x is also not defined inside the function (myfunc()).
That’s where the Global keyword comes into the picture. You will see the usage of Global Keywords in the following sections.
Global keyword is used to modify the global variable outside its current scope and meaning. It is used to make changes in the global variable in a local context. The keyword ‘Global’ is also used to create or declare a global variable inside a function.
Usually, when you create a variable inside a function (a local variable), it can only be used within that function. That’s where the global keyword comes in the play, which helps to create global variables inside the function and which can be accessible in a global scope.
Use a global keyword to create a variable in the global scope.
Use a global keyword to change the value of the global variable inside the function.
You have seen what ‘global’ keywords are, their examples, and how to use global keywords. But Python has some basic rules to use the ‘global’ keyword.
Let’s see Global in Nested functions.
When you declare a global keyword variable inside the nested function and when you change the global keyword variable inside the nested function, it will reflect outside the local scope, since it is used as a global keyword.
Let's see an example for global in nested functions.
You can see the above output for the global in nested functions. But maybe a quick following explanation will help for better understanding.
You have declared the global variable inside the inner() function, which is nested inside the main() function.
Before and after calling the inner(), the variable ‘integ’ takes the value of the local variable main i.e. integ = 20. Outside of the main() function, the variable ‘integ’ takes the value of the global keyword declared inside the inner() function i.e., integ = 20 as you used the global keyword inside the inner() function local scope. If you make any changes inside the inner() function global keyword variable ‘integ’, will reflect outside of the scope, as a behavior of the global keyword.
The fundamental rules of the ‘global’ keyword are as follows:
- When you create a variable inside the function, it is in a local context by default
- When you create or define a variable outside the function, by default it is a global context, there’s no need for a global keyword here
- Global keywords can be used to read or modify the global variable inside the function
- Using a global keyword outside of the function has no use or makes no effect.
Difference Between Global and Local Variables
Let us see an example of how global and local variables behave in the same code.
Here in the program above, you declared x as a global and y as a local variable in the same program. Then it tried to modify the global variable using the global keyword in the local function and printing both gx and ly.
Once you called function1(), the value of gx became global global. As you tried to modify as gx*2, it printed ‘global’ two times. After this, you printed the local variable ly, which displayed the local variable value i.e., again ‘local’.
Difference Between Global and Nonlocal Variables
When a variable is either in local or global scope, it is called a nonlocal variable. Nonlocal variables are defined in the nested function whose scope is not defined.
From the above program, it can be noticed that the nested function is innerfn(). Inside the innerfn(), you saw the use of a nonlocal keyword to create a nonlocal variable. The innerfn() is defined in the scope of outerfn(). If you make changes to the value of a nonlocal variable, they reflect in the local variable.
Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer
Variables are one of the most basic elements of a programming language. It is an abstraction layer for the memory cells that contain the actual value. Global, Local and Nonlocal types of variables help the programmer to access some values entirely in the program scope, or some values that are limited to within the function.
In this article, you learned what a global variable is in Python, how to define global variables in Python, how to declare a global variable in Python, what is a global keyword, when to use a global keyword, the difference between global, local, and nonlocal variables along with some working examples.
Join Simplilearn's Python Training Course to learn more about this topic. This course will teach you the basics of Python, conditional statements, data operations, shell scripting, and Django. This certification course, which includes 38 hours of blended learning and 8 hours of online self-paced learning, will prepare you for a fulfilling career as a professional Python programmer by providing you with practical programming experience.
Have any questions for us? Leave them in the comments section of this article, and our experts will get back to you on them, as soon as possible!