Learn Python – How to declare a global variable in Python- Basic and advance

A variable which can be accessed by way of the other block is called a world variable. It can be described in backyard a block. In other words, a global variable is described outside the function, and we can access it interior the function.

On the different hand, a variable described in a block and on hand for in that block is known as a neighborhood variable. Such a variable can be accessed solely in the defined block.

Let’s apprehend the following example of a nearby and international variable.

Example – Local Variables

# example of local variable  
def sum():  
    a = 10   # local variables  
    b = 20  
    c = a + b  
    print("the sum is:", c)  
  
sum()  # function calling  

Output:

The sum is: 30

Explanation:

The variable is defined inner the function and it can only use in a described characteristic so that nature of the variable is referred to as a neighborhood variable. We cannot get admission to them in different functions.

To overcome this problem, we use world variables. Let’s apprehend the example of a international variable.

Example –

# example of a global variable  
a = 20   # defined outside the function  
b = 10  
  
def sum():  
    c = a + b  # Using global variables  
    print("The sum is:", c)  
  
def sub():  
    d = a - b   # using global variables  
    print("The sub is:", d)  
  
  
sum()  # function calling  
sub()  

Output:

The sum is: 30
The sub is: 10

Explanation:

In the above code, we have described two world variables a and b outside the functions. We used them interior the sum() and sub() function. Both functions again the result when we called.

If we define the identical name local variable, it will print the price that is internal the function and then global variable value.

Example – 3:

def msg():  
    m = "Hello, how are you?"  
    print(m)  
  
msg()  
m = "I am fine"  # global scope  
print(m)  

Output:

Hello, how are you?
I am fine

Explanation:

We have defined the nearby variable the equal title as a world variable; first, it printed the neighborhood variable and then international variable value.

The Global Keyword

Python affords the world Keyword that is used to alter the fee of the global variable inner the function. It is advisable when we favor to alternate the fee of the international variable or assign some other value. Below are a few regulations to define the international variables.

Rules of global Keywords

If the value is defined output the function, it will automatically become the global variable or its scope globally.

The global Keyword is used to declare the global variable inside a function.

We don’t need to use the global keyword to declare a global variable outside the function.

Variables that have reference inside a function are implicitly global.

Example – Without global keyword

# The example of without using the global keyword  
  
c = 10  
  
def mul():  
  
    # Multiply by 10  
    c = c * 10  
    print(c)  
  
mul()  

Output:

line 8, in mul
    c = c * 10
UnboundLocalError: local variable 'c' referenced before assignment

Explanation:

The above code has thrown an error because we have tried to assign the fee to the world variable. We can modify the cost of world cost interior a feature the use of the world keyword.

Example – With global Keyword

# The example using the global keyword  
  
c = 10  
  
def mul():  
    global c  
    # Multiply by 10  
    c = c * 10  
    print("The value inside function: ", c)  
  
mul()  
print('The value outside the function: ', c)  

Output:

The value inside function:  100
The value outside the function:  100

Explanation:

In the above example, we have described the variable c in mul() feature the use of the international keyword. The cost of c is extended by way of the 10; therefore, it lower back the 200. We can see in the output that the variation in value interior the characteristic is reflected in the value outdoor the international variable.

Global Variables in Python Modules

The gain of the world key-word is to create international variables and share them among the specific modules. For instance – we create a name.py which consists of global variables. If we trade these variables, then this trade will reflect everywhere. Let’s apprehend the following example.

Code – 1: Make a name.py file to store the world variables.

a = 10  
b = 20  
msg = "Hello World"  

Code – 2: Make a change.py file to adjust world variables.

import name  
name.a = 15  
name.b = 26  
name.msg = "Welcome to JavaTpoint"  

Here, we have modified the cost of a, b, and msg. These global variables were defined in the name.py file and we imported name, and accessed these variables.

Code – 3: Make a result.py file to print the modified international variables.

import name   
import change  
print(change.a)   
print(change.b)    
print(change.msg)  

Output:

15
26
Welcome to JavaTpoint

Global in Nested Functions

We can use the world keyword internal a nested function. We need to declare the variable the usage of the world key-word inside a nested function. Let’s understand the following example.

Example –

# The example of global in nested function  
  
def add():  
    a = 15  
  
    def modify():  
        global a  
        a = 20  
  
    print("Before modifying : ", a)  
    print("Making change")  
    modify()  
    print("After modifying: ", a)  
  
  
add()  
print("value of x: ", a)  

Output:

Before modifying :  15
Making change
After modifying:  15
value of x 20

Explanation:

In the above code, the fee inner add() takes the fee of neighborhood variable x = 15. In modify() function, we have assigned the x = 20 the usage of the global keyword. That trade reflected in add() characteristic variable.