Introduction

In this article, I will explain Python classes and objects.

Definition

Python allows object-oriented programming languages. A Class is like a” blueprint" (Class Example: human). An object is like an instance (object Example: man, woman, children). In Python the object is another number is the method.

Create a class

Class is declared one time. In Python class keyword is used to create a class.
Syntax
  1. class New_Class:
  2. # any code
Example
  1. class add:#class name
  2. a=10
  3. b=20
  4. c=a+b
  5. print(c)
  6. print(add)#call the class
Output
Create a class named add.
Python Classes And Objects

Create object

Objects are created many times. We create the class named “new_class” to create objects named “new_object”. In Object last line we must use this symbol ().
Syntax
  1. class New_class:
  2. #any code
  3. object=New_class()
Example
  1. class new_class:#class
  2. a="c# corner"
  3. b=20
  4. c=2.4
  5. new_object=new_class()#object
  6. print(new_object.a)
  7. print(new_object.b)
  8. print(new_object.c)
Output
Create a class and object.
Python Classes And Objects

Object method

Create a method in the “new_class” class. In Python the object is another number is the method. Objects can also contain methods.
Example
  1. class maths:#class name
  2. def add(self):#function name
  3. a=10
  4. b=20
  5. c=a*b
  6. print(c)
  7. obj =maths()
  8. obj.add()
Output
Create an object method.
Python Classes And Objects

The __init__ () function

The __init__ () function is used automatically every time in the class. That means for training we have to understand the built in __init__ () feature. All lessons have a function called __init__ (), which is usually finished while the elegance is being initiated. Use the__init__ () function to assign values to item properties.
Example
  1. class computer:#class name
  2. def __init__(self,computer_name,process,ram,storage):# __init__ ()function
  3. self.computer_name =computer_name
  4. self.process =process
  5. self.ram = ram
  6. self.storage = storage
  7. obj= computer("hp","i5","4gp","1tb")#object name
  8. print(obj.computer_name)
  9. print(obj.process)
  10. print(obj.ram)
  11. print(obj.storage)
Output
Create an __init__ () function.
Python Classes And Objects

The self-parameter

The self-parameter refers to the current instance of the class. If you give any parameter you must use a self-keyword.
Example
  1. class student:#class
  2. def __init__(self, name,tamil,english,maths,social,science):# __init__ and self keyword
  3. self.name = name
  4. self.tamil = tamil
  5. self.english = english
  6. self.maths= maths
  7. self.social=social
  8. self.science= science
  9. def total_mark(self):#self
  10. print("total mark ",end="")
  11. print("total mark ",
  12. self.tamil+self.english+self.maths+self.social+self.science)
  13. obj =student("surya", 36,83,45,67,67)#object
  14. obj1 =student("naveen", 36,67,85,57,97)
  15. obj2=student("vijay", 56,83,75,47,87)
  16. obj.total_mark()
  17. obj1.total_mark()
  18. obj2.total_mark()
Output
Self-keyword usage.
Python Classes And Objects

Modify object properties

Modify objects are used to change object properties.
Example
  1. class car:#class
  2. def __init__(self, car_name, mil):
  3. self.car_name = car_name
  4. self.mil = mil
  5. def myfunc(self):
  6. print(self.car_name,self.mil)
  7. car1= car("Lamborghini", 20)#object
  8. car2= car("ford",30)
  9. car3= car("bmw", 40)
  10. car2.mil=28#modify object
  11. car1.myfunc()
  12. car2.myfunc()
  13. car3.myfunc()
Output
Modify object.
Python Classes And Objects

Conclusion

In this article, we have seen Python classes and objects. I hope this article was useful to you. Thanks for reading!