SlideShare a Scribd company logo
Polymorphism in Python - Detailed
Explanation
• An overview of polymorphism with examples
and real-life applications.
What is Polymorphism?
• • Derived from Greek: 'Poly' (many) + 'Morph'
(forms)
• • Ability of an object to take multiple forms
• • Same function/method behaves differently
based on the object
Why is Polymorphism Important?
• • Flexibility and code reusability
• • Generalization: One function works for
multiple object types
• • Enhances readability and maintenance
Types of Polymorphism in Python
• 1. Duck Typing
• 2. Method Overriding (Runtime
Polymorphism)
• 3. Operator Overloading
Duck Typing Example
•Duck Typing allows objects to be used
interchangeably as long as they
•implement the required behavior (i.e., they have
the expected methods), regardless of the class they
belong to.
•This is why Python doesn't care about the
"type" of the object, but rather if it can perform a
certain action.
Duck Typing Example
class Cat:
def sound(self):
return 'Meow'
class Dog:
def sound(self):
return 'Bark'
def animal_sound(animal):
print(animal.sound())
animal_sound(Cat()) # Meow
animal_sound(Dog()) # Bark
Duck Typing Example
Write a program to illustrate duck typing in
Python using Banking as a scenario
//use google collab
if hasattr
operator overloading
Method Overriding Example
class Shape:
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
shape = Shape()
rectangle = Rectangle(5, 10)
print(shape.area()) # 0
print(rectangle.area()) # 50
Operator Overloading
Operator Overloading (also called operator ad-hoc
polymorphism) allows you to define or change the
behavior of built-in operators (like +, -, *, etc.) for custom
classes.
•Operator Overloading allows you to use operators (such
as +, -, *, etc.) to work with objects of your class, just like
they work with primitive data types like integers and strings.
Operator Overloading
What is Operator Overloading?
In Python, operator overloading allows you to redefine the
meaning of standard operators (+, -, *, etc.) when they are
used with objects of your own class. This means you can
customize how an operator behaves when applied to your
objects.
Operator Overloading
Operator Overloading Example
class Book:
def __init__(self, pages):
self.pages = pages
def __add__(self, other):
return self.pages + other.pages
book1 = Book(150)
book2 = Book(200)
print(book1 + book2) # 350
We use operator overloading when we want to
make custom classes behave more like built-in
types, especially when using operators such as
+, -, *, ==, etc.
When Do We Use Operator Overloading?
When Do We Use Operator Overloading?
When Do We Use Operator Overloading?
When Do We Use Operator Overloading?
Real-Life Example - Payment System
class Payment:
def pay(self):
pass
class CreditCard(Payment):
def pay(self):
return 'Paid using Credit Card'
class Paypal(Payment):
def pay(self):
return 'Paid using Paypal'
def make_payment(payment_method):
print(payment_method.pay())
make_payment(CreditCard())
make_payment(Paypal())
Benefits of Polymorphism
• Code Reusability: Same interface for multiple
object types
• Extensibility: Easy to add new classes
• Maintenance: Easier to maintain and scale
code
• Cleaner Code: Reduces complex if-else
structures

More Related Content

Similar to Polymorphism_in_Python_Programming_Language (20)

PPTX
java poly ppt.pptx
sukhpreetsingh295239
 
PPTX
Python presentation of Government Engineering College Aurangabad, Bihar
UttamKumar617567
 
PPTX
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
 
PPTX
Python introduction
Roger Xia
 
PPTX
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
PDF
OOP_UnitIII.pdf
SuyogSabale1
 
PDF
Lecture on Lecture on Python programming OP conceptsPolymorphism.pdf
waqaskhan428678
 
PPTX
Java(Polymorphism)
harsh kothari
 
PPTX
Polymorphism and its types
Suraj Bora
 
PPTX
polymorphism and virtual function
Bhanuprataparya
 
PDF
From Java to Python: beating the Stockholm syndrome
Javier Arias Losada
 
PPTX
Introduction to oop
colleges
 
PPTX
Presentation 4th
Connex
 
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
PDF
Python interview questions and answers
kavinilavuG
 
PPTX
Polymorphism.Difference between Inheritance & Polymorphism
huzaifaakram12
 
PPTX
oops.pptx
ASHWINAIT2021
 
PPTX
Introduction to Object Oriented Programming in Python.pptx
eduardocehenmu
 
PPTX
Polymorphism topic power point presentation li.pptx
MalligaarjunanN
 
PPTX
full defination of final opp.pptx
rayanbabur
 
java poly ppt.pptx
sukhpreetsingh295239
 
Python presentation of Government Engineering College Aurangabad, Bihar
UttamKumar617567
 
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
 
Python introduction
Roger Xia
 
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
OOP_UnitIII.pdf
SuyogSabale1
 
Lecture on Lecture on Python programming OP conceptsPolymorphism.pdf
waqaskhan428678
 
Java(Polymorphism)
harsh kothari
 
Polymorphism and its types
Suraj Bora
 
polymorphism and virtual function
Bhanuprataparya
 
From Java to Python: beating the Stockholm syndrome
Javier Arias Losada
 
Introduction to oop
colleges
 
Presentation 4th
Connex
 
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
Python interview questions and answers
kavinilavuG
 
