KEMBAR78
OOP Concepts Python with code refrences.pptx
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!

OOP Concepts Python with code refrences.pptx

  • 1.
    Object-Oriented Programming (OOP) Concepts inPython An Introduction to OOP Principles with Python Examples
  • 2.
    Introduction to OOPin 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.
  • 3.
    Key Concepts ofOOP • 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.
  • 4.
    Defining a Classand 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() • ```
  • 5.
    Encapsulation • - Protectsdata 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()) • ```
  • 6.
    Abstraction • - Hidesunnecessary 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()) • ```
  • 7.
    Inheritance • - Allowsa 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() • ```
  • 8.
    Polymorphism • - Allowsmethods 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()) • ```
  • 9.
    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.
  • 10.
    Questions & Discussion •Any questions? Let's discuss with live coding examples!