Creating a Class

InΒ 
Python,Β 
theΒ 
keywordΒ 
"class"
Β 
isΒ 
usedΒ 
toΒ 
defineΒ 
aΒ 
class.Β 
AllΒ 
theΒ 
classΒ 
methods
Β 
andΒ 
attributes
Β 
underΒ 
aΒ 
classΒ 
needΒ 
toΒ 
beΒ 
indented
.
class class_name:
    attributes
    def function_name():
object_name = class_name(attributes)
class Car:
    mfg_year = 2021
#this attribute will not change for the objects, so we can initialize it here
    def start(self):
        print("Car started")

Methods of a class

AΒ 
'self'
Β 
argumentΒ 
isΒ 
passedΒ 
whenΒ 
aΒ 
methodΒ 
isΒ 
created.Β 
ItΒ 
isΒ 
usedΒ 
toΒ 
link
Β 
anΒ 
objectΒ 
ofΒ 
theΒ 
classΒ 
toΒ 
theΒ 
methodsΒ 
ofΒ 
thatΒ 
class.
TheΒ 
nameΒ 
ofΒ 
theΒ 
objectΒ 
isΒ 
passedΒ 
asΒ 
"self"
.

Constructor

TheΒ 
specialΒ 
methodΒ 
withΒ 
theΒ 
nameΒ 
"
__init__
"Β 
isΒ 
theΒ 
classΒ 
Constructor
.Β 
WheneverΒ 
anΒ 
objectΒ 
isΒ 
created,Β 
thisΒ 
methodΒ 
isΒ 
automaticallyΒ 
calledΒ 
byΒ 
default,Β 
andΒ 
youΒ 
canΒ 
initialize
Β 
the
Β 
attributes
Β 
insideΒ 
theΒ 
constructor,Β 
asΒ 
differentΒ 
objectsΒ 
canΒ 
haveΒ 
differentΒ 
attributes.
class Class_Name:
    def __init__(self, attribute_1, attribute_2):
        self.attribute_1 = attribute_1
        self.attribute_2 = attribute_2
class Car:
    def __init__(self, name, color, price):
        self.name = name
        self.color = color
        self.price = price
honda = Car("Honda", "Red", 3500)
print(honda.name, honda.color, honda.price)