2. 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.
3. 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
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)
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
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.
13. • 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()