Global Variables in Python
Last updated on Dec Fri, 2022 5793

Creating a Global Variable
The user can create a global variable by declaring the variable outside the function in python. This can also be done using declaring the variable in a global scope.
Become a Python Certified professional by learning this HKR Python Training!
Let’s see the syntax of how a global variable is created:
def function():
print("Inside Function", a)
# Global scope
a = "HRX Training"
function()
Output of the program:
Inside Function HRX Training
>
Below is another python code to understand how a global variable is created to use inside a function:
a = "HRX"
def myfunc():
print("Welcome to " + a)
myfunc()
Output of the program:
Welcome to HRX
In the code above, the user a global variable ‘ a and defined a function to print it. Finally, when he calls the function, the output is printed as the value of variable ‘a’.
In case the user creates a variable inside a function with the same name, then it will be called a local variable, however, it can be made use of only inside the function. In python, a global variable will remain the same with the same name ‘global’ along with the original value.
Below is a python code to understand how a global variable is created with the same name as ‘global’ inside a function:
a = "great learning platform"
def myfunc():
a = "amazing learning platform"
print("HRX is " + a)
myfunc()
print("HRX is " + a)

Python Training Certification
- Master Your Craft
- Lifetime LMS & Faculty Access
- 24/7 online expert support
- Real-world & Project Based Learning
Output of the program:
HRX is amazing learning platform
HRX is great learning platform
Global Keyword
As we have understood till now that when a user creates a global variable inside the function, the variable is actually a local variable hence it can only be used inside the function. But to create e global variable inside a function, the user can use the keyword ‘global’. The global keyword will give access to the global space and hence will be used inside.
If you want to Explore more about Python? then read our updated article - Python Tutorial
Below is a python code to make understand the use of global keywords making the variable accessible inside the function:
def myfunc():
global a
a = "HRX"
myfunc()
print("Welcome to " + a)
Output of the program:
Welcome to HRX
We have seen the example above where the global keyword is being used to make variabel accessible in global space however, it is not necessary that python will support it every time. Python follows some set of rules for a global keyword.
Let us take an example of a global variable in nested functions. When a user declares a global keyword inside the nested function and changes the global keyword variable inside a nested function, it comes to a conclusion that the reflection will be outside the local scope because it is being used as a global keyword in python.
Let us see an example below to understand this concept better:
def myfunc():
integer = 10
def inner():
global integer
integer = 20
print("before calling: ", integer)
print("calling")
inner()
print("after calling: ", integer)
myfunc()
print("main integer", integer)
Output of the program:
before calling: 10
calling
after calling: 10
main integer 20
>
Subscribe to our youtube channel to get new updates..!
Let’s discuss some set of rules where a user can successfully use a global keyword:
1. When a user creates a variable inside a function, it is a local variable by default.
2. When a user creates a variable outside a function, it is global by default. The user need not place the ‘global’ keyword especially to make it global.
3. Global keywords have the ability to read as well ad modify the global variables inside the function.
4. There is no use in creating a global variable outside the function as it won’t change anything.
If you have any doubts on PostgreSQL, then get them clarified from PostgreSQL Industry experts on our PostgreSQL Community If you have any doubts on PostgreSQL, then get them clarified from PostgreSQL Industry experts on our PostgreSQL Community
If you have any doubts on Python, then get them clarified from Python Industry experts on our Python Community!
Difference between Global variable and Local Variable
Global Variables:
1. They are defined outside the function.
2. They tend to have a global space.
3. They are accessible inside every created function.
4. Their value is used in case there is no defined local function.
5. It can make the use of the ‘global’ keyword to create global space.
Local Variables:
1. Unlike global variables, they are defined inside the function.
2. They don't have any global space, as their scope is to the defined function only.
3. They are accessible only inside the function they are created for.
4. They are used as default. If absent then the user makes the use of global variables.
5. It can never make use of the ‘global’ keyword as they are only part of global variables.
Top 50 frequently asked Python interview Question and answers
Below is a python code to understand the use of local as well as global variables in a single program:
a = "global"
def myfunc():
global a
b = "local"
a = a * 2
print(a)
print(b)
myfunc()
Output:
global global
local
CONCLUSION
Variables are a very important part of programming languages. In other terms, they actually consist of the value in a function. In python, they perform similar features as normal functions. They are basically of 3 types - global, local, and non-local and in this article, we have deeply discussed global variables. We have also discussed how to create global variables in python, how to access global variables, global keywords as well as the difference between local and global variables along with some examples as well as python codes.
Related Articles:
About Author
As a content writer at HKR trainings, I deliver content on various technologies. I hold my graduation degree in Information technology. I am passionate about helping people understand technology-related content through my easily digestible content. My writings include Data Science, Machine Learning, Artificial Intelligence, Python, Salesforce, Servicenow and etc.
Upcoming Python Training Certification Online classes
Batch starts on 6th Oct 2023 |
|
||
Batch starts on 10th Oct 2023 |
|
||
Batch starts on 14th Oct 2023 |
|
FAQ's
.