hierarchical_inheritance.py PYTHON
1 Hierarchical Inheritance

Multiple derived classes inherit from a single base class.

class Animal:
    def __init__(self, name):
        self.name = name
    def sound(self):
        print(f"{self.name} makes a sound")

class Dog(Animal):
    def bark(self):
        print(f"{self.name}: Woof!")

class Cat(Animal):
    def meow(self):
        print(f"{self.name}: Meow!")

dog = Dog("Buddy")
cat = Cat("Whiskers")
dog.sound()
dog.bark()
cat.sound()
cat.meow()
Output Buddy makes a sound | Buddy: Woof! | Whiskers makes a sound | Whiskers: Meow!

Key Concepts

operator_overloading.py PYTHON
2 Operator Overloading

Redefine built-in operators for custom classes using magic methods.

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)
    def __str__(self):
        return f"({self.x}, {self.y})"

p1 = Point(2, 3)
p2 = Point(4, 5)
p3 = p1 + p2
print(f"p1 = {p1}")
print(f"p2 = {p2}")
print(f"p1 + p2 = {p3}")
Output p1 = (2, 3) | p2 = (4, 5) | p1 + p2 = (6, 8)

Key Concepts

abstract_class.py PYTHON
3 Abstract Class

Cannot be instantiated; forces child classes to implement abstract methods.

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, r):
        self.r = r
    def area(self):
        return 3.14 * self.r ** 2

class Square(Shape):
    def __init__(self, s):
        self.s = s
    def area(self):
        return self.s ** 2

c = Circle(5)
s = Square(4)
print(f"Circle area: {c.area()}")
print(f"Square area: {s.area()}")
Output Circle area: 78.5 | Square area: 16

Key Concepts

gui_basic.py PYTHON
4 GUI (Basic Concept)

Graphical user interface using Tkinter for interactive applications.

from tkinter import *

def show():
    result.config(text=f"Hello, {entry.get()}!")

root = Tk()
root.title("Simple GUI")
root.geometry("300x150")

Label(root, text="Enter Name:").pack(pady=10)
entry = Entry(root, width=20)
entry.pack()
Button(root, text="Submit", command=show).pack(pady=10)
result = Label(root, text="")
result.pack()

root.mainloop()
Output Window with entry field and button. Displays: "Hello, [name]!" when submitted

Key Concepts

calculator_gui.py PYTHON
5 Calculator using GUI

Simple calculator with 2 input boxes and basic arithmetic operations (Add, Sub, Mul, Div).

from tkinter import *

def add():
    result.config(text="Result: " + str(float(num1.get()) + float(num2.get())))

def subtract():
    result.config(text="Result: " + str(float(num1.get()) - float(num2.get())))

def multiply():
    result.config(text="Result: " + str(float(num1.get()) * float(num2.get())))

def divide():
    try:
        result.config(text="Result: " + str(float(num1.get()) / float(num2.get())))
    except ZeroDivisionError:
        result.config(text="Error: Division by zero!")

root = Tk()
root.title("Calculator")
root.geometry("300x250")

# Input boxes
Label(root, text="Number 1:", font=("Arial", 12)).pack(pady=5)
num1 = Entry(root, font=("Arial", 14), width=20)
num1.pack(pady=5)

Label(root, text="Number 2:", font=("Arial", 12)).pack(pady=5)
num2 = Entry(root, font=("Arial", 14), width=20)
num2.pack(pady=5)

# Operation buttons
btn_frame = Frame(root)
btn_frame.pack(pady=10)

Button(btn_frame, text="Add", width=6, command=add).grid(row=0, column=0, padx=5)
Button(btn_frame, text="Sub", width=6, command=subtract).grid(row=0, column=1, padx=5)
Button(btn_frame, text="Mul", width=6, command=multiply).grid(row=0, column=2, padx=5)
Button(btn_frame, text="Div", width=6, command=divide).grid(row=0, column=3, padx=5)