Polymorphism.Difference between Inheritance & Polymorphism
huzaifaakram12
 
oops.pptx
ASHWINAIT2021
 
Introduction to Object Oriented Programming in Python.pptx
eduardocehenmu
 
Polymorphism topic power point presentation li.pptx
MalligaarjunanN
 
full defination of final opp.pptx
rayanbabur
 

More from abigailjudith8 (12)

PPTX
Endpoint Security - - IP layer Attacks and Vulnerabilities
abigailjudith8
 
PPTX
Endpoint Security - Network Security Infrastructure
abigailjudith8
 
PPTX
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
PPTX
Encapsulation_Python_Programming_Language
abigailjudith8
 
PPTX
Application and Data security and Privacy.pptx
abigailjudith8
 
PPTX
Cyber Hackathon Media Campaign Proposal (1).pptx
abigailjudith8
 
PPTX
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
abigailjudith8
 
PPTX
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
abigailjudith8
 
PPT
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
abigailjudith8
 
PPTX
SVM introduction for machine learning engineers
abigailjudith8
 
PPTX
Big Data for Pearson Btec Higher level 3.ppt
abigailjudith8
 
PPTX
INTRODUCTION TO PROGRAMMING and Python.pptx
abigailjudith8
 
Endpoint Security - - IP layer Attacks and Vulnerabilities
abigailjudith8
 
Endpoint Security - Network Security Infrastructure
abigailjudith8
 
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Encapsulation_Python_Programming_Language
abigailjudith8
 
Application and Data security and Privacy.pptx
abigailjudith8
 
Cyber Hackathon Media Campaign Proposal (1).pptx
abigailjudith8
 
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
abigailjudith8
 
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
abigailjudith8
 
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
abigailjudith8
 
SVM introduction for machine learning engineers
abigailjudith8
 
Big Data for Pearson Btec Higher level 3.ppt
abigailjudith8
 
INTRODUCTION TO PROGRAMMING and Python.pptx
abigailjudith8
 
Ad

Recently uploaded (20)

PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
Kubernetes - Architecture & Components.pdf
geethak285
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Ad

Polymorphism_in_Python_Programming_Language

  • 1. Polymorphism in Python - Detailed Explanation • An overview of polymorphism with examples and real-life applications.
  • 2. What is Polymorphism? • • Derived from Greek: 'Poly' (many) + 'Morph' (forms) • • Ability of an object to take multiple forms • • Same function/method behaves differently based on the object
  • 3. Why is Polymorphism Important? • • Flexibility and code reusability • • Generalization: One function works for multiple object types • • Enhances readability and maintenance
  • 4. Types of Polymorphism in Python • 1. Duck Typing • 2. Method Overriding (Runtime Polymorphism) • 3. Operator Overloading
  • 5. Duck Typing Example •Duck Typing allows objects to be used interchangeably as long as they •implement the required behavior (i.e., they have the expected methods), regardless of the class they belong to. •This is why Python doesn't care about the "type" of the object, but rather if it can perform a certain action.
  • 6. Duck Typing Example class Cat: def sound(self): return 'Meow' class Dog: def sound(self): return 'Bark' def animal_sound(animal): print(animal.sound()) animal_sound(Cat()) # Meow animal_sound(Dog()) # Bark
  • 7. Duck Typing Example Write a program to illustrate duck typing in Python using Banking as a scenario //use google collab if hasattr operator overloading
  • 8. Method Overriding Example class Shape: def area(self): return 0 class Rectangle(Shape): def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width shape = Shape() rectangle = Rectangle(5, 10) print(shape.area()) # 0 print(rectangle.area()) # 50
  • 9. Operator Overloading Operator Overloading (also called operator ad-hoc polymorphism) allows you to define or change the behavior of built-in operators (like +, -, *, etc.) for custom classes. •Operator Overloading allows you to use operators (such as +, -, *, etc.) to work with objects of your class, just like they work with primitive data types like integers and strings.
  • 10. Operator Overloading What is Operator Overloading? In Python, operator overloading allows you to redefine the meaning of standard operators (+, -, *, etc.) when they are used with objects of your own class. This means you can customize how an operator behaves when applied to your objects.
  • 12. Operator Overloading Example class Book: def __init__(self, pages): self.pages = pages def __add__(self, other): return self.pages + other.pages book1 = Book(150) book2 = Book(200) print(book1 + book2) # 350
  • 13. We use operator overloading when we want to make custom classes behave more like built-in types, especially when using operators such as +, -, *, ==, etc. When Do We Use Operator Overloading?
  • 14. When Do We Use Operator Overloading?
  • 15. When Do We Use Operator Overloading?
  • 16. When Do We Use Operator Overloading?
  • 17. Real-Life Example - Payment System class Payment: def pay(self): pass class CreditCard(Payment): def pay(self): return 'Paid using Credit Card' class Paypal(Payment): def pay(self): return 'Paid using Paypal' def make_payment(payment_method): print(payment_method.pay()) make_payment(CreditCard()) make_payment(Paypal())
  • 18. Benefits of Polymorphism • Code Reusability: Same interface for multiple object types • Extensibility: Easy to add new classes • Maintenance: Easier to maintain and scale code • Cleaner Code: Reduces complex if-else structures