3.
Multiple Inheritance Example
class Person:
def __init__(self, name):
self.name = name
class Skills:
def __init__(self, skill):
self.skill = 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: {self.name}, Skill: {self.skill}, 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: 1.Add 2.Subtract 3.Multiply 4.Divide")
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("sample.txt", "r") as file:
words = file.read().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):
self.name = name
self.dept = dept
self.emp_id = emp_id
def display(self):
print(f"Name: {self.name}, Department: {self.dept}, ID: {self.emp_id}")
f1 = Faculty("Dr. Smith", "CSE", "F001")
f1.display()
10. Faculty Details with Default Values
class Faculty:
def __init__(self, name="John Doe", dept="General", emp_id="000"):
self.name = name
self.dept = dept
self.emp_id = emp_id
def display(self):
print(f"Name: {self.name}, Department: {self.dept}, ID: {self.emp_id}")
f1 = Faculty()
f1.display()
f2 = Faculty("Dr. Allen", "ECE", "F100")
f2.display()
11. GUI with Tkinter (Entry + Checkbox)
import tkinter as tk
def show_data():
print(f"Name: {name_entry.get()}, Subscribe: {var.get()}")
root = tk.Tk()
root.title("Simple GUI")
tk.Label(root, text="Enter Name:").pack()
name_entry = tk.Entry(root)
name_entry.pack()
var = tk.BooleanVar()
tk.Checkbutton(root, text="Subscribe to newsletter", variable=var).pack()
tk.Button(root, text="Submit", command=show_data).pack()
root.mainloop()
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 re.match(phone_pattern, phone):
print("Valid phone number")
else:
print("Invalid phone number")
if re.match(email_pattern, email):
print("Valid email ID")
else:
print("Invalid email ID")
13. Numpy 1D, 2D, 3D Arrays + Operations
import numpy as np
a = np.array([1, 2, 3]) # 1D
b = np.array([[1, 2], [3, 4]]) # 2D
c = np.array([[[1], [2]], [[3], [4]]]) # 3D
print("1D:", a)
print("2D:", b)
print("3D:", c)
# Reshape
reshaped = b.reshape(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 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
print("Addition:\n", arr1 + arr2)
print("Subtraction:\n", arr1 - arr2)
print("Multiplication:\n", arr1 * arr2)
print("Division:\n", arr1 / arr2)