1- #author: seema kumari patel
2- #OOPs concept - Python
3-
41---
52id : python-OOPs
63title : Python OOPs concept
74sidebar_label : Python OOPs concept # displays in sidebar
85sidebar_position : 19
6+ author : Seema Kumari Patel
7+ description : OOPs concept in Python
98tags :
109 [
1110 Python,
1211 Introduction of python,
1312 Python Syntax,
1413 Python Variables,
15- Python Operators,
16-
14+ Python Operators,
1715 ]
18-
1916---
2017
2118
@@ -31,26 +28,31 @@ Python supports the core principles of object-oriented programming, which are th
3128
3229📌 ** Use Case** : create a class - car
3330
31+ ``` python
3432class Car :
3533 def __init__ (self , brand , model ):
3634 self .brand = brand
3735 self .model = model
3836
3937 def start (self ):
4038 print (f " { self .brand} { self .model} is starting... " )
39+ ```
4140
42412 . Object - An instance of a class.
4342
4443📌 ** Use Case** : instantiate the variables
4544
45+ ``` python
4646my_car = Car(" Tesla" , " Model S" )
4747my_car.start() # Tesla Model S is starting...
48+ ```
4849
4950
50513 . Encapsulation - Hiding the internal details and only exposing necessary parts.
5152
5253📌 ** Use Case** : Not letting data to be accessed by other class
5354
55+ ``` python
5456class BankAccount :
5557 def __init__ (self , balance ):
5658 self .__balance = balance # private variable
@@ -60,12 +62,14 @@ class BankAccount:
6062
6163 def get_balance (self ):
6264 return self .__balance
65+ ```
6366
6467
65684 . Inheritance - One class can inherit from another.
6669
6770📌 ** Use Case** : car (parent) class is getting inherited by (child) ElectricCar
6871
72+ ``` python
6973class ElectricCar (Car ):
7074 def __init__ (self , brand , model , battery ):
7175 super ().__init__ (brand, model)
@@ -77,12 +81,14 @@ class ElectricCar(Car):
7781tesla = ElectricCar(" Tesla" , " Model X" , 100 )
7882tesla.start()
7983tesla.battery_info()
84+ ```
8085
8186
82875 . Polymorphism - Same function name, but different behavior depending on the object.
8388
8489📌 ** Use Case** : Different classes using single method for different purposes.
8590
91+ ``` python
8692class Dog :
8793 def speak (self ):
8894 return " Woof!"
@@ -94,12 +100,14 @@ class Cat:
94100pets = [Dog(), Cat()]
95101for pet in pets:
96102 print (pet.speak()) # Woof! / Meow!
103+ ```
97104
98105
991066 . Abstraction - Hiding implementation details, showing only essential features (using abc module).
100107
101108📌 ** Use Case** : Hiding low level details
102109
110+ ``` python
103111from abc import ABC , abstractmethod
104112
105113class Shape (ABC ):
@@ -116,5 +124,6 @@ class Circle(Shape):
116124
117125c = Circle(5 )
118126print (c.area()) # 78.5
127+ ```
119128
120129
0 commit comments