Polymorphism
Polymorphism
This happens when a function or operator behaves differently depending on the input
provided during compilation.
• Method Overloading: Defining multiple methods with the same name but different
parameters.
• class Calculator:
• def add(self, a, b):
• return a + b
•
• def add(self, a, b, c): # Overloading the add method
• return a + b + c
•
• calc = Calculator()
• print(calc.add(1, 2, 3)) # Output: 6
• Operator Overloading: Giving additional meaning to standard operators.
• class Point:
• def __init__(self, x, y):
• self.x = x
• self.y = y
•
• def __add__(self, other):
• return Point(self.x + other.x, self.y + other.y)
•
• p1 = Point(1, 2)
• p2 = Point(3, 4)
• result = p1 + p2
• print(result.x, result.y) # Output: 4, 6
This occurs when a subclass provides a specific implementation of a method that is already
defined in its parent class. It uses method overriding.
• Method Overriding: A child class redefines a method from its parent class.
• class Animal:
• def sound(self):
• print("Animal makes a sound")
•
• class Dog(Animal):
• def sound(self): # Overriding the sound method
• print("Dog barks")
•
• class Cat(Animal):
• def sound(self): # Overriding the sound method
• print("Cat meows")
•
• def make_sound(animal):
• animal.sound()
•
• dog = Dog()
• cat = Cat()
• make_sound(dog) # Output: Dog barks
• make_sound(cat) # Output: Cat meows
Advantages of Polymorphism
1. Code Reusability: Reduces duplication by enabling the same interface for different
implementations.
2. Flexibility: Allows methods to work with objects of various types.
3. Extensibility: Makes it easier to add new functionality without altering existing code.
Real-world Analogy
Consider a "remote control" (interface). It can work with multiple devices like a TV, AC, or
music system. The remote's behavior changes depending on the device it is connected to, but
the same interface (buttons) is used.
1. List
• Ordered: The items have a defined sequence, and you can access them using an
index (starting from 0).
• Mutable: You can change, add, or remove items after the list is created.
• Allows duplicates: A list can have multiple items with the same value.
Syntax:
Key Features:
Example:
2. Tuple
Syntax:
Key Features:
• Use parentheses ( ).
• Tuples are faster than lists because they are immutable.
• Useful for fixed collections of items.
Example:
3. Dictionary
• Unordered (Python < 3.7), Ordered (Python ≥ 3.7): The order of items is preserved
in Python 3.7 and later.
• Mutable: You can change, add, or remove items.
• No duplicate keys: Each key must be unique, but values can repeat.
Syntax:
Key Features:
Example:
Comparison Table
Quick Analogy:
• List: A shopping list — you can add, remove, or change items.
• Tuple: A person's date of birth — fixed and unchangeable.
• Dictionary: A contact book — you use a name (key) to find a phone number (value).
Apologies for the misunderstanding earlier! Here's a corrected table without examples as
points:
This table provides clear and distinct points for a 5-mark answer. Let me know if you’d like
further refinement!
Here’s a concise table for a 5-mark question on the difference between global and local
variables:
Key Point: Global variables are used program-wide, while local variables are confined to
specific functions.