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

PPTX
Classes_and_Objects_in_Pythonoopsconcept.pptx
PPTX
Python 2. classes- cruciql for students objects1.pptx
PPTX
Unit – V Object Oriented Programming in Python.pptx
PPTX
Object Oriented Programming Class and Objects
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
PPTX
OOP Concepts Python with code refrences.pptx
PPTX
OOP_with_Java_Beginner explanation .pptx
Classes_and_Objects_in_Pythonoopsconcept.pptx
Python 2. classes- cruciql for students objects1.pptx
Unit – V Object Oriented Programming in Python.pptx
Object Oriented Programming Class and Objects
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
OOP Concepts Python with code refrences.pptx
OOP_with_Java_Beginner explanation .pptx

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

PPTX
Objects and Classes in JAVA introduction
PPTX
Math-Bridge Edit Authoring
PPT
Deekshith-class and object.ppt bhkkggxbskoavsvvsjjxbxbbdh
PPTX
Introduction to OOP_Python_Lecture1.pptx
PPTX
Object Oriented Programming_Lecture 2
PPTX
Introduce oop in python
PPT
Overview of Object Oriented Programming using C++
PPTX
Slide - FPT University - PFP191 - Chapter 11 - Objects
PPTX
Object Oriented Programming -Single Inheritance.pptx
PPTX
Python Basic for Data science enthusiast
PPTX
Pi j2.2 classes
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
PPTX
Basic_concepts_of_OOPS_in_Python.pptx
PPTX
Classes and Objects _
PPTX
OOP Presentation.pptx
PPTX
OOP Presentation.pptx
PDF
Core java complete notes - PAID call at +91-814-614-5674
PDF
Core java complete notes - Contact at +91-814-614-5674
PDF
classes and objects.pdf
PPTX
12_oop templa.pptx
Objects and Classes in JAVA introduction
Math-Bridge Edit Authoring
Deekshith-class and object.ppt bhkkggxbskoavsvvsjjxbxbbdh
Introduction to OOP_Python_Lecture1.pptx
Object Oriented Programming_Lecture 2
Introduce oop in python
Overview of Object Oriented Programming using C++
Slide - FPT University - PFP191 - Chapter 11 - Objects
Object Oriented Programming -Single Inheritance.pptx
Python Basic for Data science enthusiast
Pi j2.2 classes
Unit 3-Classes ,Objects and Inheritance.pdf
Basic_concepts_of_OOPS_in_Python.pptx
Classes and Objects _
OOP Presentation.pptx
OOP Presentation.pptx
Core java complete notes - PAID call at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
classes and objects.pdf
12_oop templa.pptx
Ad

More from ssuserbad56d (11)

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

Recently uploaded (20)

PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Transform Your Business with a Software ERP System
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
ai tools demonstartion for schools and inter college
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Introduction to Artificial Intelligence
PDF
System and Network Administration Chapter 2
PPT
Introduction Database Management System for Course Database
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Nekopoi APK 2025 free lastest update
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Transform Your Business with a Software ERP System
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
2025 Textile ERP Trends: SAP, Odoo & Oracle
VVF-Customer-Presentation2025-Ver1.9.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Odoo POS Development Services by CandidRoot Solutions
Navsoft: AI-Powered Business Solutions & Custom Software Development
Upgrade and Innovation Strategies for SAP ERP Customers
ISO 45001 Occupational Health and Safety Management System
ai tools demonstartion for schools and inter college
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Operating system designcfffgfgggggggvggggggggg
Introduction to Artificial Intelligence
System and Network Administration Chapter 2
Introduction Database Management System for Course Database
Design an Analysis of Algorithms I-SECS-1021-03
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Nekopoi APK 2025 free lastest update

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