Introduction:
python is a object orintend programing language. In python we createing a class using the ‘class’ . we have key words followed by the class name and a colon. A Class is a user defined blueprint or prototypefrom which objects are careate. Class provides a means of bulding data and functions together.
we have step-by-step lines
- Class Defination
- Creating an instance
- Calling method
class defination:
Here i am creating a class using keyword class:
class MyClasa: x=5
OutPut:
<class ‘__main__.MyClass’>
Instalizer method:
This instalizer method are used to define the ‘__init__’ used in instalize the class. It is a special method to define the (object) class is created.It is an additional parameter for any attributes i want to initialize.
class MyClass: def __init__(self,attribute1, attribute2: self.attribute1 = attribute1 self.attribute2 = attribute2
Define method:
methods are many types, self is also a one method. methods are functions that belongs to the class. It would take ‘self ‘ as the first parameter.
class MyClass: def __init__(self, attribute1, attribute2): self.attribute1 = attribute1 self.attribute2 = attribute2 def display_attributes(self): print(f"Attribute 1: {self.attribute1}") print(f"Attribute 2: {self.attribute2}")
create an instance of the class:
I am creating an instance(object) of the class by calling the class name with the required parameters. It would connect all the class in one form.
class Car: def _init_(self, make, model, year): self.make = make self.model = model self.year = year def display_info(self): print(f"Car Make: {self.make}") print(f"Car Model: {self.model}") print(f"Car Year: {self.year}") my_car = Car("Toyota", "Corolla", 2021) my_car.display_info()
Output :
Car make : Toyota Car model : Corolla Car Year : 2021