OOP Concepts Python with code refrences.pptx

SofiMusic 21 views 10 slides Mar 04, 2025
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes.

Helps in structuring the code for reusability, modularity, and scalability.

Python supports OOP along with procedural and functional programming.

Slide 2: Key Concepts of OOP

Class - Blueprint for creating...


Slide Content

Object-Oriented Programming (OOP) Concepts in Python An Introduction to OOP Principles with Python Examples

Introduction to OOP in Python - Object-Oriented Programming (OOP) is a paradigm using objects and classes. - Helps in structuring code for reusability and modularity. - Python supports OOP along with procedural and functional programming.

Key Concepts of OOP 1. Class - Blueprint for objects. 2. Object - An instance of a class. 3. Encapsulation - Restricting access to data. 4. Abstraction - Hiding implementation details. 5. Inheritance - Deriving new classes from existing ones. 6. Polymorphism - Ability to take multiple forms.

Defining a Class and Creating an Object Example: ```python class Car: def __ init __(self, brand, model): self.brand = brand self.model = model def display(self): print( f'Car : { self.brand } { self.model }') car1 = Car('Toyota', 'Corolla') car1.display() ```

Encapsulation - Protects data by restricting access. - Uses private and protected attributes. Example: ```python class BankAccount: def __init__(self, balance): self.__balance = balance def deposit(self, amount): self.__balance += amount def get_balance(self): return self.__balance account = BankAccount(1000) account.deposit(500) print(account.get_balance()) ```

Abstraction - Hides unnecessary details and exposes only relevant parts. Example: ```python from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def make_sound(self): pass class Dog(Animal): def make_sound(self): return 'Bark' dog = Dog() print(dog.make_sound()) ```

Inheritance - Allows a class to inherit properties and methods from another class. Example: ```python class Vehicle: def __init__(self, brand): self.brand = brand def show_brand(self): print('Brand:', self.brand) class Car(Vehicle): def __init__(self, brand, model): super().__init__(brand) self.model = model car = Car('Honda', 'Civic') car.show_brand() ```

Polymorphism - Allows methods to be used interchangeably between different classes. Example: ```python class Bird: def sound(self): return 'Some bird sound' class Sparrow(Bird): def sound(self): return 'Chirp' birds = [Sparrow(), Bird()] for bird in birds: print(bird.sound()) ```

Summary - Class & Object: Blueprint and instance. - Encapsulation: Hiding data for security. - Abstraction: Simplifying complex systems. - Inheritance: Code reuse via parent-child relationships. - Polymorphism: Flexibility in method usage.

Questions & Discussion Any questions? Let's discuss with live coding examples!