Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

9.

Add two numbers in Python using the function

def add(a, b):


return a + b

price1 = float(input("Enter the price for 1st product: "))


price2 = float(input("Enter the price for 2nd product: "))

print("Total price to pay:", add(price1, price2))

The output is:


Enter the price for 1st product: 10
Enter the price for 2nd product: 25
Total price to pay: 35.0
17. Design class rectangle with data members length and breadth. create suitable methods of reading
and printing the area and perimeter of rectangle.
class Rectangle:
def __init__(self, length, breadth):
self.length = length
self.breadth = breadth

def display(self):
print ("Length of Rectangle is: ", self.length)
print ("Breadth of Rectangle is: ", self.breadth)

def area(self):
return (self.length*self.breadth)

def perimeter(self):
return (2*self.length + 2*self.breadth)

l = int (input("Enter the length of the Rectangle: "))


b = int (input("Enter the breadth of rectangle: "))
r1 = Rectangle(l , b)
print ("Rectangle object details are:")
r1.display()
print("")
print ("Area of Rectangle is: ", r1.area())
print("")
print ("The Perimeter of the Rectangle is: ", r1.perimeter())

Output:
Enter the length of the Rectangle: 10
Enter the breadth of rectangle: 5

Rectangle object details are:

Length of Rectangle is: 10

The breadth of Rectangle is: 5

Area of Rectangle is: 50

The perimeter of the Rectangle is: 30

19. calculate area of rectangle and area of square using method overloading.
Method Overloading in Python | Method Overloading Examples (besanttechnologies.com)

20. create class EMPLYEE with ID and NAME and display its contents.
class Employee: # Employee class
def __init__(self, name, employee_id):
self.name = name # Initialize the attributes
self.employee_id = employee_id
def get_details(self):
print(f"Name: {self.name}, ID: {self.employee_id}") # Print the details of the employee

class EmployeeManagement: # EmployeeManagement class


def __init__(self):
self.employees = [] # Initialize the list of employees
def add_employee(self, employee):
self.employees.append(employee) # Add an employee to the list
def display_all_employees(self): # Display all employees
for employee in self.employees:
employee.get_details()

employee1 = Employee("John", "001") # Create Employee objects


employee2 = Employee("Jane", "002")

employee_management = EmployeeManagement() # Create an EmployeeManagement object


employee_management.add_employee(employee1)
employee_management.add_employee(employee2)

print("All Employees:") # Display all employees


employee_management.display_all_employees()

Output:

All Employees:
Name: John, ID: 001
Name: Jane, ID: 002

You might also like