3.
Multiple Inheritance Example
class Person:
def __init__(self, name):
[Link] = name
class Skills:
def __init__(self, skill):
[Link] = skill
class Employee(Person, Skills):
def __init__(self, name, skill, employee_id):
Person.__init__(self, name)
Skills.__init__(self, skill)
self.employee_id = employee_id
def display_info(self):
print(f"Name: {[Link]}, Skill: {[Link]}, Employee ID: {self.employee_id}")
emp = Employee("Alice", "Python Programming", "E123")
emp.display_info()
4. Factorial Using For Loop
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"Factorial of {num} is {factorial}")
5. Fibonacci Using While Loop
n = int(input("Enter number of terms: "))
a, b = 0, 1
count = 0
while count < n:
print(a, end=" ")
a, b = b, a + b
count += 1
6. Simple Calculator Using Functions
def add(x, y): return x + y
def subtract(x, y): return x - y
def multiply(x, y): return x * y
def divide(x, y): return x / y if y != 0 else "Cannot divide by zero"
print("Select operation: [Link] [Link] [Link] [Link]")
choice = input("Enter choice (1/2/3/4): ")
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if choice == '1':
print("Result:", add(a, b))
elif choice == '2':
print("Result:", subtract(a, b))
elif choice == '3':
print("Result:", multiply(a, b))
elif choice == '4':
print("Result:", divide(a, b))
else:
print("Invalid choice")
7. Read File and Print Words of Specific Length
length = int(input("Enter word length to filter: "))
with open("[Link]", "r") as file:
words = [Link]().split()
filtered = [word for word in words if len(word) == length]
print(f"Words with length {length}:", filtered)
8. Division With Exception Handling
try:
a = int(input("Enter numerator: "))
b = int(input("Enter denominator: "))
result = a / b
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Invalid input. Please enter integers.")
else:
print("Result:", result)
9. Faculty Details Using Constructor
class Faculty:
def __init__(self, name, dept, emp_id):
[Link] = name
[Link] = dept
self.emp_id = emp_id
def display(self):
print(f"Name: {[Link]}, Department: {[Link]}, ID: {self.emp_id}")
f1 = Faculty("Dr. Smith", "CSE", "F001")
[Link]()
10. Faculty Details with Default Values
class Faculty:
def __init__(self, name="John Doe", dept="General", emp_id="000"):
[Link] = name
[Link] = dept
self.emp_id = emp_id
def display(self):
print(f"Name: {[Link]}, Department: {[Link]}, ID: {self.emp_id}")
f1 = Faculty()
[Link]()
f2 = Faculty("Dr. Allen", "ECE", "F100")
[Link]()
11. GUI with Tkinter (Entry + Checkbox)
import tkinter as tk
def show_data():
print(f"Name: {name_entry.get()}, Subscribe: {[Link]()}")
root = [Link]()
[Link]("Simple GUI")
[Link](root, text="Enter Name:").pack()
name_entry = [Link](root)
name_entry.pack()
var = [Link]()
[Link](root, text="Subscribe to newsletter", variable=var).pack()
[Link](root, text="Submit", command=show_data).pack()
[Link]()
12. Validate Phone & Email using Regex
import re
phone = input("Enter phone number: ")
email = input("Enter email ID: ")
phone_pattern = r"^\+?\d{10,13}$"
email_pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"
if [Link](phone_pattern, phone):
print("Valid phone number")
else:
print("Invalid phone number")
if [Link](email_pattern, email):
print("Valid email ID")
else:
print("Invalid email ID")
13. Numpy 1D, 2D, 3D Arrays + Operations
import numpy as np
a = [Link]([1, 2, 3]) # 1D
b = [Link]([[1, 2], [3, 4]]) # 2D
c = [Link]([[[1], [2]], [[3], [4]]]) # 3D
print("1D:", a)
print("2D:", b)
print("3D:", c)
# Reshape
reshaped = [Link](1, 4)
print("Reshaped 2D to 1x4:", reshaped)
# Slicing and Indexing
print("Element at (1,1):", b[1, 1])
14. Arithmetic Operations on 2D Arrays
import numpy as np
arr1 = [Link]([[1, 2], [3, 4]])
arr2 = [Link]([[5, 6], [7, 8]])
print("Addition:\n", arr1 + arr2)
print("Subtraction:\n", arr1 - arr2)
print("Multiplication:\n", arr1 * arr2)
print("Division:\n", arr1 / arr2)