class Car:# Initializting Attributes of Class Cardef initializeAttribute(self, color, brand, model):self.color = colorself.brand = brandself.model = model# Creating Object called "MyCar"MyCar = Car()# Initializing Attributes of "MyCar"MyCar.initializeAttribute("OrchidWhite", "Honda", "Amaze")# Extracting Information from the "My Car" Objectprint(MyCar.color)print(MyCar.brand)print(MyCar.model)
OrchidWhite
Honda
Amaze
Initializing the attributes with init
class Car:# Initializting Attributes of Class Cardef__init__(self, color, brand, model):self.color = colorself.brand = brandself.model = model# Creating Object called "MyCar" along with attributesMyCar = Car("OrchidWhite", "Honda", "Amaze")# Creating Object called "HelensCar" along with attributesHelensCar = Car("Silver", "Tata", "Nano")# Extracting Information from the "My Car" Objectprint(f"My Car is a {MyCar.color}{MyCar.brand}{MyCar.model}")# Extracting Information from the "Helen's Car" Objectprint(f"Helen's Car is a {HelensCar.color}{HelensCar.brand}{HelensCar.model}")
My Car is a OrchidWhite Honda Amaze
Helen's Car is a Silver Tata Nano
Creation of Methods
class Car:# Initializting Attributes of Class Cardef__init__(self, color, brand, model):self.color = colorself.brand = brandself.model = modeldef repaint(self, new_color):self.color = new_color# Creating Object called "MyCar" along with attributesMyCar = Car("OrchidWhite", "Honda", "Amaze")# Extracting Information from the "My Car" Objectprint(f"My Car is a {MyCar.color}{MyCar.brand}{MyCar.model}")# My Car Repatined with Metallic BlueMyCar.repaint("MetallicBlue")# Extracting Information from the "My Car" Objectprint(f"After Repaint My Car is a {MyCar.color}{MyCar.brand}{MyCar.model}")
My Car is a OrchidWhite Honda Amaze
After Repaint My Car is a MetallicBlue Honda Amaze
Adding More Variation
class Car:# Initializting Attributes of Class Cardef__init__(self, color, brand, model, mileage=0):self.color = colorself.brand = brandself.model = modelself.mileage = mileagedef repaint(self, new_color):self.color = new_colordef add_mileage(self, miles_driven):self.mileage =self.mileage + miles_driven# Creating Object called "MyCar" along with attributesMyCar = Car("OrchidWhite", "Honda", "Amaze")# My Car Repatined with Metallic BlueMyCar.repaint("MetallicBlue")MyCar.add_mileage(100)# Creating Object called "MyCar" along with attributesHelensCar = Car("Silver", "Tata", "Nano", 100)# Helens Car Repatined with RedHelensCar.repaint("Red")HelensCar.add_mileage(100)# Extracting Information from the "My Car" Objectprint(f"Current Mileage of My Car is {MyCar.mileage}")# Extracting Information from the "Helens Car" Objectprint(f"Current Mileage of Helens Car is {HelensCar.mileage}")
Current Mileage of My Car is 100
Current Mileage of Helens Car is 200
class Car:# Initializting Attributes of Class Cardef__init__(self, color, brand, model, mileage=0, fuel_type="Petrol"):self.color = colorself.brand = brandself.model = modelself.mileage = mileageself.fuel_type = fuel_typedef repaint(self, new_color):self.color = new_colordef add_mileage(self, miles_driven):self.mileage += miles_drivendef describe_car(self):print(f"This car is a {self.color}{self.brand}{self.model} with {self.mileage} miles on it. It runs on {self.fuel_type}.")# Creating Object called "MyCar" along with attributesMyCar = Car("OrchidWhite", "Honda", "Amaze", fuel_type="Diesel")# Creating Object called "HelensCar" along with attributesHelensCar = Car("Silver", "Tata", "Nano", 100, "Electric")# Using the new methodsMyCar.repaint("MetallicBlue")MyCar.add_mileage(1500)HelensCar.add_mileage(500)# Describe cars again after updatesMyCar.describe_car()HelensCar.describe_car()
This car is a MetallicBlue Honda Amaze with 1500 miles on it. It runs on Diesel.
This car is a Silver Tata Nano with 600 miles on it. It runs on Electric.