Home » Python Tutorial » Python Local and Global Variables

Python Local and Global Variables


Reading Time - 6 mins

Python Local and Global Variables – We can define the scope of the variable as the extent of the program within which we can access the variable. In this tutorial, we will cover the Local variable in Python and Global Variable in Python. We will also discuss the scope of a local and global variable in Python and how to use global variables inside the local scope.

In Python, there are two primary variable scopes:

  • Local variable in Python
  • Global variable in Python

Global Variable in Python

Global Variable in Python – Variables created outside the function are called global variables. Anyone can access the global variable, both inside and outside of a function. Below is the program for the global variable in python.

#variable defined in global scope
var='Hello world'

#function accessing the global variable
def my_function():
  print(var)

#calling the function
my_function()

Here we create a variable called ‘var’ and assign a value ‘Hello world’ to it. We are defining the variable ‘var’ outside the function ‘my_function’. When we call the function, it prints the value of the variable ‘var’. Since we don’t have another variable with the same name defined anywhere inside the function, the global variable is getting printed.

Local Variable in Python

The variable defined inside the function is called the local variable. We can access the local variable only inside the function. Below is the program for local variable in python.

def my_function():
    #variable defined in local scope
    var='Hello world'
    print(var)

#calling the function
my_function()

What happens when we try to print the variable outside the function?

>>> var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'var' is not defined
>>>

When we try to access the local variable outside of the function, it throws an error.

READ  Python Lists

Scope of Local and Global variables

Let’s say we define a variable with the same name both inside and outside the function. Then inside the function, the variable defined in local scope is given priority over the variable defined in the global scope.

var='global scope'
def my_function():
    var='local scope'
    print(var)

my_function()
print(var)

The output will be:

local scope
global scope

Using global variables inside the local scope

Even though we define the variable with the same name inside and outside the function, by changing the value of the variable in the local scope doesn’t affect the value of the variable in the global scope. What if you want to change the value of the global variable inside the function.

Inside the function, we have to use the keyword ‘global’ before a variable so that we can change the variable at the global level.

var='global scope'

def my_function():
    global var
    print(var)
    var='local scope'

my_function()
print(var)

And the output is:

global scope
local scope

Inside the function, we are mentioning that the variable is a global variable. We are changing the value of the variable inside the function, and when we print the variable outside the function, it displays the changed value. This is how we change the value of a global variable inside the function.

Appendix

In the official documentation, you can find more information on rules for local and global variables.

Conclusion

We have covered Python Local and Global Variables in detail. We have also discussed the scope of local and global variables and how to use global variables inside the local scope.

READ  Python Functions
Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions