AI Basics with AK

Season 02 - Introduction to Python Programming

Arun Koundinya Parasa

Episode 15

Classes & Objects

Agenda

  • Initializing Attributes
  • Initializing Attributes with init
  • Creating Objects
  • Defining Methods
  • Objects Calling the Class Methods

Initializing the attributes

class Car:
  # Initializting Attributes of Class Car
  def initializeAttribute(self, color, brand, model):
    self.color = color
    self.brand = brand
    self.model = model

# Creating Object called "MyCar"
MyCar = Car()

# Initializing Attributes of "MyCar"
MyCar.initializeAttribute("OrchidWhite", "Honda", "Amaze")

# Extracting Information from the "My Car" Object
print(MyCar.color)
print(MyCar.brand)
print(MyCar.model)
OrchidWhite
Honda
Amaze

Initializing the attributes with init

class Car:
  # Initializting Attributes of Class Car
  def __init__(self, color, brand, model):
    self.color = color
    self.brand = brand
    self.model = model

# Creating Object called "MyCar" along with attributes
MyCar = Car("OrchidWhite", "Honda", "Amaze")

# Creating Object called "HelensCar" along with attributes
HelensCar = Car("Silver", "Tata", "Nano")

# Extracting Information from the "My Car" Object
print(f"My Car is a {MyCar.color} {MyCar.brand} {MyCar.model}")

# Extracting Information from the "Helen's Car" Object
print(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 Car
  def __init__(self, color, brand, model):
    self.color = color
    self.brand = brand
    self.model = model

  def repaint(self, new_color):
    self.color = new_color


# Creating Object called "MyCar" along with attributes
MyCar = Car("OrchidWhite", "Honda", "Amaze")

# Extracting Information from the "My Car" Object
print(f"My Car is a {MyCar.color} {MyCar.brand} {MyCar.model}")

# My Car Repatined with Metallic Blue
MyCar.repaint("MetallicBlue")

# Extracting Information from the "My Car" Object
print(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 Car
  def __init__(self, color, brand, model, mileage=0):
    self.color = color
    self.brand = brand
    self.model = model
    self.mileage = mileage

  def repaint(self, new_color):
    self.color = new_color

  def add_mileage(self, miles_driven):
    self.mileage = self.mileage + miles_driven

# Creating Object called "MyCar" along with attributes
MyCar = Car("OrchidWhite", "Honda", "Amaze")

# My Car Repatined with Metallic Blue
MyCar.repaint("MetallicBlue")
MyCar.add_mileage(100)

# Creating Object called "MyCar" along with attributes
HelensCar = Car("Silver", "Tata", "Nano", 100)

# Helens Car Repatined with Red
HelensCar.repaint("Red")
HelensCar.add_mileage(100)

# Extracting Information from the "My Car" Object
print(f"Current Mileage of My Car is {MyCar.mileage}")

# Extracting Information from the "Helens Car" Object
print(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 Car
  def __init__(self, color, brand, model, mileage=0, fuel_type="Petrol"):
    self.color = color
    self.brand = brand
    self.model = model
    self.mileage = mileage
    self.fuel_type = fuel_type

  def repaint(self, new_color):
    self.color = new_color

  def add_mileage(self, miles_driven):
    self.mileage += miles_driven

  def 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 attributes
MyCar = Car("OrchidWhite", "Honda", "Amaze", fuel_type="Diesel")

# Creating Object called "HelensCar" along with attributes
HelensCar = Car("Silver", "Tata", "Nano", 100, "Electric")

# Using the new methods
MyCar.repaint("MetallicBlue")
MyCar.add_mileage(1500)
HelensCar.add_mileage(500)

# Describe cars again after updates
MyCar.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.

THANK YOU