Javatpoint Logo
Javatpoint Logo

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


The sum is: 30


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 -


The sum is: 30
The sub is: 10


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:


Hello, how are you?
I am fine


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.

Example - Without global keyword


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


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.

Example - With global Keyword


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


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 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 file to store the global variables.

Code - 2: Make a file to modify global variables.

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

Code - 3: Make a file to print the modified global variables.


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 -


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


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.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA