0% found this document useful (0 votes)
3 views22 pages

Python Programming

The document contains a series of Python programming exercises focused on object-oriented programming concepts such as inheritance, polymorphism, encapsulation, and method overloading. Each practical exercise provides a code example demonstrating the implementation of these concepts through classes and methods. The exercises cover a range of topics including class creation, method overriding, and the use of private attributes.

Uploaded by

rrbzbjbtfy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Python Programming

The document contains a series of Python programming exercises focused on object-oriented programming concepts such as inheritance, polymorphism, encapsulation, and method overloading. Each practical exercise provides a code example demonstrating the implementation of these concepts through classes and methods. The exercises cover a range of topics including class creation, method overriding, and the use of private attributes.

Uploaded by

rrbzbjbtfy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRACTICAL 75

Write a Python program to:Create a class Person with method


display() Create a class Student that inherits from Person Call the
method using Student object
class Person:
def display(self):
print("I am a Person")

class Student(Person):
pass
s = Student()
[Link]()
PRACTICAL 76
Write a Python program to: Create a class Animal with attribute name
Create a derived class Dog Initialize and display the name
class Animal:
def __init__(self, name):
[Link] = name

class Dog(Animal):
pass

d = Dog("Tommy")
print("Dog Name:", [Link])
PRACTICAL 77
Write a Python program to: Create class Grandparent with method
show1() Create class Parent inheriting from Grandparent Create class
Child inheriting from Parent Call all methods using Child object
class Grandparent:
def show1(self):
print("This is Grandparent")

class Parent(Grandparent):
def show2(self):
print("This is Parent")

class Child(Parent):
def show3(self):
print("This is Child")
c = Child()
c.show1()
c.show2()N
c.show3()

PRACTICAL 78
Write a Python program to: Create class Vehicle → Car →
ElectricCar Add methods in each class Demonstrate inheritance chain
class Vehicle:
def vehicle_type(self):
print("This is a vehicle")

class Car(Vehicle):
def car_type(self):
print("This is a car")

class ElectricCar(Car):
def battery_type(self):
print("This car uses an electric battery")

ec = ElectricCar()
ec.vehicle_type()
ec.car_type()
ec.battery_type()

PRACTICAL 79
Write a Python program to: Create class Teacher with method teach()
Create class Researcher with method research() Create class Professor
inheriting both Call both methods
class Teacher:
def teach(self):
print("Teaching students")

class Researcher:
def research(self):
print("Doing research work")

class Professor(Teacher, Researcher):


pass
p = Professor()
[Link]()
[Link]()

PRACTICAL 80
Write a Python program to: Create two classes A and B with
constructors Create class C inheriting both Call constructors using
super()
class A:
def __init__(self):
print("Constructor of A")
super().__init__()

class B:
def __init__(self):
print("Constructor of B")
super().__init__()

class C(A, B):


def __init__(self):
print("Constructor of C")
super().__init__()

obj = C()
PRACTICAL 81
Write a Python program to: Create class Shape Create derived classes
Circle and Rectangle Add methods to calculate area Demonstrate both
child classes
class Shape:
def area(self):
pass

class Circle(Shape):
def __init__(self, radius):
[Link] = radius

def area(self):
return 3.14 * [Link] * [Link]

class Rectangle(Shape):
def __init__(self, length, width):
[Link] = length
[Link] = width

def area(self):
return [Link] * [Link]

c = Circle(5)
r = Rectangle(4, 6)
print("Area of Circle:", [Link]())
print("Area of Rectangle:", [Link]())

PRACTICAL 82
Write a Python program to: Create class Employee with method
salary() Create subclasses Manager and Developer Override salary
method differently
class Employee:
def salary(self):
print("Employee salary")

class Manager(Employee):
def salary(self):
print("Manager Salary: 80000")
class Developer(Employee):
def salary(self):
print("Developer Salary: 60000")
m = Manager()
d = Developer()
[Link]()
[Link]()

PRACTICAL 83
Write a Python program to: Combine multiple + multilevel
inheritance Create classes A, B, C, D Show method access from all
parent classes
class A:
def method_a(self):
print("Method from class A")

class B(A):
def method_b(self):
print("Method from class B")

