In this tutorial, we would learn about Classes and Objects in Python. Python is an Object-Oriented programming language. If you have ever learned any Object Oriented Programming language like Java or C# before then you have an idea how classes and objects work.
Those who never learned Object Oriented programming language before. In OOP we keep related things(Data and methods) together in one class. Class is like a blueprint of object and Object is run time entity of class. We use the class keyword to create a class.

Creating Class in Python:

We use the class keyword to create a class in Python. Here we will create a simple class with a single variable.

class FirstClass:
  num = 100

Here in this example, you can see we have created a class FirstClass using the class keyword. Then we created a variable class num with value 100 in it.

Creating Object in Python:

After creating a class our next step would be to create an object of that class. Check out this example here we will create an object of our FirstClass.

class FirstClass:
  num = 100

obj1 = FirstClass()
print(obj1.num)

The __init__() function in Python:

In the last example, we learned how we can create a simple class in Python. But if you ever learned any Object-oriented programming language you would have a clear idea about the concept called the constructor. The _init_() in the same way as a constructor in Python.

Every class in Python would have this function called _init_() and this method will execute automatically when we create an object of the class.

The main motive of this _init_() is to initialize all the variable of class.

Check this example to understand this _init_() function.

class Student:
  def __init__(self, name, age):
    self.name = name
    self.age = age

stu = Person("Ravi", 21)

print(stu.name)
print(stu.age)

Object Method in Python:

We can all create methods in our class and then we call these methods of the class in with the help of object of class check this example.

class Student:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def show(self):
    print("Hello" + self.name)

stu = Student("Ravi", 21)
stu.show()