Javatpoint Logo
Javatpoint Logo

Variable Scope in Python

Variables in Python language serve as the storage units for the various data values and data structures. When a variable is assigned to any Python object, it then points toward that object since they are reference, or a pointer, to that particular object in the memory location. Python programming language is not "statically typed," unlike other languages like C/C++/JAVA. Variables do not have to have their types or initial values declared before use. When a variable is initially given a value, it is considered to have been formed.

Code

Output:

This value is stored in the variable 'integer':-  45
This value is stored in the variable 'floating':-  1456.8
This value is stored in the variable 'string':-  John

Scope of a Variable

The scope of a Python variable refers to the area where we may locate it and, if necessary, access it.

Global and Local Variables

Global variables are those we declare and define outside of any functions but not specific to any of them. Any section of the program can make use of them.

Code

Output:

This is in the main function:- 
2
This is in function 'f1()':- 
2
This is in function 'f2()':- 
2

Suppose a variable is defined within the local scope of that function and has the same name as the global variable. In that case, it will only display the value supplied to the variable inside that particular function and not the value assigned in the global scope.

Code

Output:

This is defined inside the function.

Before defining the function func(), the variable 'a' is assigned to a string that states, "This is assigned in the global scope.". The print(a) statement of the function func() will first search for variable 'a' in its local scope. Since there is already a variable 'a' assigned to a different string, the function will print the value of this variable and will not go in the global scope. The function, in this case, will not use the value of variable 'a' from the global scope.

What will be the result of modifying the value of variable 'a' inside the function 'func()' is the next question. Will it also have an impact on the global variable 'a'? The following snippet of code is used to test it:

Code

Output:

Accessed in the local scope of function:-  This is defined inside the function.
Accessed in the global scope:-  This is assigned in the global scope.

We must use the Python global keyword to make the preceding program function. Only when making assignments or changing the variable value do we need to use the Python global keyword inside a function. The global keyword is not necessary for displaying and accessing a variable present in the global scope. Why? Since the assignment to the variable 'a' inside of func(), Python "presumes" that we desire to access the local variable, which is why the initial print command returns the variable's value in the local scope. If a variable is modified or assigned inside a function without being defined as a global variable, it is considered a local variable. The following example demonstrates how to use the global keyword to instruct the Python interpreter that we wish to modify the global variable:

Code

Output:

Accessed in the local scope of function:-  This is defined inside the function.
Accessed in the global scope:-  This is defined inside the function.

Summarising the Global Local Variable Scope.

Code

Output:

global:  in the global scope
Inside func():  in the global scope
global:  in the global scope
Inside local(): 
inside function local()
global:  in the global scope
Inside global_() : 
changed inside function global_()
global:  changed inside function global_()

Nonlocal Keyword

In Python, the nested functions are handled with the Python nonlocal keyword. This keyword acts similarly global keyword, except that in the case of nested functions, it defines a variable to refer to the variable assigned in the outer enclosing function rather than the global variable.

Code

Output:

Using the nonlocal keyword before changing a:
Inside 'inner' function:- 
14
Inside the 'outer' function:-  14
In the global scope:- 
0

Not using the nonlocal keyword before changing a:
Inside 'inner' function:- 
14
Inside the 'outer' function:-  3
In the global scope:- 
0

Next TopicAlphabet in Python





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA