Menu Close

Python Global, Local and Nonlocal Variables

Python global variable

In this article, you will learn all about Python Global, Python Local, and Python Nonlocal variables along with examples.
Python variable is the most important thing in any programming language. The variables are used to store value for future use.

In this guide, we will see what is python’s local, global, and nonlocal variables as well as we will see how to use them using examples.

Python Local variable

A variable declared inside the function’s body and local scope is called the python local variable.


def Sum():
	#Local variable
	x,y = 12,34
	print(f"Sum of {x} and {y} is:- {x+y}")	


Sum of 12 and 34 is:- 46

In the above example, x and y are python’s local variables which accessible only within the function scope.

If you try to access the python local variable from outside the function, Then you will get an error.

def Sum():	
	x,y = 12,34
	print(x + y)
print(x) #<- error will be raised


NameError: name 'x' is not defined

Python Global Variable

Python global variable declared outside of the function or global scope of the function is called a global variable. Python Global variable can use both inside and outside of the function.


# Global variables
x = 12
y = 4

def Divide():
	result = x/y
	print('Result is:- ', result)

Output will be:- Result is:- 3.0

In the above program, x and y declare as the global variable that can be used inside and outside of the function. Then, we can use the division operator (/) to calculate the division of both x and y and then print the result.

Python Global and Local variables

You can use python global and local variables both in the same program.

#Global variable
x = 12
def Multiplication():
	global x
    #Local variable
	y = 4
	x = x * y
	print(f"Multiplication of {x} and {y} is:- {x * y}")


Multiplication of 48 and 4 is:- 192

In the above program, we declare x as the global variable and y as the local variable inside the Multiplication() function. Then, we use the multiplication operator (*) to calculate the multiplication of x and y and change the value of x.

Python Global and Local variables same name

What happen if the python global and local variables same name.


language = 'Python'
def printNow():
	language = 'Programming'
	print('Local variable:- ', language)
print('Global variable:- ', language)


Local variable:- Programming
Global variable:- Python

In the above code, the name of the global and local variables is the same. We get different results when we print the same variable because the variable is declared in both scopes.

when we print the language inside a function then it will print the value of the python local variable and when we print the language outside of the function then it will print the value of a python global variable.

Python Nonlocal Variable

Python Nonlocal variable is used inside in nested function whose scope is not defined, that means the scope of a nonlocal variable is neither local and global.

We create a nonlocal variable using a nonlocal keyword.
If we changed the value of the nonlocal variable then it will affect the value of a python local variable.

def outer():
    #Local variable
	x = 'Python'
	def inner():
        #Nonlocal variable
		nonlocal x
		x = 'Programming'
		print("Inner x:- ", x)		
	print("Outer x:- ", x)


Inner x:-  Programming
Outer x:-  Programming

In the above program, we create a nested function inner and inside the inner() function we created a nonlocal variable x and change the value of x whose defined inside the outer() function.

Learn Python From Scratch


In this article you have learned python global and local variables and also we have seen python nonlocal variable along with examples.
If you like this article, please share and keep visiting for further python tutorials.

For more information:- Click Here

Python if else statement
Python Global Keyword ( Best Examples )

Related Posts