Menu Close

Python Global Keyword With Examples

Python Global Keyword

In this python guide, we are going to learn all about Python global keywords, and Python global variables along with examples. The global keyword in python is the most important keyword because using this keyword you can change the global variable value inside the local scope.

Before going through this article, you should have basic knowledge of Python global and local variables.

Python Global Keyword

Python global keyword provides a facility to modify the variable define outside of the current scope as well as it is used to create a global variable inside the local scope.

Some points of Global keyword

You have to keep in mind some points if you are going to work with python global keyword:-

  • When we create a variable inside a function it is a local variable by default.
  • When we create a variable outside a function it is global by default.
  • When we use a global keyword outside a function it does not affect.
  • We use a global variable to read and write a global variable inside a function.

Example:


# Global keyword
my_name = 'Python Programming'
def printNow():
	print("My name is:- ", my_name)

#Calling function	
printNow()

Output

My name is:-  Python Programming

In the above program, we create a global variable my_name and assign a string literal to my_name variable is ‘Python Programming‘.
When we create a variable outside a function then it is global by default. We don’t need to use global keywords outside.

Modify global variable inside function

What happens if we will try to modify the global variable value inside a function?

Example:

# Global variable
my_name = 'Python Programming'

def printNow():
	my_name = my_name + ' :- Programming Funda'
	print("My name is:- ", my_name)
	
#Calling function	
printNow()

If we try to execute the above program, then we will get an error because we can’t modify the global variable inside the function.

To solve the above problem we will use the global keyword. let’s see how to use the global keyword in the above program.

Example:


# Global variable
my_name = 'Python Programming'

def printNow():
	global my_name
	my_name = my_name + ' :- Programming Funda'
	print("My name is:- ", my_name)

#Calling this function	
printNow()

In the above program, we use a global keyword to make a my_name as a global variable inside printNow() function and then changed the value of the my_name.

Global keyword in nested function

You can use the Python global keyword inside a nested function.

Example:


def outer():
    x = 30	
    def inner():
        global x
        x = 40

  
    print("Value of x before calling inner() function:- ", x)
    print("Calling the inner() function")
    inner()
    print("Value of x after calling inner() function:- ", x)
	
outer() 
print("Value of x in main:- ", x) 

Output

Value of x before calling inner() function:-  30
Calling the inner() function
Value of x after calling inner() function:-  30
Value of x in main:-  40

In the above program, we declared a global variable x inside the nested inner() function. Inside the outer() function, x has no effect on the global keyword.

The value of x before and after calling the inner function will be the same. Outside of the outer() function, the value of x will be changed whose defined in the inner() function.

Conclusion

So in this article, you have learned all about Python’s global keyword to change the value of a global variable inside the local scope or inside the function. The global keyword in python is the best python keyword to change the global variable. The global keyword in python will very helpful to your future python programming.

If you like this article, please share and keep visiting for further python interesting tutorials.

For More Information:- Click Here

Thanks for your valuable time … ❤️❤️🙏🙏

Python Global, Local and Nonlocal Variables
Python ord() Function

Related Posts