0% found this document useful (0 votes)
10 views3 pages

Class and Object

The document contains Python code that defines three classes: Box, Cars, and Book. Each class includes methods to calculate the volume of a box, display car information, and describe a book's details, respectively. The code also prompts users for input to create instances of these classes and display the relevant information.
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)
10 views3 pages

Class and Object

The document contains Python code that defines three classes: Box, Cars, and Book. Each class includes methods to calculate the volume of a box, display car information, and describe a book's details, respectively. The code also prompts users for input to create instances of these classes and display the relevant information.
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

#Q.

1)Volume of box by using class and object

class Box:

def __init__(self, l, b, h):

self.l = l

self.b = b

self.h = h

def volume(self):

self.vol = self.l * self.b * self.h

print("Volume of box : {:.2f}".format(self.vol))

dimensions = input("Enter length, breadth, height of box : ").split()

l, b, h = map(float,dimensions)

b1 = Box(l, b, h)

b1.volume()

#Q.2)Information of cars

class Cars:

def __init__(self, make="Tesla", model = "Roadster", year = "2022"):

self.make = make

self.model = model

self.year = year

def car_info(self):

print("Information of car......")
print("""Car Company <{0}>

car model <{1}>

car yera <{2}>\n\n""".format(self.make,self.model,self.year))

c1 = Cars()

c1.car_info()

c2 = Cars("Ford", "Mustang", 1969)

c2.car_info()

make = input("Enter car company name : ")

model = input("Enter model number : ")

year = int(input("Enter year of making car model : "))

c3 = Cars(make, model, year)

c3.car_info()

#Q.3)Enter information about any book

class Book:

def __init__(self,name, author, pages):

self.name = name

self.author = author

self.pages = pages

def book_desc(self):

print(f"""Information of book :

Name of book = {name}

Author of Book = {author}


no of pages in book = {pages}""")

attr = input("Enter name and author of any book : ").split()

name, author = attr

pages = int(input("Enter number of pages in that book : "))

b1 = Book(name, author, pages)

b1.book_desc()

You might also like