How to declare a global variable in Python

How to declare a global variable in Python

A variable which can be accessed by the other block is called a global variable. It can be defined in outside a block. In other words, a global variable is defined outside the function, and we can access it inside the function.

On the other hand, a variable defined in a block and available for in that block is called a local variable. Such a variable can be accessed only in the defined block.

Let’s understand the following example of a local and global variable.

Example – Local Variables

Output:

The sum is: 30

Explanation:

The variable is defined inside the function and it can only use in a defined function so that nature of the variable is called a local variable. We cannot access them in other functions.

To overcome this problem, we use global variables. Let’s understand the example of a global variable.

Example –

Output:

The sum is: 30
The sub is: 10

Explanation:

In the above code, we have defined two global variables a and b outside the functions. We used them inside the sum() and sub() function. Both functions returned the result when we called.

If we define the same name local variable, it will print the value that is inside the function and then global variable value.

Example – 3:

Output:

Hello, how are you?
I am fine

Explanation:

We have defined the local variable the same name as a global variable; first, it printed the local variable and then global variable value.

The Global Keyword

Python provides the global Keyword that is used to modify the value of the global variable inside the function. It is beneficial when we want to change the value of the global variable or assign some other value. Below are a few rules to define the global variables.

Rules of global Keywords

  • If the value is defined output the function, it will automatically become the global variable or its scope globally.
  • The global Keyword is used to declare the global variable inside a function.
  • We don’t need to use the global keyword to declare a global variable outside the function.
  • Variables that have reference inside a function are implicitly global.

Example – Without global keyword

Output:

line 8, in mul
    c = c * 10
UnboundLocalError: local variable 'c' referenced before assignment

Explanation:

The above code has thrown an error because we have tried to assign the value to the global variable. We can modify the value of global value inside a function using the global keyword.

Example – With global Keyword

Output:

The value inside function:  100
The value outside the function:  100

Explanation:

In the above example, we have defined the variable c in mul() function using the global keyword. The value of c is multiplied by the 10; therefore, it returned the 200. We can see in the output that the variation in value inside the function is reflected in the value outside the global variable.

Global Variables in Python Modules

The benefit of the global keyword is to create global variables and share them among the different modules. For example – we create a name.py which consists of global variables. If we change these variables, then this change will reflect everywhere. Let’s understand the following example.

Code – 1: Make a name.py file to store the global variables.

Code – 2: Make a change.py file to modify global variables.

Here, we have modified the value of a, b, and msg. These global variables were defined in the name.py file and we imported name, and accessed these variables.

Code – 3: Make a result.py file to print the modified global variables.

Output:

15
26
Welcome to JavaTpoint

Global in Nested Functions

We can use the global keyword inside a nested function. We must declare the variable using the global keyword inside a nested function. Let’s understand the following example.

Example –

Output:

Before modifying :  15
Making change
After modifying:  15
value of x 20

Explanation:

In the above code, the value inside add() takes the value of local variable x = 15. In modify() function, we have assigned the x = 20 using the global keyword. That change reflected in add() function variable.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263611.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论