class Student:
# Constructor to initialize attributes
def __init__(self, name, roll_no):
[Link] = name
self.roll_no = roll_no
# Method to display student information
def display(self):
print(“Student Name:”, [Link])
print(“Roll Number:”, self.roll_no)
# Creating objects (instances of Student class)
s1 = Student(“Rahul”, 101)
s2 = Student(“Priya”, 102)
# Method call using objects
[Link]()
[Link]()
-----------------------------------------------------
class Student:
def __init__(self, name, roll_no): # constructor
[Link] = name
self.roll_no = roll_no
def display(self):
print(“Name:”, [Link], “Roll No:”, self.roll_no)
# object creation → constructor runs automatically
s1 = Student(“Rahul”, 101)
[Link]()
-----------------------------------------------------
class Car:
wheels = 4 # class attribute (same for all cars)
def __init__(self, brand, color):
[Link] = brand # instance attribute
[Link] = color # instance attribute
def show(self):
print(“Brand:”, [Link], “Color:”, [Link], “Wheels:”,
[Link])
c1 = Car(“BMW”, “Black”)
c2 = Car(“Audi”, “Red”)
[Link]()
[Link]()
-----------------------------------------------------
class Car:
def __init__(self, brand):
[Link] = brand
print(“Car created:”, [Link])
def __del__(self):
print(“Car destroyed:”, [Link])
c1 = Car(“BMW”) # constructor runs
del c1 # destructor runs here
-----------------------------------------------------
class BankDemo:
# constructor to initialize attributes
def __init__(self, acc_no, name, balance=0):
self.acc_no = acc_no
[Link] = name
[Link] = balance
print(f”Account created for {[Link]} (Acc No: {self.acc_no})
with balance {[Link]}”)
# method to deposit money
def deposit(self, amount):
if amount > 0:
[Link] += amount
print(f”{amount} deposited successfully. New Balance:
{[Link]}”)
else:
print(“Invalid deposit amount!”)
# method to withdraw money
def withdraw(self, amount):
if amount <= [Link]:
[Link] -= amount
print(f”{amount} withdrawn successfully. Remaining Balance:
{[Link]}”)
else:
print(“Insufficient Balance!”)
# method to check balance
def check_balance(self):
print(f”Account Balance for {[Link]}: {[Link]}”)
# Creating object of BankDemo class
acc1 = BankDemo(101, “Rahul”, 5000)
# Performing operations
[Link](2000) # deposit
[Link](1500) # withdraw
acc1.check_balance() # check balance
-----------------------------------------------------
class Student:
# Constructor to initialize values
def __init__(self, first_name, last_name, qualification):
self.first_name = first_name
self.last_name = last_name
[Link] = qualification
print(f”Student record created for {self.first_name}
{self.last_name}”)
# Method to update qualification
def update_qualification(self, new_qualification):
print(f”Updating qualification of {self.first_name}
{self.last_name}...”)
[Link] = new_qualification
print(“Qualification updated successfully!”)
# Method to display student details
def display(self):
print(“\n--- Student Details ---”)
print(“First Name:”, self.first_name)
print(“Last Name:”, self.last_name)
print(“Qualification:”, [Link])
# ---------- Main Program ----------
# Creating student object
s1 = Student(“Rahul”, “Sharma”, “[Link]”)
# Display details before update
[Link]()
# Updating qualification
s1.update_qualification(“MCA”)
# Display details after update
[Link]()
--------------------------------------------------------
class Parent:
# parent code
class Child(Parent):
# child code
--------------------------------------------------------
class Parent: # Superclass
def parent_method(self):
print(“This is a method from Parent class”)
class Child(Parent): # Child inherits Parent
def child_method(self):
print(“This is a method from Child class”)
# Object of Child class
obj = Child()
obj.parent_method() # method from Parent
obj.child_method() # method from Child
--------------------------------------------------------
class Parent:
def show(self):
print(“This is the Parent class method”)
class Child(Parent):
def show(self): # Overriding the parent method
print(“This is the Child class method (overridden)”)
# Testing
p = Parent()
c = Child()
[Link]() # Parent’s method
[Link]() # Child’s method overrides Parent’s
--------------------------------------------------------
class Parent:
def func1(self):
print(“This is Parent class”)
class Child(Parent): # Child inherits Parent
def func2(self):
print(“This is Child class”)
obj = Child()
obj.func1() # Parent method
obj.func2() # Child method
--------------------------------------------------------
class Parent1:
def func1(self):
print(“This is Parent1 class”)
class Parent2:
def func2(self):
print(“This is Parent2 class”)
class Child(Parent1, Parent2): # inherits from both
def func3(self):
print(“This is Child class”)
obj = Child()
obj.func1() # from Parent1
obj.func2() # from Parent2
obj.func3() # own method
--------------------------------------------------------
class Grandparent:
def func1(self):
print(“This is Grandparent class”)
class Parent(Grandparent):
def func2(self):
print(“This is Parent class”)
class Child(Parent):
def func3(self):
print(“This is Child class”)
obj = Child()
obj.func1() # from Grandparent
obj.func2() # from Parent
obj.func3() # from Child
--------------------------------------------------------
class Parent:
def func1(self):
print(“This is Parent class”)
class Child1(Parent):
def func2(self):
print(“This is Child1 class”)
class Child2(Parent):
def func3(self):
print(“This is Child2 class”)
obj1 = Child1()
obj2 = Child2()
obj1.func1(); obj1.func2()
obj2.func1(); obj2.func3()
--------------------------------------------------------
class Parent1:
def func1(self):
print(“This is Parent1 class”)
class Parent2:
def func2(self):
print(“This is Parent2 class”)
class Child1(Parent1):
def func3(self):
print(“This is Child1 class”)
class Child2(Child1, Parent2): # multiple + multilevel
def func4(self):
print(“This is Child2 class”)
obj = Child2()
obj.func1() # from Parent1
obj.func2() # from Parent2
obj.func3() # from Child1
obj.func4() # from Child2
--------------------------------------------------------
class Number:
def __init__(self, value):
[Link] = value
# Overloading + operator
def __add__(self, other):
return [Link] + [Link]
# Creating objects
n1 = Number(10)
n2 = Number(20)
print(n1 + n2) # Works like normal addition
--------------------------------------------------------
class Student:
def __init__(self, name):
[Link] = name # object’s own variable
def show(self): # normal method
print(“Name:”, [Link])
s1 = Student(“Rahul”)
s2 = Student(“Priya”)
[Link]() # prints Rahul
[Link]() # prints Priya
--------------------------------------------------------
class Student:
count = 0 # class variable (same for all students)
def __init__(self, name):
[Link] = name
[Link] += 1 # increase when new student is created
@classmethod
def total_students(cls): # cls = Student class
print(“Total Students:”, [Link])
s1 = Student(“Rahul”)
s2 = Student(“Priya”)
Student.total_students()
--------------------------------------------------------
class Math:
@staticmethod
def add(a, b):
return a + b
@staticmethod
def multiply(a, b):
return a * b
print([Link](5, 10))
print([Link](3, 4))
--------------------------------------------------------
class Printer:
def print_page(self):
print(“Page is printing...”)
class Computer:
def __init__(self):
[Link] = Printer() # has a printer
def print_document(self):
# Delegation → Computer asks Printer to do the task
[Link].print_page()
c1 = Computer()
c1.print_document()
--------------------------------------------------------
class Engine:
def start(self):
print(“Engine started”)
class Car:
def __init__(self):
[Link] = Engine() # Car has an Engine
def drive(self):
[Link]()
print(“Car is moving”)
c1 = Car()
[Link]()
--------------------------------------------------------
import re
pattern = r”abc”
text = “abcdef”
if [Link](pattern, text):
print(“Match found!”)
else:
print(“Match not found!”)
--------------------------------------------------------
import re
pattern = r”abc”
text = “This is a test: abc”
search = [Link](pattern, text)
if search:
print(“Pattern found!”)
else:
print(“Pattern not found!”)
--------------------------------------------------------
import re
text = “The first date is 12-05-2022 and the second date is 15-06-
2021.”
# Regex pattern for matching dates in format DD-MM-YYYY
pattern = r”\d{2}-\d{2}-\d{4}”
dates = [Link](pattern, text)
print(“Dates found:”, dates)
--------------------------------------------------------
import re
text = “You can contact us at info@[Link] or support@[Link].”
# Regex pattern for matching email addresses
pattern = r”[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+”
emails = [Link](pattern, text)
print(“Emails found:”, emails)
--------------------------------------------------------
import re
text = “Check website ://[Link] and follow on
[Link]
# Regex pattern for matching URLs
pattern = r”https?://[a-zA-Z0-9-]+\.[a-zA-Z]{2,}”
urls = [Link](pattern, text)
print(“URLs found:”, urls)
--------------------------------------------------------
import re
def validate_password(password):
# Regex for strong password
pattern = r’^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\
d@$!%*?&]{8,}$’
if [Link](pattern, password):
return “Valid Strong Password”
else:
return “Invalid Password”
# Test the program
print(validate_password(“Rahul@123”)) # strong password
print(validate_password(“rahul123”)) # missing uppercase & special
char
print(validate_password(“RAHUL@123”)) # missing lowercase
--------------------------------------------------------
import re
def validate_email(email):
# Regex pattern for validating email
pattern = r’^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$’
if [Link](pattern, email):
return “Valid Email Address ”
else:
return “Invalid Email Address ”
# Test the program
print(validate_email(“student123@[Link]”)) # valid
print(validate_email(“[Link]@[Link]”)) # valid
print(validate_email(“invalid-email@com”)) # invalid
--------------------------------------------------------
import threading
def task():
print(“Thread is running...”)
# Create thread
t1 = [Link](target=task)
# Start thread
[Link]()
# Wait for thread to finish
[Link]()
----------------------------------------------------------
import threading
balance = 1000 # shared resource
def withdraw(amount):
global balance
for _ in range(3):
balance -= amount
print(“Withdraw:”, amount, “Remaining Balance:”, balance)
# Create threads
t1 = [Link](target=withdraw, args=(200,))
t2 = [Link](target=withdraw, args=(200,))
[Link]()
[Link]()
-------------------------------------------------------
import threading
balance = 1000
lock = [Link]()
def withdraw(amount):
global balance
for _ in range(3):
[Link]() # lock the resource
balance -= amount
print(“Withdraw:”, amount, “Remaining Balance:”, balance)
[Link]() # release the resource
# Create threads
t1 = [Link](target=withdraw, args=(200,))
t2 = [Link](target=withdraw, args=(200,))
[Link]()
[Link]()
--------------------------------------------------------
import threading
def print_numbers():
for i in range(1, 6):
print(“Number:”, i)
def print_letters():
for ch in [‘A’, ‘B’, ‘C’, ‘D’, ‘E’]:
print(“Letter:”, ch)
# Create threads
t1 = [Link](target=print_numbers)
t2 = [Link](target=print_letters)
# Start threads
[Link]()
[Link]()
# Wait for both threads to finish
[Link]()
[Link]()
print(“Both threads finished, program ends here”)
--------------------------------------------------------
import threading
import time
# Create a lock for synchronization
lock = [Link]()
# Thread function for ascending order
def print_ascending():
for i in range(1, 11):
[Link]() # lock before printing
print(“Ascending:”, i)
[Link]() # release lock
[Link](0.3) # small delay for better output
# Thread function for descending order
def print_descending():
for i in range(10, 0, -1):
[Link]()
print(“Descending:”, i)
[Link]()
[Link](0.3)
# Create threads
t1 = [Link](target=print_ascending)
t2 = [Link](target=print_descending)
# Start threads
[Link]()
[Link]()
# Wait for threads to complete
[Link]()
[Link]()
print(“Both threads finished execution”)
--------------------------------------------------------
import threading
import time
# Create a lock for synchronization
lock = [Link]()
# Thread function for printing squares
def print_squares():
for i in range(1, 6):
[Link]() # acquire lock
print(“Square of”, i, “=”, i * i)
[Link]() # release lock
[Link](0.3) # small delay
# Thread function for printing cubes
def print_cubes():
for i in range(1, 6):
[Link]()
print(“Cube of”, i, “=”, i * i * i)
[Link]()
[Link](0.3)
# Create threads
t1 = [Link](target=print_squares)
t2 = [Link](target=print_cubes)
# Start threads
[Link]()
[Link]()
# Wait for threads to finish
[Link]()
[Link]()
print(“Both threads finished execution”)
----------------------------------------------------------