Use Global Variable in a Python Function



We can use a global variable inside a Python function by declaring it using the global keyword. This allows us to read or modify a variable that is defined outside the function's local scope.

By default, any variable you create or change inside a function is considered a local variable (only used inside that function). If we try to change a global variable without declaring it as global, Python will create a new local variable with the same name, and the actual global variable will remain unchanged.

In this article, we will understand what global variables are, how to use them inside a function, and how to modify their values using examples.

What is a Global Variable?

A global variable is a variable that is defined outside all functions. It can be accessed and used anywhere in the program, including functions.

However, if we want to modify the value of a global variable inside a function, we must use the global keyword. Otherwise, Python will treat it as a local variable and create a new one inside the function.

Accessing Global Variable Inside a Function

We can access a global variable inside a function without using the global keyword if we only want to read its value. The function will simply refer to the variable that was defined outside its scope (at the global level).

Example

In the following example, we are printing the value of the global variable x from inside the function. Since we are not changing its value, we don't need to use the global keyword ?

# global variable
x = 10  

def print_value():
   print("Value of x is:", x)

# Call the function
print_value()

Following is the output of the above code ?

Value of x is: 10

Modifying Global Variable Inside a Function

To change the value of a global variable from inside a function, we need to declare it as global using the global keyword. This tells Python that we are referring to the global variable, not creating a new local one.

Example

In this example, we are modifying the global variable x from inside the function using the global keyword. This allows the function to update the original value of x instead of creating a new local variable -

# global variable
x = 5  

def update_value():
   global x
   x = x + 10
   print("Inside function, x is:", x)

# Call the function
update_value()

# Print updated value of x
print("Outside function, x is:", x)

We get the output as shown below ?

Inside function, x is: 15
Outside function, x is: 15

What Happens Without Using global?

If we try to modify a global variable without declaring it as global, Python will treat it as a new local variable and may give an error if we try to read it before assigning.

Example

In the example below, we are trying to update the global variable x inside the function without using the global keyword. This causes an error because Python treats x as a new local variable, but it's being used before it is defined ?

# global variable
x = 7  

def try_update():
   # trying to modify without global
   x = x + 3   
   print("Inside function, x is:", x)

# Call the function
try_update()

The result obtained is as shown below ?

UnboundLocalError: local variable 'x' referenced before assignment

When Should You Use Global Variables?

Global variables are helpful when you need to share a value across multiple functions, for example, a configuration setting or a counter that many functions update. In such cases, using a global variable can make your code simpler and avoid passing the same value repeatedly between functions.

However, relying too much on global variables can make your code harder to follow, especially in large programs. It becomes difficult to track where and how the variable is being changed, which can lead to bugs. That's why it is usually better to use function parameters and return values to pass data around.

Example

In the following example, we are using a global variable counter to keep track of how many times the increment() function is called.

Inside the function, we use the global keyword so that we can update the global counter variable. After calling the function three times, the value of counter becomes 3 ?

# global variable
counter = 0  

def increment():
   global counter
   counter += 1

# Increment counter 3 times
increment()
increment()
increment()

print("Final counter value is:", counter)

Following is the output of the above code ?

Final counter value is: 3
Updated on: 2025-04-11T14:55:53+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements