python
  1. python-oops-concepts

Python OOPs Concepts

Python is a popular object-oriented programming language, which means that it is based on the principles of object-oriented programming (OOP). Object-oriented programming is a programming paradigm that is based on the concept of objects, which can contain data and methods. In this article, we will discuss some of the main OOPs concepts used in Python programming.

Syntax

Class Definition

class ClassName:
   # Statement-1
   .
   .
   .
   # Statement-N

Object Creation

object_name = ClassName()

Example

class Circle:
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius ** 2
    
circle1 = Circle(5)
print(circle1.area())

Output

78.5

Explanation

In this example, we have created a class named Circle, which has a constructor method that takes a radius as input and initializes it as an instance variable. The class also has a method called area, which calculates the area of the circle using the radius instance variable.

We then create an object of the Circle class with a radius of 5 and call the area method, which returns the area of the circle i.e. 78.5.

Use

Object-oriented programming allows us to create more complex programs by organizing data and methods into objects. OOPs concepts like encapsulation, inheritance, polymorphism, etc. make it easier to create programs that are more modular, reusable, and maintainable.

Important Points

  • Class is a blueprint for creating objects.
  • Object is an instance of a class.
  • init method is used to initialize instance variables.
  • Self parameter is a reference to the current instance of the class.
  • Instance variables are specific to an instance of a class.
  • Class variables are shared by all instances of a class.
  • Inheritance allows us to create a new class that is a modified version of an existing class.
  • Polymorphism allows us to use a single interface to represent different types of objects.

Summary

Python is a popular object-oriented programming language, which means that it is based on the principles of object-oriented programming (OOP). In this article, we discussed some of the main OOPs concepts used in Python programming, such as classes, objects, methods, encapsulation, inheritance, polymorphism, etc. By using these concepts, we can create more complex, modular, and maintainable programs.

Published on: