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

Your email address will not be published. Required fields are marked *