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 because it is used to store value for future use.

In this guide, we are about to explore Python’s local, global, and nonlocal variables as we will see how to use them in a Python program.

Python Local Variable

A variable declared inside the function’s body or local scope is called the Python local variable.

Example:


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

Output

Sum of 12 and 34 is:- 46

In the above example, x and y are Python’s local variables which are 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)
Sum()
print(x) #<- error will be raised

Output

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 be used both inside and outside of the function.

Example:


# Global variables
x = 12
y = 4

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

Output will be:- Result is:- 3.0

In the above program, x and y are declared as the global variables 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()

Output

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 happens if the Python global and local variables have the same name?

Example:


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

Output


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 variable inside a function then it will print the value of the Python local variable and when we print the language variable 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, which means the scope of a nonlocal variable is neither local nor global.

We create a nonlocal variable using a nonlocal keyword.
If we change 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)		
	inner()
	print("Outer x:- ", x)
	
	
outer()

Output

Inner x:-  Programming
Outer x:-  Programming

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

Conclusion

So throughout this article, we have learned Python global and local variables and also we have seen Python nonlocal variables 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 With Examples

Related Posts