# Result label
result = Label(root, text="Result: ", font=("Arial", 14), fg="blue")
result.pack(pady=10)

root.mainloop()
Output Calculator window with 2 input boxes and 4 buttons (Add, Sub, Mul, Div). Enter numbers like 10 and 5, click Add to display "Result: 15.0"

Key Concepts

tiranga_canvas.py PYTHON
6 Tiranga using Canvas

Draw Indian flag with canvas rectangles and Ashoka Chakra.

from tkinter import *
import math

root = Tk()
root.title("Tiranga")
c = Canvas(root, width=500, height=350, bg="white")
c.pack()

c.create_rectangle(50, 50, 450, 150, fill="#FF9933", outline="black")
c.create_rectangle(50, 150, 450, 250, fill="white", outline="black")
c.create_rectangle(50, 250, 450, 350, fill="#138808", outline="black")

cx, cy, r = 250, 200, 35
c.create_oval(cx-r, cy-r, cx+r, cy+r, outline="#000080", width=2)
for i in range(24):
    angle = 2 * math.pi * i / 24
    x = cx + r * math.cos(angle)
    y = cy + r * math.sin(angle)
    c.create_line(cx, cy, x, y, fill="#000080")

root.mainloop()
Output Indian flag with saffron, white, green stripes. Blue Ashoka Chakra with 24 spokes in center

Key Concepts

circle_area.py PYTHON
7 Area of Circle using Function

User-defined function to calculate circle area using formula πr².

import math

def circle_area(r):
    return math.pi * r ** 2

def circle_circumference(r):
    return 2 * math.pi * r

r = 7
print(f"Radius: {r}")
print(f"Area: {circle_area(r):.2f}")
print(f"Circumference: {circle_circumference(r):.2f}")

r2 = 10
print(f"\nRadius: {r2}")
print(f"Area: {circle_area(r2):.2f}")
print(f"Circumference: {circle_circumference(r2):.2f}")
Output Radius: 7 Area: 153.94 Circumference: 43.98 | Radius: 10 Area: 314.16 Circumference: 62.83

Key Concepts

multilevel_inheritance.py PYTHON
8 Multilevel Inheritance

Chain of inheritance: Grandparent → Parent → Child.

class Vehicle:
    def __init__(self, brand):
        self.brand = brand
    def start(self):
        print(f"{self.brand} starting...")

class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)
        self.model = model
    def drive(self):
        print(f"{self.brand} {self.model} driving")

class ElectricCar(Car):
    def __init__(self, brand, model, battery):
        super().__init__(brand, model)
        self.battery = battery
    def charge(self):
        print(f"Charging {self.battery}kWh battery")

ev = ElectricCar("Tesla", "Model 3", 75)
ev.start()
ev.drive()
ev.charge()
Output Tesla starting... | Tesla Model 3 driving | Charging 75kWh battery

Key Concepts

oop_combined.py PYTHON
9 OOP Concepts Combined

Demonstrates Encapsulation, Inheritance, Polymorphism & Abstraction.

from abc import ABC, abstractmethod

class Account(ABC):
    def __init__(self, holder, balance):
        self.holder = holder
        self._balance = balance
    
    def deposit(self, amt):
        self._balance += amt
    
    @abstractmethod
    def withdraw(self, amt):
        pass
    
    def show(self):
        print(f"{self.holder}: ₹{self._balance}")

class Savings(Account):
    def withdraw(self, amt):
        if self._balance - amt >= 1000:
            self._balance -= amt
        else:
            print("Min balance ₹1000 required")

class Current(Account):
    def withdraw(self, amt):
        self._balance -= amt

s = Savings("Priyanshu", 5000)
c = Current("Rahul", 3000)

s.deposit(2000)
s.withdraw(500)
s.show()

c.deposit(1000)
c.withdraw(3500)
c.show()
Output Priyanshu: ₹6500 | Rahul: ₹500

Key Concepts