SlideShare a Scribd company logo
Classes and Objects in
Python
Object-Oriented Programming (OOP)
• What is OOP?
• Programming paradigm focusing on objects rather than just functions + data
• Objects encapsulate data (attributes) and functions (methods)
• Benefits of OOP
• Modularity, reusability, maintainability
• Abstraction, encapsulation, inheritance, polymorphism
Classes and Objects
• A class is a new data type
• Objects are instances of a class
class Shape:
def __init__(self, name, num_sides):
self.name = name
self.num_sides = num_sides
def describe(self):
print(f"I am a {self.num_sides}-sided {self.name}.")
triangle = Shape("Triangle", 3)
square = Shape("Square", 4)
triangle.describe() # Output: I am a 3-sided Triangle.
square.describe() # Output: I am a 4-sided Square.
Attributes
• Instance attributes
• Defined in the __init__ method
• Accessible through the self keyword
• Class attributes
• Defined outside of methods, shared among all instances
Methods
• Instance methods
• Defined with self parameter
• Access and modify instance attributes
• Class methods
• Defined with cls parameter
• Access and modify class attributes
• Static methods
• No self or cls parameter
• Standalone functions within the class
class Shape:
name = "Shape"
def __init__(self, n):
self.side = n
@classmethod
def get_class_name(cls):
return cls.name
@staticmethod
def add(x, y):
return x + y
@staticmethod
def multiply(x, y):
return x * y
# Usage
print(Shape.get_class_name()) # Output: Shape
Inheritance
• A class (child class, subclass) can inherit all methods and attributes of
another class (parent class, super class)
class Polygon(Shape):
def __init__(self, name, n, a):
super().__init__(name, n)
self.angle = a
def interior_angle(self):
print(f"The interior angle of a {self.name} is {self.angle} degrees.")
pol = Polygon("Hexagon", 6, 60)
pol.describe()
Polymorphism
• Objects of different classes can be treated as the same type
• Example: describe() method for different shapes
class Circle(Shape):
def __init__(self, radius):
super().__init__("Circle", 0)
self.radius = radius
def describe(self):
print(f"I am a circle with a radius of {self.radius} units.")
shapes = [Shape("Square", 4), Polygon("Pentagon", 5, 108), Circle(7)]
for shape in shapes:
shape.describe()
Encapsulation
• Hiding implementation details from
outside users
• Private attributes and methods
Prefixed with __ (double underscore)
• Non accessible from outside the class
• Protected attributes and methods
Prefixed with _ (single underscore)
• Accessible outside the class but meant
for internal use only
class Rectangle(Shape):
def __init__(self, length, width):
super().__init__("Rectangle", 4)
self.__length = length
self.__width = width
def get_area(self):
return self.__length * self.__width
def set_length(self, new_length):
self.__length = new_length
def set_width(self, new_width):
self.__width = new_width
Conclusion
• OOP is a powerful programming paradigm in Python
• Classes, objects, inheritance, polymorphism, and encapsulation are key
concepts
• OOP promotes modularity, reusability, and maintainability in code

More Related Content

Similar to OOP in Python Programming: Classes and Objects (20)

PPT
07slide.ppt
NuurAxmed2
 
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
PPTX
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
 
PPTX
OOP Concepts Python with code refrences.pptx
SofiMusic
 
PPTX
Object-Oriented Programming in Python.pptx
ssuser4ab3a2
 
PPTX
Python: Migrating from Procedural to Object-Oriented Programming
Damian T. Gordon
 
PPTX
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
PPTX
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
bandiranvitha
 
PPTX
object oriented programming(PYTHON)
Jyoti shukla
 
PPTX
PYTHON - OBJECT ORIENTED PROGRAMMING .pptx
SubashiniRathinavel
 
PPTX
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
PPTX
VTU Python Module 5 , Class and Objects and Debugging
rickyghoshiit
 
PPTX
Object Oriented Programming Class and Objects
rubini8582
 
PDF
Object-Oriented Programming System presentation
PavanKumarPathipati
 
PPTX
Introduction to Object Oriented Programming in Python.pptx
eduardocehenmu
 
PDF
Python - object oriented
Learnbay Datascience
 
PDF
1_7a6f85d03f132dcd9d7592bc4643be1c_MIT6_0001F16_Lec8.pdf
ID Bilişim ve Ticaret Ltd. Şti.
 
PPTX
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
07slide.ppt
NuurAxmed2
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
 
OOP Concepts Python with code refrences.pptx
SofiMusic
 
Object-Oriented Programming in Python.pptx
ssuser4ab3a2
 
Python: Migrating from Procedural to Object-Oriented Programming
Damian T. Gordon
 
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
bandiranvitha
 
object oriented programming(PYTHON)
Jyoti shukla
 
