Manual 11 - Classes & Objects (OOP) in python
Manual 11 - Classes & Objects (OOP) in python
Lab Manual 11
Important Instructions
1. Every student should have lab manual in the lab; otherwise, there will be no evaluation and
attendance.
2. The lab manual should be printed on both sides of the paper. Color printing is not required.
3. Those students who have Laptop must bring it in the lab.
4. Students should read the manual before coming to the lab.
5. Every student will have to submit assignments individually. Assignments after the due date
will not be accepted.
Experiment No. 11
Classes & Objects (OOP) in Python
Learning objectives
The main objective is to explore OOP basics in Python. After you’ve completed this manual, you'll be able to:
• Create classes, user-defined behaviors & magic/dunder functions.
• Instantiate objects and their attributes.
Task 0:
Create a python file with title “FirstName_Reg.No_Lab11.py” and define all the classes in this file.
[i.e., This file will have class definitions only]
Create another python file with title “main_Reg.No_Lab11.py” and create all the objects and demonstration
work in this file. [Import the above module in this file as directly available.]
Task 1:
Create a class named Tesla that has two Instance attributes: color & acceleration and a user defined behavior
named print_data(self). Since the attributes are instance attributes, these must be declared inside
Constructor(__init__(self,.)) as shown.
Instantiate an object from Tesla class and call the behavior print_data(self) and observe the output.
Output: ______________________________________________________________
Create __str__(self) magic/dunder method in above class to overload the print() method(Concept of Method
Overloading as discussed in class) as shown:
Note that the __str__ method must have return statement and the string to be returned.
Now instead of calling the print_data() function, print the c1 object directly as print(c1) and observe the
output.
Output: ______________________________________________________________
Task 2:
Create a class named StudentsData that has three instance attributes: name, Reg_No, & age (declare inside
Constructor) and override the default __str__ method to print these attributes directly through print()
command. Instantiate an object from StudentsData class and print it to observe the output.
Write your Code here:
Output:
Task 3:
Create a class named Polygon that has two Instance attributes: number_of_sides & side_lengths[] (an array
that will store the side lengths).
Create a user-defined behavior named inputSides(self). Observe & understand the code, recall the concepts
of List comprehension, dictionary etc.
Also, create a user-defined behavior named find_Area(self) as shown below.
Instantiate a triangle object from Polygon class and call the behaviors input_sides() and findArea() and
observe the output.
Output:
Similarly, Instantiate a rectangle object from Polygon class and call the behaviors input_sides() and
findArea().
Write your Code here:
Output:
Task4:
List down the names of all the concepts used in this Manual.