Here’s a list of Python programs covering all the topics we discussed, from basic OOP concepts to advanced features like inheritance, overloading, and polymorphism.
๐นย
1๏ธโฃ Classes and Objects
๐ Program: Create a class Car
with attributes and methods.
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print(f"Car: {self.brand} {self.model}")
car1 = Car("Toyota", "Corolla")
car1.display_info()
2๏ธโฃ Class vs. Instance Attributes
๐ Program: Show difference between class and instance attributes.
class Employee:
company = "Google" # Class attribute
def __init__(self, name, salary):
self.name = name # Instance attribute
self.salary = salary
emp1 = Employee("Alice", 50000)
emp2 = Employee("Bob", 60000)
print(emp1.company, emp1.name) # Output: Google Alice
print(emp2.company, emp2.name) # Output: Google Bob
3๏ธโฃ Inheritance
๐ Program: Demonstrate Single Inheritance.
class Animal:
def speak(self):
return "Animal speaks"
class Dog(Animal):
def speak(self):
return "Woof! Woof!"
dog = Dog()
print(dog.speak()) # Output: Woof! Woof!
๐ Program: Multiple Inheritance.
class A:
def method_a(self):
return "Method from A"
class B:
def method_b(self):
return "Method from B"
class C(A, B):
pass
obj = C()
print(obj.method_a()) # Output: Method from A
print(obj.method_b()) # Output: Method from B
4๏ธโฃ Method Overloading
๐ Program: Simulate method overloading with default arguments.
class Math:
def add(self, a, b, c=0):
return a + b + c
math = Math()
print(math.add(2, 3)) # Output: 5
print(math.add(2, 3, 4)) # Output: 9
5๏ธโฃ Method Overriding
๐ Program: Child class overrides a method from the parent class.
class Parent:
def show(self):
print("This is Parent class")
class Child(Parent):
def show(self):
print("This is Child class")
obj = Child()
obj.show() # Output: This is Child class
6๏ธโฃ Polymorphism
๐ Program: Function behaving differently based on object type.
class Cat:
def speak(self):
return "Meow"
class Dog:
def speak(self):
return "Woof"
def animal_speak(animal):
print(animal.speak())
animal_speak(Cat()) # Output: Meow
animal_speak(Dog()) # Output: Woof
๐ Program: Polymorphism in Inheritance.
class Shape:
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(shape.area())
7๏ธโฃ File Handling
๐ Program: Read and write to a file.
with open("example.txt", "w") as file:
file.write("Hello, World!")
with open("example.txt", "r") as file:
content = file.read()
print(content) # Output: Hello, World!
๐ Program: Check if a file exists using os
module.
import os
if os.path.exists("example.txt"):
print("File exists!")
else:
print("File not found!")
8๏ธโฃ OS Module for File Operations
๐ Program: List files in the current directory.
import os
print(os.listdir(".")) # Output: List of files & folders
๐ Program: Create and delete a directory.
import os
os.mkdir("test_folder") # Create directory
os.rmdir("test_folder") # Remove empty directory
9๏ธโฃ List Comprehension
๐ Program: Generate squares of numbers using list comprehension.
squares = [x**2 for x in range(1, 6)]
print(squares) # Output: [1, 4, 9, 16, 25]
๐ Scope of Variables
๐ Program: Demonstrate Local and Global Scope.
x = 10 # Global variable
def my_function():
x = 5 # Local variable
print(x)
my_function() # Output: 5
print(x) # Output: 10
1๏ธโฃ1๏ธโฃ Python Modules
๐ Program: Import and use a built-in module (math
).
import math
print(math.sqrt(25)) # Output: 5.0
๐ Program: Create a user-defined module. 1๏ธโฃ Create a file mymodule.py
def greet(name):
return f"Hello, {name}!"
2๏ธโฃ Import and use in another script
import mymodule
print(mymodule.greet("Alice")) # Output: Hello, Alice!
1๏ธโฃ2๏ธโฃ Exception Handling
๐ Program: Handle division by zero.
try:
num = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
1๏ธโฃ3๏ธโฃ Decorators
๐ Program: Use a simple decorator.
def decorator_function(func):
def wrapper():
print("Before function execution")
func()
print("After function execution")
return wrapper
@decorator_function
def say_hello():
print("Hello, World!")
say_hello()
1๏ธโฃ4๏ธโฃ Generators
๐ Program: Create a generator function.
def my_generator():
for i in range(5):
yield i
gen = my_generator()
print(next(gen)) # Output: 0
print(next(gen)) # Output: 1
๐น Conclusion
This list of Python programs covers: โ
OOP Fundamentals (Classes, Objects, Attributes)
โ
Advanced OOP (Inheritance, Polymorphism, Overloading, Overriding)
โ
File Handling & OS Operations
โ
Exception Handling & Decorators
Would you like a full project implementing all these concepts? ๐
Leave a Reply