KEMBAR78
PYTHON - OBJECT ORIENTED PROGRAMMING .pptx
Object oriented programming
• 5 things in oops concept
1. Polymorphism
2. Encapsulation
3. Inheritance
4. Abstraction
5. Classes & Objects
Class
• Python is an Object Oriented Programming
language.
• Almost everything in Python is an Object , with its
properties and methods.
• Class is a set of variables and functions.
• class keyword is used to create Class.
Some Points to create python class
• Classes are created by keyword class.
• Attributes are the variables that belong to a class.
• Attributes are always public and can be accessed
using dot(.) operator.
• Eg: Myclass.MyAttribute
Create a class
• To create a class , use the keyword class:
Syntax : class definition
class ClassName:
#statement
Example,
class MyClass:
x=5
print(MyClass)
Create an Object
• An Object is an instance of a Class.
Syntax : Object definition
Obj=ClassName()
Print(obj.atrr)
• Example,
class MyClass:
x=5
p1 = MyClass()
print(p1.x)
Output:
5
The __init__() Function
• All classes have a function called __init__(),which is
always executed when the class is being initiated.
• Use the __init__() function to assign values to object
properties.
• Example,
class Person:
def __init__(self , name , age):
self.name=name
self.age=age
p1 = person(“Lizi”,21)
print(p1.name)
print(p1.age)
Output:
Lizi
21
The __str__() Function
• The __str__() function controls what should be
returned when the class object is represented
as a string
Example,
class Person:
def __init__(self , name , age):
self.name=name
self.age=age
p1 = person(“Lizi”,21)
Output:
<__main__.Person object at 0x15039e602100>
The self parameter
• The self parameter is a reference to the current
instance of the class , and is used to access
variables that belongs to the class.
Real time example
Ootty class
Person 1 Person 2
Object
Education Party
• Example,
class ooty:
def education(self):
print(“research for flowers”)
def boathouse(self):
print(“Let’s Party”)
Person 1=ooty()
Person 2=ooty()
Person 1.education()
Person 2.boathouse()
• Output:
research for flowers
Let’s Party

PYTHON - OBJECT ORIENTED PROGRAMMING .pptx

  • 1.
    Object oriented programming •5 things in oops concept 1. Polymorphism 2. Encapsulation 3. Inheritance 4. Abstraction 5. Classes & Objects
  • 2.
    Class • Python isan Object Oriented Programming language. • Almost everything in Python is an Object , with its properties and methods. • Class is a set of variables and functions. • class keyword is used to create Class.
  • 3.
    Some Points tocreate python class • Classes are created by keyword class. • Attributes are the variables that belong to a class. • Attributes are always public and can be accessed using dot(.) operator. • Eg: Myclass.MyAttribute
  • 4.
    Create a class •To create a class , use the keyword class: Syntax : class definition class ClassName: #statement Example, class MyClass: x=5 print(MyClass)
  • 5.
    Create an Object •An Object is an instance of a Class. Syntax : Object definition Obj=ClassName() Print(obj.atrr)
  • 6.
    • Example, class MyClass: x=5 p1= MyClass() print(p1.x) Output: 5
  • 7.
    The __init__() Function •All classes have a function called __init__(),which is always executed when the class is being initiated. • Use the __init__() function to assign values to object properties.
  • 8.
    • Example, class Person: def__init__(self , name , age): self.name=name self.age=age p1 = person(“Lizi”,21) print(p1.name) print(p1.age) Output: Lizi 21
  • 9.
    The __str__() Function •The __str__() function controls what should be returned when the class object is represented as a string
  • 10.
    Example, class Person: def __init__(self, name , age): self.name=name self.age=age p1 = person(“Lizi”,21) Output: <__main__.Person object at 0x15039e602100>
  • 11.
    The self parameter •The self parameter is a reference to the current instance of the class , and is used to access variables that belongs to the class.
  • 12.
    Real time example Oottyclass Person 1 Person 2 Object Education Party
  • 13.
    • Example, class ooty: defeducation(self): print(“research for flowers”) def boathouse(self): print(“Let’s Party”) Person 1=ooty() Person 2=ooty() Person 1.education() Person 2.boathouse()
  • 14.
    • Output: research forflowers Let’s Party