Updated April 1, 2023
Introduction to Local Variable in Python
Python local variable plays an important role in the entire python programming language as it is used for any scope definition and manipulation. A local variable in Python is always declared within a specific scope like it is mostly present within any function’s body where other members can access it. Therefore, it is very difficult and rare that local variables will be present outside the scope or outside the function. If a variable is present outside the scope, it is considered a global variable, and all the members become unreachable to a local variable.
Syntax of Local Variable in Python
The syntax flow for the local variable declaration in function for Python includes the following representation:
Function_declaration ():
Variable= "var_assign"
Logic statement ()
Function_declaration () //calling of the function
Explanation:
A function is declared, and then the variable is taken, which creates the memory, and on top of it, a variable is assigned, which makes it a local variable after which the function is called and then the following logic statement is called to perform a lot of manipulation and work.
How Local Variable Works in Python?
- Unlike other variables local variable in Python also has a lot of areas for definition and declaration with a specific scope which makes its working possible.
- Local variables in Python are important when the variables, method, and method members want to execute a particular statement or function, which throws some error if some unwanted variable appears.
- Local variables are mostly defined within the function and are used within the function only; they hardly appear or are present outside the function or scope.
- Global variables behave completely opposite to local variables definition, and they can be present outside the function and scope is quite wide if in case there is no presence of local variable inside the function then the variable present outside as a global variable will be accessible for any kind of manipulation or logic implementation.
- Then there also comes some type of non-local variable, which appears to be a local variable, but they are not. These kinds of variable scope are mostly found either in nested function or nested loop statements.
- If in case any kind of change in the value of non-local variable comes for outside change, then it will for sure get reflected in the local variable.
- Formal arguments identifiers present in python also behave similar to local variables.
- If anyone tries to get the local variable outside the scope or function, it will raise an exception saying NameError Exception.
- Definition and declaration of local variables in Python work in this way again depending upon the requirement.
Examples of Local Variable in Python
Given below are the examples of Local Variable in Python:
Example #1
This program demonstrates the local variable when defined within the function where the variable is declared within function and then a statement followed by the function calling as shown in the output below.
Code:
def dinner_prep():
dine = 'Pizza_with_extra_topping'
print('Please have a pizza with extra_topping', dine)
dinner_prep()
Output:
Example #2
This program demonstrates the scenario where the pizza_name is the local variable defined for the dine_time() function and gives the error saying not accessible outside the of the function defined.
Code:
dine_time()
pizza
pizza_name
Output:
Explanation:
The above variable with pizza_name is defined after the local variable defined, which doesn’t allow the variable to access the other member or functionality, thus not providing the variable proper allocation and working.
Example #3
This program demonstrates the non-local variable with two scenarios depicting the changes and cases if the value lies in the scope or context or if in case the value doesn’t lie in the scope, then there will be the effect on the value as well, which is represented in the output below.
Code:
def outer_def(no_local_val):
p_1=150
print ('value in outer function:',p_1)
def with_no_local_val():
nonlocal p_1
p_2=65
def without_no_local_val():
p_2=78
if no_local_val==True:
with_no_local_val()
else:
without_no_local_val()
return p_1
print (outer_def(True))
print (outer_def(False))
Output:
Example #4
This program demonstrates the scenario where in case the value defined in global scope is defined in local scope, then what happens? The output represents the solution.
Code:
x_0=14
def m_func():
x_0=14
x_0=x_0*4
print ('x_0 function scope comes as: ', x_0)
m_func()
print ('x_0 function_scope_in_case_of_global_declr: ',x_0)
Output:
Explanation:
- In this program, we can see that the same variable is defined outside and inside the function, which makes solve the local variable and the variable logic first, then it comes out of the function and declares the global variable. Some people can take it otherwise in the sense since the variables declared are outside the scope, so it will come into the picture later.
- As it is a kind of clash between two types of variables, the one defined inside will get sorted first, and then it will entertain the global variable.
Example #5
This program demonstrates the scenario where the global and local both are defined in the same code snippet with the difference in their declaration where the variables have their own way to define, and then interpretation and comparison is made as shown in the output.
Code:
h_0 = 40
def f_a():
print('Present_Inside_f_a() : ', h_0)
def g_b():
h_0 = 40
print('Present_Inside_g_b() : ', h_0)
def h_p():
global h_0
h_0 = 86
print('Present_Inside_h_z() : ', h_0)
print('global : ',h_0)
f_a()
print('global : ',h_0)
g_b()
print('global : ',h_0)
h_p()
print('global : ',h_0)
Output:
Explanation:
Here the global variable is defined outside the scope followed by three functions where the local variable is declared with the global variable all together and having the nested presence within it as shown in the output. Always the priority is given to the inside and local variable within nested function followed by outside.
Conclusion
A local variable in Python plays a significant role in the sense it helps in making the function and the code snippet access to other member variables with manipulation simple and easy. In addition, local variables help in making the entire workflow with the global variable compatible and less complex. Also, the nested functions or statements play a very nice blend with local variables.
Recommended Articles
This is a guide to Local Variable in Python. Here we discuss the introduction, syntax, and working of a local variable in python along with different examples. You may also have a look at the following articles to learn more –