Lab 8 Submission
Lab 8 Submission
Section ECSc-2
1. Create a class for a Student with attributes name, age, and grade. Implement a
method to display student information.
SOL:
class Student:
def __init__(self,name,age,grade):
self.name=name
self.age=age
self.grade=grade
def display(self):
print('Hi ',self.name)
print('Your age is: ',self.age)
print('Your grade is: ',self.grade)
Student('Arnav',20,'A').display()
OUTPUT:-
2. Write a Python program to create a Student class with details given in previous
question. Call the method talk() to display student details.
SOL:
class Student:
def __init__(self,name,age,grade):
self.name=name
self.age=age
self.grade=grade
def talk(self):
print('Hi ',self.name)
print('Your age is: ',self.age)
print('Your grade is: ',self.grade)
Student('Arnav',20,'A').talk()
OUTPUT:-
OUTPUT:-
SOL:-
class BankAccount:
def __init__(self,account_number,balance):
self.account_number=account_number
self.balance=balance
def deposit(self,amount):
self.balance+=amount
print('Your new balance is: ',self.balance)
def withdraw(self,amount):
if self.balance>=amount:
self.balance-=amount
print('Your new balance is: ',self.balance)
else:
print('Insufficient balance')
def display(self):
print('Your account number is: ',self.account_number)
print('Your balance is: ',self.balance)
BankAccount(123456789,10000).deposit(1000)
Print(“Arnav Singhal 2330293”)
5. Write a Python programme with a class which has attributes like title,actor
and actress in its constructor and another method which prints
all these properties.
(a) Prompt the user to enter all this information and add this info in a
list.
(b) Keep the prompt open add all this information until user reply in
’No’ and then display all this information of the list.
SOL:-
class Movie:
def __init__(self,title,actor,actress):
self.title=title
self.actor=actor
self.actress=actress
def display(self):
print('Title:',self.title)
print('Actor:',self.actor)
print('Actress:',self.actress)
title = input('Enter title: ')
actor = input('Enter actor: ')
actress = input('Enter actress: ')
Movie(title,actor,actress).display()
Print(“Arnav Singhal 2330293”)
OUTPUT:-
6. Implement a class hierarchy for different shapes (e.g., Circle, Square,
Triangle). Include methods to calculate area and perimeter for each
shape.
SOL:-
import math
class Shape:
def area(self): pass
def perimeter(self): pass
class Circle(Shape):
def __init__(self, r): self.r = r
def area(self): return math.pi * self.r**2
def perimeter(self): return 2 * math.pi * self.r
class Square(Shape):
def __init__(self, s): self.s = s
def area(self): return self.s**2
def perimeter(self): return 4 * self.s
class Triangle(Shape):
def __init__(self, a, b, c): self.a, self.b, self.c = a, b, c
def area(self):
s = (self.a + self.b + self.c) / 2
return math.sqrt(s * (s - self.a) * (s - self.b) * (s - self.c))
def perimeter(self): return self.a + self.b + self.c
c = Circle(5)
s = Square(4)
t = Triangle(3, 4, 5)
print("Circle area:", c.area())
print("Circle perimeter:", c.perimeter())
print("Square area:", s.area())
print("Square perimeter:", s.perimeter())
print("Triangle area:", t.area())
print("Triangle perimeter:", t.perimeter())
Print(“Arnav Singhal 2330293”)
OUTPUT:-
SOL:-
class Book:
def __init__(self, title, author, price):
self.title = title
self.author = author
self.price = price
def details(self):
return f"Title: {self.title}, Author: {self.author}, Price: {self.price}"
book1 = Book("Python Basics", "John Doe", 650)
book1.details()
Print(“Arnav Singhal 2330293”)
OUTPUT:-
8. Design a class hierarchy for different vehicles (e.g., Car, Truck, Motorcycle).
Include methods to calculate mileage and display vehicle information.
SOL:-
class Vehicle:
def __init__(self,model, year):
self.model = model
self.year = year
def info(self):
return f"{self.year} {self.model}"
class Car(Vehicle):
def __init__(self,model, year, doors):
super().__init__(model, year)
self.doors = doors
def info(self):
return f"{super().info()}, {self.doors} doors"
class Truck(Vehicle):
def __init__(self,model, year, capacity):
super().__init__(model, year)
self.capacity = capacity
def info(self):
return f"{super().info()}, {self.capacity} lbs"
car = Car("Toyota", 2022, 4)
truck = Truck("Ford", 2021, 2000)
print(car.info())
print(truck.info())
Print(“Arnav Singhal 2330293”)
OUTPUT:-
9. Define a class for a TodoList with methods to add, remove, and display
tasks. Ensure tasks are stored as objects of another class.
SOL:-
class ToDoList:
def __init__(self):
self.tasks = []
# Adding tasks
todo_list.add_task("Complete Python project")
todo_list.add_task("Read a book")
# Display tasks
todo_list.display_tasks()
# Removing a task
todo_list.remove_task("Read a book")
SOL:-
class Account:
def __init__(self, num, bal):
self.num = num
self.bal = bal
class Bank:
def __init__(self):
self.accounts = []
def add(self, num, bal):
self.accounts.append(Account(num, bal))
def remove(self, num):
self.accounts = [acc for acc in self.accounts if acc.num != num]
def total(self):
return sum(acc.bal for acc in self.accounts)
b = Bank()
b.add(1, 100)
b.add(2, 200)
print(b.total())
b.remove(1)
print(b.total())
OUTPUT:-
11. Create a class for a Product with attributes name, price, and quantity.
Implement methods to calculate total cost and update quantity.
SOL:
class Product:
def __init__(self, name, price, qty):
self.name = name
self.price = price
self.qty = qty
def total(self):
return self.price * self.qty
def update(self, new_qty):
if new_qty >= 0:
self.qty = new_qty
p = Product("Book", 10, 5)
print(p.total())
p.update(3)
print(p.total())
OUTPUT:
12. Design a class hierarchy for different employees (e.g., Manager, Developer,
Tester). Include methods to calculate salary and display employee details.
SOL:
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def details(self):
return f"{self.name}, {self.salary}"
class Manager(Employee):
def __init__(self, name, salary, bonus):
super().__init__(name, salary)
self.bonus = bonus
def details(self):
return f"{super().details()}, Bonus: {self.bonus}"
class Developer(Employee):
def __init__(self, name, salary, language):
super().__init__(name, salary)
self.language = language
def details(self):
return f"{super().details()}, {self.language}"
print(m.details())
print(d.details())
OUTPUT:
• Conclusion:
Faculty Signature