# How to declare a global variable in Python

A variable which can be accessed by the other block is called a global variable. It can be defined in outside a block. In other words, a global variable is defined outside the function, and we can access it inside the function.

On the other hand, a variable defined in a block and available for in that block is called a local variable. Such a variable can be accessed only in the defined block.

Let’s understand the following example of a local and global variable.

# How to declare a global variable in Python

A variable which can be accessed by the other block is called a global variable. It can be defined in outside a block. In other words, a global variable is defined outside the function, and we can access it inside the function.

On the other hand, a variable defined in a block and available for in that block is called a local variable. Such a variable can be accessed only in the defined block.

Let’s understand the following example of a local and global 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 inside the function and it can only use in a defined function so that nature of the variable is called a local variable. We cannot access them in other functions.

To overcome this problem, we use global variables. Let’s understand the example of a global 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 defined two global variables a and b outside the functions. We used them inside the sum() and sub() function. Both functions returned the result when we called.

If we define the same name local variable, it will print the value that is inside 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 local variable the same name as a global variable; first, it printed the local variable and then global variable value.

## The Global Keyword

Python provides the global Keyword that is used to modify the value of the global variable inside the function. It is beneficial when we want to change the value of the global variable or assign some other value. Below are a few rules to define the global 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 .

# 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 value to the global variable. We can modify the value of global value inside a function using the 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 defined the variable c in mul() function using the global keyword. The value of c is multiplied by the 10; therefore, it returned the 200. We can see in the output that the variation in value inside the function is reflected in the value outside the global variable.

### Global Variables in Python Modules

The benefit of the global keyword is to create global variables and share them among the different modules. For example - we create a name.py which consists of global variables. If we change these variables, then this change will reflect everywhere. Let’s understand the following example.

Code - 1: Make a name.py file to store the global variables.

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

Code - 2: Make a change.py file to modify global variables.
import name
name.a = 15
name.b = 26
name.msg = “Welcome to JavaTpoint”

Here, we have modified the value 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 global 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 global keyword inside a nested function. We must declare the variable using the global keyword inside a nested function. Let’s understand the following example.

Example -

``````# The example of global in nested function

a = 15

def modify():
global a
a = 20

print("Before modifying : ", a)
print("Making change")
modify()
print("After modifying: ", a)

print("value of x: ", a)
``````

Output:

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

Explanation:

In the above code, the value inside add() takes the value of local variable x = 15. In modify() function, we have assigned the x = 20 using the global keyword. That change reflected in add() function variable.