Scope of the variable means ‒ The part of the program in which a variable is accessible.
Local and
Global Variable
•
Scope
of the variable means ‒ The part of the program in which a variable is
accessible.
•
Lifetime
of variable means ‒ Duration for which the variable exists.
•
Definition of global variable: A variable
which is defined in the main body of a file is called a global variable. It will be visible throughout the file and also
inside any file which imports that file.
•
Only objects which are intended to be used globally, like functions and
classes, should be put in the global
namespace.
•
Definition of local variable: A variable which
is defined inside a function is local to that function. It is accessible from
the point at which it is defined until the end of the function and exists for
as long as the function is executing.
•
The parameter names in the function
definition behave like local variables, but they contain the values that we
pass into the function when we call it.
•
When we use the assignment operator (=)
inside a function, its default behaviour is to create a new local variable.
a=10 # a is Global
Variable
def My_Function(b):# b is
Local variable
c=30 # c is Local variable
print("b =
",b)
print("c=",c)
In [14]:
My_Function (20)
print("a =
",a)#a still exists
print("b =
",,b)#being Local variable, it does not exist outside function
print("c =
",,c)#being Local variable, it does not exist outside function
b = 20
C = 30
a = 10
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
Traceback (most recent call last)
NameError
Cell
In[14], line 3
1 My_Function(20)
2 print("a = ",a)#a still exists
‒‒‒‒> 3 print("b = ",b)#being Local variable, it
does not exist outside function
4 print("c = ",c)
NameError: name 'b' is not defined


1.
A variable which is declared inside a function is called as local variable.
2.
Accessibility of variable is only within the function in which it is declared.
3.
The local variable is created when the function(in which it is defined) starts
executing and it is destroyed when the execution is complete.
4.
It is preferred as local variables are more reliable. Because the values cannot
be changed outside the function.
1.
A variable that is declared outside the function is called global variable.
2.
Accessibility of variable is throughout the program. All functions in the
program can access the global variable.
3.
The global remains throughout the entire program execution.
4.
It is generally not preferred as any function can change the values of global
variable.
Python for Data Science: Chapter 2: Functions and Files : Tag: Computer Programming, Python, Data Science : - Python: Local and Global Variable
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation