-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
Polymorphism means "many forms". In Python, it refers to the ability to use a single function name or method name in multiple ways depending on the object or class that is using it.
The same function can behave differently based on the input type:
print(len("Python")) # 6 (string)
print(len([1, 2, 3])) # 3 (list)
print(len({"a": 1, "b": 2})) # 2 (dictionary)
✅ The function len()
works on different data types but behaves according to the type passed.
Different classes can have the same method name, but with different behavior.
class Dog:
def speak(self):
print("Woof!")
class Cat:
def speak(self):
print("Meow!")
for animal in (Dog(), Cat()):
animal.speak()
✅ The method speak()
exists in both classes but behaves differently.
You can use polymorphism with inheritance by overriding methods in child classes.
class Animal:
def sound(self):
print("Animal sound")
class Cow(Animal):
def sound(self):
print("Moo")
class Snake(Animal):
def sound(self):
print("Hiss")
for a in (Cow(), Snake()):
a.sound()
✅ The method sound()
is defined in the parent class but overridden in each child class.
You can pass different objects to a single function and get different behavior.
def call_speak(animal):
animal.speak()
call_speak(Dog()) # Woof!
call_speak(Cat()) # Meow!
Code becomes more flexible and maintainable.
Same method names reduce confusion.
Makes use of abstraction and inheritance effectively.
Q1. Write a Python program to create two different classes with a method named describe()
that prints different outputs.
Q2. Write a Python program to use a for
loop to call the same describe()
method from different class instances.
Q3. Write a Python program to override a method from a parent class in two different child classes and demonstrate the outputs.
Q4. Write a Python program to create a base class Vehicle
with a start()
method, then override it in Car
and Bike
classes.
Q5. Write a Python program to use a single function to call start()
on different objects like Car
and Bike
, demonstrating polymorphism.
Q6. Write a Python program to create a class Bird
with a method fly()
, then override it in Eagle
and Penguin
classes with different behavior.
Q7. Write a Python program to store different animal objects in a list and call a common method like make_sound()
for each.
Q8. Write a Python program to create a base class with a greet()
method and override it in Student
and Teacher
classes.
Q9. Write a Python program to demonstrate polymorphism using a function that takes any object with a run()
method.
Q10. Write a Python program to build a simple game setup with a base class Character
and different character types like Warrior
, Mage
, etc., each using polymorphic methods such as attack()
.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms