Global Variables in Python

A variable type that is not declared inside, but outside the function in any programming language is called a global variable. This type of variable is made available to all the other functions present inside the program i.e. they can be used by programmers both inside and outside the function. We can also say that global variables remain in a global state. The method to declare this variable is on the top of rest of the functions and is made confidential generally so that no programmers change it while accessing other functions. These variables are different from local variables as local variables are always defined inside the function. In this article, we will understand the meaning of global variables, how to create global variables in python, how to access global variables, global keywords as well as the difference between local and global variables.

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.

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

 

Python Training Certification

Weekday / Weekend Batches

 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:

1. Python Partial Functions

2. Python Split Method

3. Python Frameworks

4. Python Generators

5. Python List Length

6. Python Operators

Find our upcoming Python Training Certification Online Classes

  • Batch starts on 10th Jul 2022, Weekend batch

  • Batch starts on 14th Jul 2022, Weekday batch

  • Batch starts on 18th Jul 2022, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Amani
Amani
Research Analyst
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.