Python for Data Science: Laboratory Programs in Python : Functions and Files : Python Programs using functions and classes
Ex 1. Write a Python program to compute area of circle using function and a return statement.
Solution :
def areaCircle(r):
PI= 3.14
result=PI*r*r
print("The result of circle: ")
return result
Output
In [12]:
areaCircle (10)
The result of circle:
Out[12]: 314.0

Ex 2. Passing a List
Python program
def fun(numbers):
numbers.append("DDD")
my_list = *"AAA","BBB", "CCC"+
fun(my_list)
print(my_list)
['AAA', 'BBB', 'CCC', 'DDD']

Ex 3. Local and Global 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

Ex 4. Simple Class:
ClassDemo.py
class Student:
name="AAA" ←Data Member
def display(self): ← Method
print ("The name of the student is ",self.name)
s1=Student() ←Object: an instance of a class
s1.display()

In above code, we have defined one class in which there is single data member and single method.
Note that by the statement
s1=Student()
the instance s1 is created.
Then using the instance of a class followed by dot operator which is followed by the name of the function, we can call the function defined in the class. The function display is called using the instance variable and dot operator.
Ex 5. Constructor
ClassDemo.py
class Student:
def _init_(self,name,marks):
self.name=name
self.marks=marks
def display(self):
print("Name: ",self.name)
print("marks: ",self.marks)
s1=Student("AAA",10)
s2=Student("BBB",20)
print("Student 1:")
s1.display()
print("Student 2:")
s2.display()

Ex 6
Write a Python program to create a class 'Rectangle'. Also find the area and perimeter of the rectangle when length and breadth values are initiated.
Solution :
Rectangle Demo.py
class Rectangle:
def _init_(self,breadth,length):
self.breadth=breadth
self.length=length
def area(self):
print(self.breadth *self.length)
def perimeter(self):
print(2*(self.breadth+self.length))
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=Rectangle(a,b)
print("Area of rectangle: ")
obj.area()
print("Perimeter of rectangle: ")
obj.perimeter()

Ex 7. Returning Instances
RectanglePoint.py
class Point:
x=0
y=0
class Rectangle:
def _init_(self,length, breadth): #constructor
self.length=length
self.breadth=breadth
def find_center_point(rect): #This method is outside the class
p=Point()
p.x=rect.length/2
p.y=rect.breadth/2
return p #returning instance of class Point
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=Rectangle(a,b)
pt=find_center_point(obj)
print("Point(x,y) = (",pt.x,",",pt.y.")")

Ex 8. Public and Private Memebers
StudentDemo.py
class Student:
def __init__(self,R,N): //These are the public members of class Student
self.Roll=R
self.Name=N
obj=Student(101,"Jayashree")
print("Roll No: ",obj.Roll) #Accessing public variable outside the class
print("Name:",obj.Name)
Output

Ex 9. Public and Private Memebers
StudentDemo.py
class Student:
def _init_(self,R,N):
self._Roll=R #private variable 'Roll'
self.___Name=N #private variable 'Name'
obj=Student(101,"Jayashree")
print("Roll No: ",obj. Roll) #trying to access private attribute
#outside the class and that will cause error

Program explanation: In above program,
1) There are two private variables ‒ Roll and Name. Note that to make these variables private they are written using the prefix as double underscore.
2) When we try to access these variables outside the class using the object of that class, it gives an Attribute error. Refer the screenshot for the output.
Ex 10. Printing Objects
MethodDemo.py
class Time:
hour=0
minute=0
second=0
def display(t): #Method defined inside the class
print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))
time=Time()
time.hour=11
time.minute=58
time.second=20
Time.display(time)#Method is called using object of class
11:58:20
Program explanation: In above program,
1. We have created a class named Time.
2. The display method is defined for this class. Note that this definition is inside the class.
3. The object or instance of the class Time is created in variable time.
4. Using the object time we invoke the method display with the help of dot operator. Hence we get the output as the values of data attributes ‒ Hour, minute and second.
InitDemo.py
class Time:
hour=0
minute=0
second=0
def __init__(self,hour, minute,second):
self.hour‒hour
self.minute=minute
self.second=second
def display(t):#Method defined inside the class
print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))
time=Time(11,58,20) #The method___init__ is called
Time.display(time)#Method is called using object of class
11:58:20
Program explanation: In above program,
1. We have defined a class named Time.
2. The data attributes of this class are hour, minute and second. The method of this class is display.
3. There is one special method defined in the class Time and that is ‒ the_init_ method.
4. The parameters that are passed to this method are self, hour, minute and second.
5. The __init __method is not called explicitly rather it is called implicitly when the object of this class is created. Thus the hour, minute and second values are i by the values passed to Time()
6. Finally using the display method, the values that are assigned to the data attributes are displayed.
Ex 12. Default arguments
class Time():
def __init__(self,hour=0,minute=0,second=0):
self.hour‒hour
self.minute=minute
self.second=second
def display(t): #Method defined inside the class
print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))
time=Time(11) #only first argument is passed explicitly
Time.display(time)#Method is called using object of class
11:00:00
• In the above program, the value of hour is overridden by the value which is passed to the Time(). The init method defines the default arguments as hour‒0, minute=0, second=0.
StrDemo.py
class Student:
def __init__(self,name,marks):
self.name=name
self.marks=marks
def__str__(self): #Method defined inside the class
return '(%s,%.2d)' % (self.name, self.marks)
st=Student("AAA",95) #The method init and str are called
print(st)#The return value of str method is displayed
Output
(AAA,95)
Program explanation: In above program,
1. We have defined a class Student
2. The data attributes are name and marks.
3. There are two special methods ‒ init and str.
4. The init method initializes the name and marks with the values.
5. The str method returns a string which helps us to display the values of data arguments ‒ name marks.
Ex:14
Write a Python program using the str method for Point class. Create a Point object and print it.
Solution:
class Point:
def __init__(self,x,y):
self.x=x
self.y=y
def ___str__(self):
return '(%d,%d)' % (self.x, self.y)
pt=Point(10,20)
print(pt)
Output
(10,20)
Python for Data Science: Laboratory Programs in Python : Tag: Computer Programming, Python, Data Science : Laboratory Programs in Python - Python Programs using functions and classes
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
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