CLASS AND
OBJECTS IN PYTHON
Understanding Object-Oriented Programming (OOP)
Basics in Python
INTRODUCTION TO OOP
IN PYTHON
• - Python supports Object-Oriented Programming
(OOP)
• - OOP is based on classes and objects
• - Helps in code reusability and organization
WHAT IS A CLASS?
• - A class is a blueprint for creating objects
• - Defines attributes (variables) and behaviors
(methods)
• - Example:
class Car:
def __init__(self, brand, model):
[Link] = brand
[Link] = model
WHAT IS AN OBJECT?
• - An object is an instance of a class
• - Objects have unique data but share the same
structure
• - Example:
my_car = Car("Toyota", "Corolla")
print(my_car.brand) # Output: Toyota
CONSTRUCTOR
(__INIT__ METHOD)
• - The `__init__` method is called when an object is
created
• - Used to initialize object attributes
• - Example:
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age
ATTRIBUTES AND
METHODS
• - Attributes: Variables that hold object data
• - Methods: Functions inside a class that define
behaviors
• - Example:
class Dog:
def __init__(self, name):
[Link] = name
def bark(self):
return "Woof!"
ACCESSING CLASS
ATTRIBUTES &
METHODS
• - Access attributes using `[Link]`
• - Call methods using `[Link]()`
• - Example:
my_dog = Dog("Buddy")
print(my_dog.name) # Output: Buddy
print(my_dog.bark()) # Output: Woof!
CLASS AND OBJECT
EXAMPLE
- Example combining attributes and methods:
class Student:
def __init__(self, name, grade):
[Link] = name
[Link] = grade
def display_info(self):
return f"{[Link]} is in grade {[Link]}"
student1 = Student("Alice", 10)
print(student1.display_info()) # Output: Alice is in
grade 10
ADVANTAGES OF USING
CLASSES AND OBJECTS
• - Code reusability
• - Better data organization
• - Easier debugging and maintenance
• - Encapsulation of data and behavior
SUMMARY
• - Class: A blueprint for objects
• - Object: An instance of a class
• - Constructor (`__init__`): Initializes object
attributes
• - Methods: Define object behaviors
THANK YOU!
• - Questions?