How to Use Global Variables in a Function in Python

The scope concept is somewhat similar to that of a namespace. Scope provides information about the viability of a Python variable. In Python programming, four kinds of scopes occur which are usually referred to as the LEGB method which are local, enclosed, global, and built-in. The article discusses the global scope in detail and how to utilize the global variables in a function in Python.

How to Use Global Variables in a Function in Python?

A global variable is utilized in any place inside the Python code. It can be altered and accessed from anywhere no matter where it is defined in the program. A global variable can thus be used from anywhere inside the program.

The Syntax of Global Variable Defined Through the Global Keyword

The following syntax is used for the declaration of a global variable

global var
  • Here global refers to the global keyword when a global variable is defined inside a function. It is not needed when the global variable is defined outside the function.
  • The var refers to the variable that has been given the global scope.

What are the Advantages of Global Variables?

The global variable holds the data that can be used throughout the program. Like defining an operation that several functions inside the Python code can utilize. Some advantages of global variables include:

  • They can be utilized and can be altered from any place in the the code
  • Having a global scope means they can be used anywhere inside the Python code.
  • They can be defined anywhere in the Python code. 
  • They are helpful in data transmission

How Does Python Handle the Global Variable?

The usual way in Python to find the variables is by using the LEGB method which defines how a variable is searched out in a Python program. The four scopes present in the LEGB method are:

  • Local: A variable having a local scope means it is located inside a function.
  • Enclosed: A variable is said to have an enclosed scope if it is present inside a nested function.
  • Global: A global variable has a global scope means that it is present at the module level and can be utilized anywhere in the Python program.
  • Built-in: A built-in scope can be made anywhere inside the Python script.

The visual representation of the LEGB method to look for the variable names while considering its scope is as follows:

If a variable is declared inside a function and it needs to have a global scope, then the global keyword must be used with that particular variable.

Comparison Between Local and Global Variables

The difference between the global variables and local variables is that the global variables are created outside the function and thus have a global scope while the local variables are created inside a function so any other function inside the code can not utilize it. The only function that can utilize the local variable is the function it is defined in.

Example 1: When a Global Variable is Defined Outside the Function

When any alteration is made in the global variable, the change occurs everywhere the variable is utilized. The following code explains how global variable is defined outside the function:

var="This is a global variable!"
def func():
    print(var)
func()

In the above code,

  • A variable var is defined outside the function func that stores the string value “This is a global variable!”
  • The func() function is defined to print the variable var.
  • The function func() is then called which prints out the string.

Output

The output displays how a global variable can be defined outside the function in Python:

Example 2: Defining the Global Variable Inside a Function

Defining a variable inside the function automatically gives it a local scope. However, a variable inside the function can be given a global scope using the global keyword. Here is a code that shows the implementation of defining a global variable inside a function:

var="This is a global variable!"
def func():
    global var
    var = "This is a global variable inside a function!"
    print(var)
func()

In the above code,

  • A variable var is defined.
  • A function func() is defined.
  • The global keyword gives the global scope to the variable var.
  • The var contains the string value.
  • The function prints the value of var.
  • The function func() is called and the updated value of the var is printed.

Output

The following output shows how a global variable can be created inside a function:

Example 3: Creating the Same Local Variable as the Global Variable

When the local variable is called the same as a global variable, the local variable has a preference for when that function will be called. A global variable is utilized if any other function is called that does not utilize the local variable. Here is a code that demonstrates this concept:

int = 10
def double():
  int = 20
  print(int+int)
 
def sq():
    print(int**2)
   
double()
sq()

In the above code,

  • A global variable int is given a value of 10.
  • A function double() is defined that takes the value of int as 20. The variable int will have a local scope. It prints the double of a number.
  • Another function sq() is defined that takes the square of a number. Here, no new local variable is defined so instead it will take the square of the global variable.
  • Both functions are called to display the results.

Output

The following output displays the usage of global and local variables that are similarly named in Python:

Conclusion

In Python programming, a global variable is defined outside a function or it can be defined inside a function using the global keyword. The global variable can be utilized and modified anywhere in the Python program as it has a global scope. They are really useful as they enable efficient data transmission. They need to be handled with consideration so that no naming problems occur. This article has provided different examples that show the use of the global variables in Python.