class C:
def method_c(self):
print("Method from class C")

class D(B, C):


def method_d(self):
print("Method from class D")

obj = D()
obj.method_a()
obj.method_b()
obj.method_c()
obj.method_d()
PRACTICAL 84
Write a Python program to: Create multiple inheritance classes Define
same method in parent classes Call method from child class Observe
which method is executed
class Parent1:
def show(self):
print("Show method from Parent1")

class Parent2:
def show(self):
print("Show method from Parent2")

class Child(Parent1, Parent2):


pass
c = Child()
[Link]()
PRACTICAL 85
Write a Python program to: Create a class Shape with method area()
Create classes Circle and Square that override area() Demonstrate
runtime polymorphism
class Shape:
def area(self):
print("Area of shape")

class Circle(Shape):
def area(self):
r=5
print("Area of Circle:", 3.14 * r * r)

class Square(Shape):
def area(self):
s=4
print("Area of Square:", s * s)

# Runtime Polymorphism
shapes = [Circle(), Square()]
for shape in shapes:
[Link]()

PRACTICAL 86
Method Overloading (Using Default Arguments) Write a Python
program to: Create a class Calculator Define a method add() that can
add 2 or 3 numbers Demonstrate polymorphism using default
arguments
class Calculator:
def add(self, a, b, c=0):
return a + b + c

calc = Calculator()

print("Sum of 2 numbers:", [Link](10, 20))


print("Sum of 3 numbers:", [Link](10, 20, 30))
PRACTICAL 87
Operator Overloading Write a Python program to: Create a class
Number Overload the + operator to add two objects Display the result
class Number:
def __init__(self, value):
[Link] = value

def __add__(self, other):


return Number([Link] + [Link])

def display(self):
print("Result:", [Link])

n1 = Number(10)
n2 = Number(20)

result = n1 + n2
[Link]()
PRACTICAL 88
Function Polymorphism Write a Python program to: Use built-in
functions like len() Apply it on string, list, and tuple Show how same
function behaves differently
text = "Hello"
lst = [1, 2, 3, 4]
tup = (10, 20, 30)

print("Length of string:", len(text))


print("Length of list:", len(lst))
print("Length of tuple:", len(tup))
PRACTICAL 89
Duck Typing Write a Python program to: Create two classes Bird and
Airplane Both have method fly() Write a function that calls fly() on
any object
class Bird:
def fly(self):
print("Bird is flying")

class Airplane:
def fly(self):
print("Airplane is flying")

def perform_fly(obj):
[Link]()
b = Bird()
a = Airplane()
perform_fly(b)
perform_fly(a)

PRACTICAL 90
Basic Encapsulation Write a Python program to: Create a class
Student Define private attributes name and marks Access them using
a public method
class Student:
def __init__(self, name, marks):
self.__name = name
self.__marks = marks

def display(self):
print("Name:", self.__name)
print("Marks:", self.__marks)

s = Student("Aman", 85)
[Link]()

PRACTICAL 91
Getter and Setter Methods Write a Python program to: Create a
class BankAccount Keep balance private Implement deposit() and
get_balance() methods
class BankAccount:
def __init__(self):
self.__balance = 0

def deposit(self, amount):


self.__balance += amount

def get_balance(self):
return self.__balance

acc = BankAccount()
[Link](1000)
print("Balance:", acc.get_balance())

PRACTICAL 92
Data Hiding Write a Python program to: Create a class Employee
Make salary private Try accessing it directly and via method
class Employee:
def __init__(self, salary):
self.__salary = salary

def get_salary(self):
return self.__salary

emp = Employee(50000)

# Direct access (will fail)


# print(emp.__salary)
# Correct way
print("Salary:", emp.get_salary())

PRACTICAL 93
Real-Life Encapsulation Write a Python program to: Create a class
Laptop Keep attributes like price private Provide methods to update
and display price securely solve the above questions in python
class Laptop:
def __init__(self, price):
self.__price = price

def set_price(self, price):


if price > 0:
self.__price = price
else:
print("Invalid price!")

def get_price(self):
return self.__price
lap = Laptop(50000)

print("Price:", lap.get_price())

lap.set_price(60000)
print("Updated Price:", lap.get_price())

You might also like