0% found this document useful (0 votes)
6 views

Class and Object

The document explains the concepts of classes and objects in programming, using the analogy of a blueprint for a car. A class serves as a blueprint from which multiple objects can be created, each with unique attributes, such as color and brand. It also details the use of the __init__ method and the self keyword to manage object-specific data in Python.

Uploaded by

kaviyakathrin261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Class and Object

The document explains the concepts of classes and objects in programming, using the analogy of a blueprint for a car. A class serves as a blueprint from which multiple objects can be created, each with unique attributes, such as color and brand. It also details the use of the __init__ method and the self keyword to manage object-specific data in Python.

Uploaded by

kaviyakathrin261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Class and objects

Fathima Sha Quadhari


Imagine a Class Like a Blueprint
• A class is like a blueprint or a recipe.
• For example:
You want to build a Car. Before making one, you create a blueprint for
what every car should have — like color, brand, model, and speed.
• That blueprint is your class.
Object Is the Real Thing
• An object is the actual car you build using the blueprint.
• You can build many cars (objects) from the same blueprint (class), like:
• Car1: red, Honda, 2020, speed 120
• Car2: blue, Toyota, 2021, speed 140
• Each car is an object, and they all follow the design of the class.
Object Is the Real Thing
• An object is the actual car you build using the blueprint.
• You can build many cars (objects) from the same blueprint (class), like:
• Car1: red, Honda, 2020, speed 120
• Car2: blue, Toyota, 2021, speed 140
• Each car is an object, and they all follow the design of the class.
Example
• # Class (blueprint)
• class Car:
• def __init__(self, color, brand, speed):
• self.color = color self.name
• self.brand = brand
• self.speed = speed

• def drive(self):
• print(f"{self.brand} is driving at {self.speed} km/h")

• # Objects (real cars)


• car1 = Car("Red", "Honda", 120)
• car2 = Car("Blue", "Toyota", 140)

• car1.drive() # Honda is driving at 120 km/h


• car2.drive() # Toyota is driving at 140 km/h
Example
• # Class (blueprint)
• class Car:
• def __init__(self, color, brand, speed):
• self.color = color
• self.brand = brand
• self.speed = speed

• def drive(self):
• print(f"{self.brand} is driving at {self.speed} km/h")

• # Objects (real cars)


• car1 = Car("Red", "Honda", 120)
• car2 = Car("Blue", "Toyota", 140)

• car1.drive() # Honda is driving at 120 km/h


• car2.drive() # Toyota is driving at 140 km/h
Example-2
• class Person:
• def __init__(self, name, age): # Constructor
• self.name = name
• self.age = age

• def greet(self):
• print(f"Hello, my name is {self.name} and I am {self.age} years old.")

• # Creating an object
• p1 = Person("Alice", 30)
• p1.greet()
Step-by-Step Explanation
• Here's what happens step-by-step:
• Python sees Person("Alice", 30) →
• So it calls the __init__ method.
• It creates a new object in memory (let's say at location X).
• It passes this new object as self, and "Alice" as name, and 30 as age.
• Inside __init__, it stores:self.name = "Alice"self.age = 30
• The object is stored in the variable p1.
Step-by-Step Explanation
•init__ assigns the values when an object is created.
•self holds these values inside the object so they can be
accessed later.
Think of it Like This:
Imagine you're filling out a name tag at an event:
1.__init__ is like the process of writing your name and age on the tag.
2.self is like the tag itself that keeps holding your name and age so
others can see it.
Super Simple Takeaway✅
• self makes sure that each object keeps its own data.✅ Without self, all
objects would lose their unique values!
Step-by-Step Explanation
• Step 3: The self Keywordself refers to the current object being
created.Think of it as saying "this object".Inside the class, we write
self.name, self.age to store the values inside the object.
• Step 4: Creating an Object
• p1 = Person("Alice", 30)
Step-by-Step Explanation
• Step 5: Using the Object
• p1.greet()
• It calls the method greet on object p1.Inside greet, self.name refers to
"Alice" and self.age refers to 30.So it prints:👉 Hello, my name is Alice
and I am 30 years old.
Step-by-Step Explanation

Term What it means


class Blueprint for creating objects
__init__ Constructor, runs when object is created
self Refers to the current object
object A real example of the class (like p1)
Step-by-Step Explanation
• p1 is the object (also called an instance) of the class Person.
• Here's how to think about it:
• p1 = Person("Alice", 30)
• Person → the class (blueprint)("Alice", 30) → data you pass to the
constructor (__init__)p1 → the object that now represents one
person with name "Alice" and age 30.
Step-by-Step Explanation
• Now you can use p1 to do things defined in the class, like
• p1.greet()
• This uses the greet method and prints:
• Hello, my name is Alice and I am 30 years old.
• create more objects like p2, p3 with different names and ages

You might also like