Polymorphism in Python, Object-Oriented Programming, Method Overloading, Method Overriding, Compile-time & Run-time Polymorphism in Python.
Polymorphism in Python
Polymorphism – Many ways/forms
Polymorphism is an important concept in programming. It refers to the use of a single type entity (method, operator, or object) to represent different types in different scenarios.
Example 1: Polymorphism in addition operator:
The + operator is used extensively in Python programs. But, it does not have a single usage.
For number data types, + operator is used to perform arithmetic addition operation.
num1 = 10
num2 = 20
print(num1+num2)num1 = 10.23
num2 = 20.45
print(num1+num2)
Similarly, for string data types, + operator is used to perform concatenation.
str1 = “Python ”
str2 = “Programming”
print(str1+str2)
Example 2: Function Polymorphism in Python:
There are some functions in Python which are compatible to run with multiple data types.
One such function is the len() function. It can run with many data types in Python.
print(len(“Python”))
print(len([“Python”, “Java”, “C”, “C++”, “Ruby”, “Kotlin”]))
print(len({“Windows”, “UNIX”, “Solaris”, “Linux”, “Macintosh”, “Android”}))
Example 3: Polymorphism with Class Methods
class Country1():
def name(self):
print (“It is India”)def capital(self):
print (“Capital city is New Delhi”)class Country2():
def name(self):
print (“It is USA”)def capital(self):
print (“Capital city is Washington, D.C.”)object1 = Country1()
object2 = Country2()object1.name()
object1.capital()
print(“”)
object2.name()
object2.capital()class Country1():
def name(self):
print (“It is India”)def capital(self):
print (“Capital city is New Delhi”)class Country2(Country1):
def name(self):
print (“It is USA”)def capital2(self):
print (“Capital city is Washington, D.C.”)myobject = Country2()
myobject.name()
myobject.capital()———————————
class Class1():
def programmer(self):
print(“I am a Python Programmer”)class Class2(Class1):
def programmer(self):
print(“I am a Java Programmer”)myobject = Class2()
myobject.programmer()
myobject.programmer()
Python Complete Tutorial
Python Video Tutorial
Python Programming Syllabus
Python Programming Quiz
Python Interview Questions for Fresher
1. Introduction to Python Programming Language
2. Download and Install Python
4. Python Keywords and Identifiers
9. Python Conditional Statements
11. Python Branching Statements
14. Python Data Structures – Lists
15. Python Data Structures – Sets
16. Python Data Structures – Tuples
17. Python Data Structures – Dictionaries
18. Python User Defined Functions
24. Python Object-Oriented Programming
25. Python Regular Expressions
Follow me on social media: