EXPERIMENT No. 7
Python Lab Practical Record · Python programming laboratory
Read with diagrams, search and self-test
7A. Class to represent a car
Aim
To write a Python class to represent a car with attributes like model, year and colour, including methods to start, stop and display car details.
Program (class_car.py)
# Exp 7A: Class to represent a car
class Car:
def __init__(self, model, year, color):
self.model = model
self.year = year
self.color = colordef start(self):
print("Car started")def stop(self):
print("Car stopped")def display(self):
print("Model :", self.model)
print("Year :", self.year)
print("Color :", self.color)c = Car("Toyota", 2024, "Red")
c.start()
c.display()
c.stop()Output
Car started Model : Toyota Year : 2024 Color : Red Car stopped
Result
A Car class with attributes and methods to start, stop and display details was created.
7B. Method overriding
Aim
To write a Python program that demonstrates method overriding.
Program (method_overriding.py)
# Exp 7B: Method overriding
class Animal:
def sound(self):
print("Animal makes sound")class Dog(Animal):
def sound(self):
print("Dog barks")a = Animal() a.sound() d = Dog() d.sound()
Output
Animal makes sound Dog barks
Result
Method overriding was demonstrated: the Dog class redefines the sound() method of the Animal class.
7C. Bank account class – deposit, withdraw and balance enquiry
Aim
To develop a Python class for a bank account that supports deposit, withdrawal and balance enquiry.
Program (bank_account.py)
# Exp 7C: Bank account class - deposit, withdraw and balance enquiry
class Bank:
def __init__(self):
self.balance = 1000def deposit(self, amount):
self.balance = self.balance + amountdef withdraw(self, amount):
if amount <= self.balance:
self.balance = self.balance - amount
else:
print("Insufficient balance")def display(self):
print("Balance =", self.balance)b = Bank() b.deposit(500) b.display() b.withdraw(200) b.display()
Output
Balance = 1500 Balance = 1300
Result
A Bank class supporting deposit, withdrawal and balance enquiry was developed.