PYTHON - OBJECT ORIENTED PROGRAMMING .pptx
SubashiniRathinavel
 
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
VTU Python Module 5 , Class and Objects and Debugging
rickyghoshiit
 
Object Oriented Programming Class and Objects
rubini8582
 
Object-Oriented Programming System presentation
PavanKumarPathipati
 
Introduction to Object Oriented Programming in Python.pptx
eduardocehenmu
 
Python - object oriented
Learnbay Datascience
 
1_7a6f85d03f132dcd9d7592bc4643be1c_MIT6_0001F16_Lec8.pdf
ID Bilişim ve Ticaret Ltd. Şti.
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 

More from ssuserbad56d (11)

PPTX
Dictionaries in Python programming language
ssuserbad56d
 
PPTX
Introduction to functions in C programming language
ssuserbad56d
 
PPTX
Software Testing and JUnit and Best Practices
ssuserbad56d
 
PPT
search
ssuserbad56d
 
PPT
search
ssuserbad56d
 
PPT
Scaling Web Applications with Cassandra Presentation.ppt
ssuserbad56d
 
PPT
Cassandra
ssuserbad56d
 
PPT
Redis
ssuserbad56d
 
PPTX
Covered Call
ssuserbad56d
 
PDF
Lec04.pdf
ssuserbad56d
 
PDF
Project.pdf
ssuserbad56d
 
Dictionaries in Python programming language
ssuserbad56d
 
Introduction to functions in C programming language
ssuserbad56d
 
Software Testing and JUnit and Best Practices
ssuserbad56d
 
search
ssuserbad56d
 
search
ssuserbad56d
 
Scaling Web Applications with Cassandra Presentation.ppt
ssuserbad56d
 
Cassandra
ssuserbad56d
 
Covered Call
ssuserbad56d
 
Lec04.pdf
ssuserbad56d
 
Project.pdf
ssuserbad56d
 
Ad

Recently uploaded (20)

PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Ad

OOP in Python Programming: Classes and Objects

  • 2. Object-Oriented Programming (OOP) • What is OOP? • Programming paradigm focusing on objects rather than just functions + data • Objects encapsulate data (attributes) and functions (methods) • Benefits of OOP • Modularity, reusability, maintainability • Abstraction, encapsulation, inheritance, polymorphism
  • 3. Classes and Objects • A class is a new data type • Objects are instances of a class class Shape: def __init__(self, name, num_sides): self.name = name self.num_sides = num_sides def describe(self): print(f"I am a {self.num_sides}-sided {self.name}.") triangle = Shape("Triangle", 3) square = Shape("Square", 4) triangle.describe() # Output: I am a 3-sided Triangle. square.describe() # Output: I am a 4-sided Square.
  • 4. Attributes • Instance attributes • Defined in the __init__ method • Accessible through the self keyword • Class attributes • Defined outside of methods, shared among all instances
  • 5. Methods • Instance methods • Defined with self parameter • Access and modify instance attributes • Class methods • Defined with cls parameter • Access and modify class attributes • Static methods • No self or cls parameter • Standalone functions within the class class Shape: name = "Shape" def __init__(self, n): self.side = n @classmethod def get_class_name(cls): return cls.name @staticmethod def add(x, y): return x + y @staticmethod def multiply(x, y): return x * y # Usage print(Shape.get_class_name()) # Output: Shape
  • 6. Inheritance • A class (child class, subclass) can inherit all methods and attributes of another class (parent class, super class) class Polygon(Shape): def __init__(self, name, n, a): super().__init__(name, n) self.angle = a def interior_angle(self): print(f"The interior angle of a {self.name} is {self.angle} degrees.") pol = Polygon("Hexagon", 6, 60) pol.describe()
  • 7. Polymorphism • Objects of different classes can be treated as the same type • Example: describe() method for different shapes class Circle(Shape): def __init__(self, radius): super().__init__("Circle", 0) self.radius = radius def describe(self): print(f"I am a circle with a radius of {self.radius} units.") shapes = [Shape("Square", 4), Polygon("Pentagon", 5, 108), Circle(7)] for shape in shapes: shape.describe()
  • 8. Encapsulation • Hiding implementation details from outside users • Private attributes and methods Prefixed with __ (double underscore) • Non accessible from outside the class • Protected attributes and methods Prefixed with _ (single underscore) • Accessible outside the class but meant for internal use only class Rectangle(Shape): def __init__(self, length, width): super().__init__("Rectangle", 4) self.__length = length self.__width = width def get_area(self): return self.__length * self.__width def set_length(self, new_length): self.__length = new_length def set_width(self, new_width): self.__width = new_width
  • 9. Conclusion • OOP is a powerful programming paradigm in Python • Classes, objects, inheritance, polymorphism, and encapsulation are key concepts • OOP promotes modularity, reusability, and maintainability in code