Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions docs/python/python_oops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#author: seema kumari patel
#OOPs concept - Python

---
id: python-OOPs
title: Python OOPs concept
sidebar_label: Python OOPs concept #displays in sidebar
sidebar_position: 19
tags:
[
Python,
Introduction of python,
Python Syntax,
Python Variables,
Python Operators,

]

---


# Python OOPs

**OOP** is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOP, object has attributes thing that has specific data and can perform certain actions using methods.


Characteristics of OOP (Object Oriented Programming)
Python supports the core principles of object-oriented programming, which are the building blocks for designing robust and reusable software. The diagram below demonstrates these core principles:

1. Class - A blueprint for creating objects.

📌 **Use Case**: create a class - car

class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def start(self):
print(f"{self.brand} {self.model} is starting...")

2. Object - An instance of a class.

📌 **Use Case**: instantiate the variables

my_car = Car("Tesla", "Model S")
my_car.start() # Tesla Model S is starting...


3. Encapsulation - Hiding the internal details and only exposing necessary parts.

📌 **Use Case**: Not letting data to be accessed by other class

class BankAccount:
def __init__(self, balance):
self.__balance = balance # private variable

def deposit(self, amount):
self.__balance += amount

def get_balance(self):
return self.__balance


4. Inheritance - One class can inherit from another.

📌 **Use Case**: car (parent) class is getting inherited by (child) ElectricCar

class ElectricCar(Car):
def __init__(self, brand, model, battery):
super().__init__(brand, model)
self.battery = battery

def battery_info(self):
print(f"Battery: {self.battery} kWh")

tesla = ElectricCar("Tesla", "Model X", 100)
tesla.start()
tesla.battery_info()


5. Polymorphism - Same function name, but different behavior depending on the object.

📌 **Use Case**: Different classes using single method for different purposes.

class Dog:
def speak(self):
return "Woof!"

class Cat:
def speak(self):
return "Meow!"

pets = [Dog(), Cat()]
for pet in pets:
print(pet.speak()) # Woof! / Meow!


6. Abstraction - Hiding implementation details, showing only essential features (using abc module).

📌 **Use Case**: Hiding low level details

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

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

def area(self):
return 3.14 * self.radius ** 2

c = Circle(5)
print(c.area()) # 78.5


Loading