Python Global Keyword Example

In the previous tutorial, we learned that what are global variables, local variables and nonlocal variables. And how to use them.

Global Keyword in Python

In this post, we will learn what is the global keyword in Python. What are the rules for writing this and how do you use the global keyword in Python programs.

What is global keyword in python

In Python, it is used to create a global variable. And also you can modify variables outside the current scope.

As you know, we cannot change the value of the global variable inside a function. If you don’t know, read this. But if you use the global keyword, then you can also change the value of the global variable inside the function.

Rules to write a global keyword in python

Following are the rules for writing global keywords in Python:

  1. When writing a variable inside a function. So that is the local variable.
  2. If you define a variable outside the function, it is a global variable. For this you do not need global keywords.
  3. If you want to define a global variable inside the function, then you have to use the global keyword.
  4. Use of global keywords outside a function has no effect

Using global keyword in python

Example 1: Accessing global Variable From Inside a Function

a = 1 # global variable

def myFunc():
    print(a)

myFunc()

First we defined a global variable in the program named a. After this we assigned = = 1 to this variable.

After this we created a function called myFunc (). Inside this function, we called the global variable.

After that we called this function from which we got some output which is given below.

Output

1

Example 2: Modify Global Variable From Inside a Function using global keyword

a = 0 # global variable

def myFunc():
    global a
    a = a + 2 # increment by 2
    print("Inside add():", a)

myFunc()
print("In main:", a)

First we defined a global variable in the program named a. After this we assigned = = 1 to this variable.

After this we created a function called myFunc (). Inside this function, we modified the value of the global variable by using the global keyword.

After that we called this function from which we got some output which is given below.

Output

Inside add(): 2
In main: 2

Note:- If you want to change the value of the global variable inside the function, then you have to use the global keyword for this. We will give an example of changing the value of a global variable.

In this post, you have learned how to change the value of the global variables by using the global keyword.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *