Python
Python
Page No: 1
[Link]: 1 Exp. Name: Print Statements
02
Aim:
3 9 3 0 1 0 3 S C 5 2 N E : DI
Write a Python program that stores a user's name in a variable and prints a greeting
message in the format "Hello, <name>".
Input Format:
• A single line containing the user's name as a string.
Output Format:
• A single line greeting the user in the format "Hello, <name>".
Source Code:
[Link]
print("Hello,",a)
Test Case - 1
User Output
code tantra
Hello, code tantra
Medicaps University
Test Case - 2
User Output
satya
Hello, satya
Date: 2026-02-
[Link]: 2 Exp. Name: Arithmetic Operations
Page No: 2
02
Aim:
Write a program to perform addition, subtraction, multiplication, division, and integer
3 9 3 0 1 0 3 S C 5 2 N E : DI
division on two numbers.
Input Format:
• Two input lines read two positive integers representing the operands.
Output Format:
• The first line should print the result of addition.
• The second line should print the result of subtraction.
• The third line should print the result of multiplication.
• The fourth line should print the result of division with two decimal places.
• The fifth line should print the result of integer division.
[Link]
a = int(input())
b = int(input())
if a<0:
print("No")
if b<=0:
print("No")
Medicaps University
print(a+b)
print(a-b)
print(a*b)
print(f"{a/b:.2f}")
print(a//b)
Page No: 3
User Output
10
3
3 9 3 0 1 0 3 S C 5 2 N E : DI
13
7
30
3.33
3
Test Case - 2
User Output
20
Medicaps University
Exp. Name: Program to print a message Date: 2026-02-
[Link]: 3
Page No: 4
on the screen 02
Aim:
Write a python program to print a message (Hello, World!) on screen.
3 9 3 0 1 0 3 S C 5 2 N E : DI
Source Code:
[Link]
print("Hello, World!")
User Output
Hello, World!
Medicaps University
Date: 2026-02-
[Link]: 4 Exp. Name: Area of Rectangle
Page No: 5
02
Aim:
Write a Python program to calculate the area of a rectangle given its length and width.
3 9 3 0 1 0 3 S C 5 2 N E : DI
Formula:
Area of Rectangle = Length × Width
Input Format:
• First line contains a float value representing the length of the rectangle
• Second line contains a float value representing the width of the rectangle
Output Format:
• Print the area of the rectangle as a float value formatted to 2 decimal places.
Source Code:
a = float(input())
b = float(input())
c = a*b
d = f"{c:.2f}"
print(d)
User Output
10.5
5.2
54.60
Test Case - 2
User Output
15
8
120.00
Page No: 6
Test Case - 3
3 9 3 0 1 0 3 S C 5 2 N E : DI
User Output
2.5
3.5
8.75
Test Case - 4
User Output
100
Test Case - 5
User Output
7
7
49.00
Medicaps University
Date: 2026-02-
[Link]: 5 Exp. Name: Fahrenheit to Celsius
Page No: 7
02
Aim:
Write a Python program to convert temperature from Fahrenheit to Celsius. Prompt the
3 9 3 0 1 0 3 S C 5 2 N E : DI
user to enter a temperature in Fahrenheit.
Input Format:
• Input should prompt the user to enter the temperature in Fahrenheit as a floating
point.
Output Format:
• The output should print the temperature in Celsius.
[Link]
a = float(input())
b = a-32
c = b*5/9
d = f"{c:.2f}"
print(d)
Medicaps University
Test Case - 1
User Output
68.5
20.28
Page No: 8 3 9 3 0 1 0 3 S C 5 2 N E : DI nahuohC ayviD forP-B_11P_2003SC_5202 Medicaps University
Test Case - 2
User Output
37.00
98.6
Date: 2026-02-
[Link]: 6 Exp. Name: List Operations in Python
Page No: 9
09
Aim:
Write a Python program to perform the following operations on a list:
3 9 3 0 1 0 3 S C 5 2 N E : DI
1. Read an integer n representing the number of elements.
2. Read n space-separated integers and create a list. Display the original list.
3. Read an integer representing the element to be inserted and an integer representing
the 0-based position at which it should be inserted. Insert the element into the list and
display the list after insertion.
4. Read an integer representing the element to be deleted. If the element exists, delete it
from the list; otherwise, leave the list unchanged. Display the list after deletion.
5. Traverse the final list and display each element on a new line.
Note:
• Assume that all given positions are valid.
Medicaps University
[Link]
Page No: 10
n = int(input())
3 9 3 0 1 0 3 S C 5 2 N E : DI
print("Original List:", list)
a = int(input())
b = int(input())
[Link](b,a)
c = int(input())
if (c in list):
print("Traversing List:")
for i in list:
print(i)
Test Case - 1
User Output
4 Medicaps University
10 20 30 40
Original List: [10, 20, 30, 40]
25
2
After Insertion: [10, 20, 25, 30, 40]
20
After Deletion: [10, 25, 30, 40]
Traversing List:
10
25
40
Page No: 11
Test Case - 2
User Output
3 9 3 0 1 0 3 S C 5 2 N E : DI
123
Original List: [1, 2, 3]
10
0
After Insertion: [10, 1, 2, 3]
5
After Deletion: [10, 1, 2, 3]
Traversing List:
10
Medicaps University
Date: 2026-02-
[Link]: 7 Exp. Name: Tuple and Set Operations
Page No: 12
16
Aim:
Write a Python program to perform the following operations on Tuple and Set data types.
3 9 3 0 1 0 3 S C 5 2 N E : DI
Tuple Operations
• Create a tuple by reading n elements from the user.
• Display the tuple.
• Read an index from the user and display the element present at that index.
Set Operations
• Create a set by reading m elements from the user.
• Display the set.
• Read an element to be added to the set and update the set.
• Read an element to be removed from the set and update the set.
Medicaps University
Note:
• Assume all index values provided are valid.
• The input set may contain duplicate values, but it should store unique elements.
• Refer to the visible test cases for a better understanding and ensure strict
matching with the input/outputs.
Source Code:
[Link]
Page No: 13
n = int(input())
t_list = []
3 9 3 0 1 0 3 S C 5 2 N E : DI
for _ in range(n):
t_list.append(int(input()))
t = tuple(t_list)
print("Tuple:",t)
index = int(input())
s = set()
for _ in range(m):
[Link](int(input()))
print("Set:",s)
add_ele = int(input())
[Link](add_ele)
print("After Addition:",s)
rem_ele = int(input())
Medicaps University
[Link](rem_ele)
print("After Deletion:", s)
Test Case - 1
User Output
4
1
Page No: 14
2
3
4
Tuple: (1, 2, 3, 4)
3 9 3 0 1 0 3 S C 5 2 N E : DI
0
Accessed Element: 1
4
10
20
30
40
Set: {40, 10, 20, 30}
40
Test Case - 2
User Output
5
5
10
15
20
25 Medicaps University
Tuple: (5, 10, 15, 20, 25)
3
Accessed Element: 20
6
2
4
6
8
10
10
After Addition: {2, 4, 6, 8, 10, 12}
4
Page No: 15
After Deletion: {2, 6, 8, 10, 12}
3 9 3 0 1 0 3 S C 5 2 N E : DI
nahuohC ayviD forP-B_11P_2003SC_5202
Medicaps University
Date: 2026-02-
[Link]: 8 Exp. Name: Dictionary Operations
Page No: 16
16
Aim:
Write a Python program to perform insertion, update, deletion, and traversal operations
3 9 3 0 1 0 3 S C 5 2 N E : DI
on a dictionary. An initial dictionary containing 10 predefined records is already given in
the program.
Operations to be Performed:
1. Insertion – Insert a new key-value pair into the dictionary using user input.
2. Update – Update the value of an existing key using user input.
3. Deletion – Delete a specified key from the dictionary using user input.
4. Traversal – Traverse the final dictionary and display all key-value pairs.
Note:
• All operations must be performed using dictionary methods.
• Perform deletion only if possible; leave the dictionary unchanged.
Medicaps University
• Refer to the visible test cases for better understanding and strictly match with the
input/outputs.
Source Code:
[Link]
Page No: 17
# Initial dictionary with 10 predefined records
student = {
1: "Amit",
2: "Riya",
3 9 3 0 1 0 3 S C 5 2 N E : DI
3: "Kiran",
4: "Neha",
5: "Arjun",
6: "Pooja",
7: "Rahul",
8: "Sneha",
9: "Vikram",
10: "Anjali"
}
student[a] = b
c = int(input())
d = input()
student[c] = d
e = int(input())
Medicaps University
if e in student:
del student[e]
print("Traversing Dictionary:")
for i in student:
print(i ,":",student[i])
Execution Results - All test cases have succeeded!
Page No: 18
Test Case - 1
User Output
3 9 3 0 1 0 3 S C 5 2 N E : DI
'Neha', 5: 'Arjun', 6: 'Pooja', 7: 'Rahul', 8: 'Sneha', 9:
'Vikram', 10: 'Anjali'}
11
Suresh
After Insertion: {1: 'Amit', 2: 'Riya', 3: 'Kiran', 4: 'Neha',
5: 'Arjun', 6: 'Pooja', 7: 'Rahul', 8: 'Sneha', 9: 'Vikram',
10: 'Anjali', 11: 'Suresh'}
3
Karthik
After Update: {1: 'Amit', 2: 'Riya', 3: 'Karthik', 4: 'Neha',
Test Case - 2
User Output
Page No: 19
10: 'Anjali', 12: 'Meera'}
6
Divya
After Update: {1: 'Amit', 2: 'Riya', 3: 'Kiran', 4: 'Neha', 5:
3 9 3 0 1 0 3 S C 5 2 N E : DI
'Arjun', 6: 'Divya', 7: 'Rahul', 8: 'Sneha', 9: 'Vikram', 10:
'Anjali', 12: 'Meera'}
1
After Deletion: {2: 'Riya', 3: 'Kiran', 4: 'Neha', 5: 'Arjun',
6: 'Divya', 7: 'Rahul', 8: 'Sneha', 9: 'Vikram', 10: 'Anjali',
12: 'Meera'}
Traversing Dictionary:
2 : Riya
3 : Kiran
4 : Neha
Medicaps University
Date: 2026-02-
[Link]: 9 Exp. Name: Check Even or Odd
Page No: 20
16
Aim:
Write a Python program that reads an integer from the user and checks whether the given
3 9 3 0 1 0 3 S C 5 2 N E : DI
number is even or odd.
Input Format:
• The first line of input is an integer representing the number to be checked.
Output Format:
• Print Even number if the number is even.
• Print Odd number if the number is odd.
a = int(input())
if a%2==0:
print("Even number")
else:
print("Odd number")
User Output
4
Even number
Test Case - 2
User Output
5
Odd number
Exp. Name: Find the Largest of Three Date: 2026-02-
[Link]: 10
Page No: 21
Numbers 16
Aim:
Write a Python program that reads three integers from the user and prints the largest
3 9 3 0 1 0 3 S C 5 2 N E : DI
among them.
Input Format:
• The first line of input is an integer representing the first number.
• The second line of input is an integer representing the second number.
• The third line of input is an integer representing the third number.
Output Format:
• Print the largest of the three numbers in the following format:
Largest number is: <value>
[Link]
a = int(input())
b = int(input())
c = int(input())
if a>b:
if a>c:
print("Largest number is:", a)
else:
print("Largest number is:", c)
Test Case - 1
User Output
10
20
Page No: 22
30
Largest number is: 30
3 9 3 0 1 0 3 S C 5 2 N E : DI
Test Case - 2
User Output
45
12
33
Largest number is: 45
Test Case - 3
5
5
5
Largest number is: 5
Medicaps University
Exp. Name: Factorial of a Number using Date: 2026-02-
[Link]: 11
Page No: 23
While Loop 16
Aim:
Write a Python program that reads an integer from the user and calculates its factorial
3 9 3 0 1 0 3 S C 5 2 N E : DI
using a while loop.
Input Format:
• The first line of input is an integer representing the number.
Output Format:
• Print the factorial of the given number in the following format:
Factorial: <value>
Source Code:
[Link]
print("Factorial:", b)
Test Case - 1
5
Factorial: 120
Test Case - 2
User Output
7
Factorial: 5040
Exp. Name: Fibonacci Sequence Using For Date: 2026-02-
[Link]: 12
Page No: 24
Loop 16
Aim:
Write a Python program that reads an integer n from the user and prints the first n terms
3 9 3 0 1 0 3 S C 5 2 N E : DI
of the Fibonacci sequence using a for loop.
Input Format:
• The first line of input is an integer representing the number of terms n.
Output Format:
• Print the Fibonacci sequence up to n terms in a single line, with each term
separated by a space.
Source Code:
[Link]
b = [0,1,1]
i=0
while i<a:
if i>2:
[Link](b[i-1] + b[i-2])
i += 1
print(*b, "\n")
Medicaps University
Test Case - 1
User Output
5
0 1 1 2 3
Page No: 25 3 9 3 0 1 0 3 S C 5 2 N E : DI nahuohC ayviD forP-B_11P_2003SC_5202 Medicaps University
Test Case - 2
0 1 1 2 3 5 8 13
User Output
8
Date: 2026-02-
[Link]: 13 Exp. Name: Sum of Prime Numbers
Page No: 26
18
Aim:
Write a Python program that reads two integers representing a start range and an end
3 9 3 0 1 0 3 S C 5 2 N E : DI
range, and computes the sum of all prime numbers within this range (both inclusive).
Input Format:
• The first line contains an integer representing the starting value of the range.
• The second line contains an integer representing the ending value of the range.
Output Format:
• Print the sum of all prime numbers present in the given range in the format:
Sum of prime numbers: <sum>
Constraints:
[Link]
a = int(input())
b = int(input())
sum = 0
if prime:
sum += num
Page No: 27
User Output
5
15
Sum of prime numbers: 36
3 9 3 0 1 0 3 S C 5 2 N E : DI
Test Case - 2
User Output
8
10
Sum of prime numbers: 0
Page No: 28
Classes 28
Aim:
Write a Python program that defines a class Calculator with two methods:
3 9 3 0 1 0 3 S C 5 2 N E : DI
• add(a, b) - returns the sum of a and b.
• subtract(a, b) - returns the difference of a and b.
Create an object of the Calculator class named calc and use it to perform the required
operations. Read two numbers from the user, and print:
• The result of the addition
• The result of the subtraction
Input Format:
• The first line contains an integer representing the first number a.
• The second line contains an integer representing the second number b.
[Link]
def subtract(self,a,b):
return a-b
Medicaps University
# User input
num1 = int(input())
num2 = int(input())
# Perform calculations
print("Addition:", [Link](num1, num2))
print("Subtraction:", [Link](num1, num2))
Execution Results - All test cases have succeeded!
Page No: 29
Test Case - 1
User Output
3 9 3 0 1 0 3 S C 5 2 N E : DI
3
Addition: 6
Subtraction: 0
Test Case - 2
User Output
5
15
Test Case - 3
User Output
25
10
Addition: 35
Subtraction: 15
Medicaps University
Exp. Name: Inheritance using a Class Date: 2026-02-
[Link]: 15
Page No: 30
Hierarchy 28
Aim:
Write a Python program to create a class hierarchy consisting of a base class and two
3 9 3 0 1 0 3 S C 5 2 N E : DI
derived classes to demonstrate the concept of inheritance.
Program Requirements:
1. Create a base class named Person with:
• An attribute name.
• A method show_name() that displays the name of the person.
Name: <name>
2. Create two derived classes:
• Student, inheriting from Person, with a method study() that prints:
<name> is studying
• Teacher, inheriting from Person, with a method teach() that prints:
Input Format:
• The first line contains a string representing the student name.
• The second line contains a string representing the teacher name.
Output Format:
The output should be printed in the following order:
• Display the student name using the base class method.
• Display the teacher name using the base class method.
Medicaps University
• Display the student study message.
• Display the teacher's teaching message.
Source Code:
[Link]
Page No: 31
# Base class
class Person:
name = ""
def show_name(self):
3 9 3 0 1 0 3 S C 5 2 N E : DI
print("Name:", [Link])
stu = input()
tea = input()
student = Student()
teacher = Teacher()
[Link] = stu
[Link] = tea
Page No: 32
student.show_name()
teacher.show_name()
[Link]()
3 9 3 0 1 0 3 S C 5 2 N E : DI
[Link]()
Test Case - 1
User Output
Rahul
Test Case - 2
User Output
Michael
Philips
Name: Michael
Name: Philips
Michael is studying Medicaps University
Philips is teaching
Exp. Name: Method Overriding using Date: 2026-02-
[Link]: 16
Page No: 33
Inheritance 28
Aim:
Write a Python program to implement method overriding in a class hierarchy.
3 9 3 0 1 0 3 S C 5 2 N E : DI
Program Requirements:
• Create a base class named Vehicle with a method description() that prints:
This is a vehicle
• Create a derived class named Car that inherits from Vehicle and overrides the
description() method to print:
This is a car
• Create objects of both the base class and the derived class.
• Call the description() method using both objects to demonstrate method
overriding.
Output Format:
The output should consist of two lines:
• The first line displays the output of the base class method.
• The second line displays the output of the overridden method in the derived class.
Source Code:
[Link]
# Base class
class Vehicle:
def description(self):
print("This is a vehicle")
Medicaps University
# Derived class
class Car:
def description(self):
print("This is a car")
# Create objects
v = Vehicle()
c = Car()
# Call methods
[Link]() # Calls base class method
[Link]() # Calls overridden method in derived class
Execution Results - All test cases have succeeded!
Page No: 34
Test Case - 1
User Output
This is a vehicle
3 9 3 0 1 0 3 S C 5 2 N E : DI
This is a car
Page No: 35
28
Aim:
Write a Python program to demonstrate encapsulation by creating a class with a private
3 9 3 0 1 0 3 S C 5 2 N E : DI
attribute and accessing it using a public method.
Program Requirements:
• Create a class named Student.
• Inside the constructor, define a private data member named __marks and initialize
it with the value 90.
• Define a public method get_marks() that returns the value of the private data
member.
• Create an object of the class and display the marks using the public method.
Output Format:
Note: The code for the output is already provided. Write the code for the class referring to
the requirements specified.
Source Code:
[Link]
Medicaps University
s = Student()
print("Marks:", s.get_marks())
Test Case - 1
User Output
Marks: 90
Date: 2026-02-
[Link]: 18 Exp. Name: Abstract Base Class
Page No: 36
28
Aim:
Write a Python program to create an abstract base class representing a geometric shape
3 9 3 0 1 0 3 S C 5 2 N E : DI
and implement it using a derived class.
Program Requirements:
• Create an abstract class named Shape using the ABC module.
• Define an abstract method named area() inside the base class.
• Create a derived class named Circle that inherits from the Shape class.
• Implement the area() method in the derived class to calculate the area of a circle
using the formula:
Area = π × r × r
Input Format:
• The first line contains a floating-point number representing the radius of the circle.
Output Format:
• Print the area of the circle in the format:
Area of circle: <value>
Where the 'value' is formatted to two decimal places.
Note:
• Use the abc module to define the abstract base class.
• Use the constant pi from the math module for calculation.
• The result should be displayed as a floating-point value.
Note: Partial code is provided; please complete the missing code according to the Medicaps University
requirements mentioned.
Source Code:
[Link]
Page No: 37
from abc import ABC, abstractmethod
import math
3 9 3 0 1 0 3 S C 5 2 N E : DI
class Shape(ABC):
@abstractmethod
def area(self):
pass
def area(self):
return [Link] * [Link] * [Link]
Medicaps University
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
5
Area of circle: 78.54
Test Case - 2
User Output
12
Area of circle: 452.39
Page No: 38
Test Case - 3
User Output
3 9 3 0 1 0 3 S C 5 2 N E : DI
24.5
Area of circle: 1885.74
Page No: 39
Handling 01
Aim:
Write a Python program to implement a function that reads data from a file and handles
3 9 3 0 1 0 3 S C 5 2 N E : DI
file-related exceptions such as FileNotFoundError and PermissionError.
Program Requirements
1. Define a function named read_file(filename) that:
• Attempts to open the given file in read mode.
• Displays the contents of the file if it exists and is accessible.
• Handles the following exceptions:
• If the file does not exist, display:
Error: File not found
• If permission to read the file is denied, display:
Error: Permission denied
Output Format:
• If the file exists and can be read:
<contents of the file>
• If the file does not exist:
Error: File not found
• If permission is denied:
Error: Permission denied
Source Code:
Medicaps University
[Link]
Page No: 40
# Type Content here...
def read_file(filename):
try:
with open(filename,'r') as file:
3 9 3 0 1 0 3 S C 5 2 N E : DI
content = [Link]()
print(content)
except FileNotFoundError:
print("Error: File not found")
except PermissionError:
print("Error: Permission denied")
filename = input()
read_file(filename)
User Output
[Link]
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Everything matters.
Page No: 41
Test Case - 2
User Output
[Link]
3 9 3 0 1 0 3 S C 5 2 N E : DI
Error: File not found
Page No: 42
01
Aim:
Write a Python program that reads two integers from the user and performs division. The
3 9 3 0 1 0 3 S C 5 2 N E : DI
program should handle the ZeroDivisionError exception gracefully if the
denominator is zero.
Program Requirements:
1. Read two integers:
• The numerator.
• The denominator.
2. Perform the division operation.
3. If the denominator is zero, catch the ZeroDivisionError and display the message:
Cannot divide by zero
4. If the division is valid, display the result of the division.
Output Format:
• If division is possible, print the result of the division formatted to two decimal
places.
• If the denominator is zero, print:
Cannot divide by zero
Source Code:
[Link]
try:
n = int(input()) Medicaps University
d = int(input())
r = n/d
print(f"{r:.2f}")
except ZeroDivisionError:
print("Cannot divide by zero")
Page No: 43
User Output
5
0
Cannot divide by zero
3 9 3 0 1 0 3 S C 5 2 N E : DI
Test Case - 2
User Output
5
5
1.00
User Output
0
5
0.00
Medicaps University
Exp. Name: File Handling with Multiple Date: 2026-04-
[Link]: 21
Page No: 44
Exception Handling 01
Aim:
Write a Python program to read the contents of a file and handle the exceptions
3 9 3 0 1 0 3 S C 5 2 N E : DI
FileNotFoundError and PermissionError gracefully.
Program Requirements:
• Read the file name from the user.
• Attempt to open the file in read mode.
• If the file exists and is accessible, display its contents.
• If the file is not found, display:
Error: File not found
• If the user does not have permission to access the file, display:
Error: Permission denied
Output Format:
• If the file is read successfully:
<contents of the file>
• If the file is not found:
Error: File not found
• If permission is denied:
Error: Permission denied
Source Code:
Medicaps University
[Link]
Page No: 45
try:
f = input()
3 9 3 0 1 0 3 S C 5 2 N E : DI
co = [Link]()
print(co)
except FileNotFoundError:
print("Error: File not found")
except PermissionError:
print("Error: Permisssion denied")
CodeTantra
Start coding in 60 mins
Test Case - 1
[Link]
CodeTantra
Start coding in 60 mins
Test Case - 2
User Output
[Link]
Error: File not found
Exp. Name: File Operations using Date: 2026-04-
[Link]: 22
Page No: 46
Exception Handling 01
Aim:
Write a Python program that opens a file and reads its contents. The program should
3 9 3 0 1 0 3 S C 5 2 N E : DI
demonstrate the use of the finally block to ensure that the file is closed properly,
regardless of whether an exception occurs or not.
Program Requirements:
• Read the file name from the user.
• Attempt to open the file in read mode and display its contents.
• Handle the following exception:
• If the file does not exist, display:
Error: File not found
• Use a finally block to ensure that:
• If the file is opened successfully, it is closed properly, and displays:
Input Format:
• The first line contains a string representing the file name.
Output Format:
• If the file exists and can be opened:
<contents of the file>
File closed successfully
• If the file does not exist:
Error: File not found
No file to close
Source Code:
Medicaps University
[Link]
Page No: 47
file = None
try:
f = input()
3 9 3 0 1 0 3 S C 5 2 N E : DI
file = open(f,'r')
print([Link]())
except FileNotFoundError :
print("Error: File not found")
finally:
if file:
[Link]()
print("File closed successfully")
[Link]
Hello World
[Link]
Python Programming
AI is the future!
[Link]
123 Programming!
Page No: 48
User Output
[Link]
Hello World
File closed successfully
3 9 3 0 1 0 3 S C 5 2 N E : DI
Test Case - 2
User Output
[Link]
Error: File not found
No file to close
Page No: 49
an Email Address 01
Aim:
Write a Python program that uses regular expressions to validate:
3 9 3 0 1 0 3 S C 5 2 N E : DI
1. A variable name, and
2. An email address
Program Requirements
1. Read a string representing a variable name from the user and validate it using regular
expressions based on the following rules:
• The variable name must start with a letter (a–z or A–Z) or underscore (_).
• It may contain letters, digits, and underscores only.
• It must not start with a digit.
2. Read a string representing an email address and validate it based on the following
rules:
Medicaps University
Input Format:
• The first line contains a string representing the variable name.
• The second line contains a string representing the email address.
Output Format:
• Print the validation result for the variable name.
• Print the validation result for the email address.
Source Code:
[Link]
Page No: 50
import re
v = input()
e = input()
3 9 3 0 1 0 3 S C 5 2 N E : DI
var = r'^[A-Za-z_][A-Za-z0-9_]*$'
if [Link](var,v):
print("Valid variable name")
else:
print("Invalid variable name")
dm = r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'
if [Link](dm, e):
Test Case - 1
User Output
my_var1
user@[Link]
Valid variable name
Medicaps University
Valid email address
Test Case - 2
User Output
1var
test@[Link]
Invalid variable name
Valid email address
User Output
Page No: 51
count_value
user#[Link]
Valid variable name
Invalid email address
3 9 3 0 1 0 3 S C 5 2 N E : DI
Test Case - 4
User Output
9num
user@com
Invalid variable name
Invalid email address
Page No: 52
01
Aim:
Write a Python program that uses the built-in math module to perform the following
3 9 3 0 1 0 3 S C 5 2 N E : DI
mathematical operations using user-provided input values:
• Calculate the square root of a given number.
• Calculate the factorial of a given number.
• Calculate the power of a number raised to another number.
Program Requirements
1. Read an integer representing the number whose square root is to be calculated.
2. Read an integer representing the number whose factorial is to be calculated.
3. Read two integers representing the base and exponent for the power calculation.
4. Display the results using the math module functions.
Output Format:
Display the results in the following format:
Square root: <value>
Factorial: <value>
Power: <value>
Constraints:
• n1 ≥ 0
• n2 ≥ 0
Medicaps University
Source Code:
[Link]
Page No: 53
import math
# Read inputs
n1 = int(input())
3 9 3 0 1 0 3 S C 5 2 N E : DI
n2 = int(input())
base = int(input())
exp = int(input())
# Display results
print("Square root:", s)
Test Case - 1
User Output
16
5
2
3
Square root: 4.0 Medicaps University
Factorial: 120
Power: 8.0
Test Case - 2
User Output
0
0
5
3
Page No: 54 3 9 3 0 1 0 3 S C 5 2 N E : DI nahuohC ayviD forP-B_11P_2003SC_5202 Medicaps University
Factorial: 1
Power: 125.0
Exp. Name: Date and Time Handling using Date: 2026-04-
[Link]: 25
Page No: 55
datetime Module 01
Aim:
Write a Python program that reads a date and time from the user and processes it using
3 9 3 0 1 0 3 S C 5 2 N E : DI
Python’s built-in datetime module.
The input will be provided as a single string representing date and time in the format:
YYYY-MM-DD HH:MM:SS
The program must convert this string into a datetime object and display the individual
components of the given date and time, namely the year, month, day, hour, minute, and
second.
Input Format:
• A single line containing a date and time string in the specified format.
Output Format:
Constraints:
• The date provided will be a valid calendar date.
Source Code:
Medicaps University
[Link]
Page No: 56
from datetime import datetime
date_string = input()
3 9 3 0 1 0 3 S C 5 2 N E : DI
# Convert the input string into a datetime object using the given
format
dt = [Link](date_string, "%Y-%m-%d %H:%M:%S")
# Display year
print("Year:",[Link])
# Display month
print("Month:",[Link])
# Display day
# Display hour
print("Hour:",[Link])
# Display minute
print("Minute:",[Link])
# Display second
print("Second:",[Link])
Medicaps University
Test Case - 1
User Output
2024-08-15 10:30:45
Year: 2024
Month: 8
Day: 15
Hour: 10
Minute: 30
Second: 45
User Output
Page No: 57
2021-06-07 03:04:05
Year: 2021
Month: 6
Day: 7
Hour: 3
3 9 3 0 1 0 3 S C 5 2 N E : DI
Minute: 4
Second: 5
Page No: 58
Numpy 01
Aim:
Write a Python program to create two NumPy arrays using user input and perform the
3 9 3 0 1 0 3 S C 5 2 N E : DI
following element-wise operations:
• Addition
• Subtraction
• Multiplication
• Division
Input Format:
• The first line contains an integer n, representing the size of the arrays.
• The second line contains n space-separated integers representing the first array.
• The third line contains n space-separated integers representing the second array.
Constraints:
• 1 ≤ n ≤ 100
• All elements are integers.
• Division by zero will not be provided in input.
Source Code:
Medicaps University
[Link]
Page No: 59
import numpy as np
3 9 3 0 1 0 3 S C 5 2 N E : DI
# Read elements of first array
arr1 = [Link](list(map(int, input().split())))
arr2 = [Link](list(map(int, input().split())))
print(arr1+arr2)
print(arr1-arr2)
print(arr1*arr2)
print(arr1/arr2)
Test Case - 1
User Output
3 Medicaps University
10 20 30
123
[11 22 33]
[ 9 18 27]
[10 40 90]
[10. 10. 10.]
Test Case - 2
User Output
Page No: 60 3 9 3 0 1 0 3 S C 5 2 N E : DI nahuohC ayviD forP-B_11P_2003SC_5202 Medicaps University
[ 16 160]
1.6]
[10 26]
[6 6]
8 16
2 10
[4.
Exp. Name: Data Filtering and Selection Date: 2026-04-
[Link]: 27
Page No: 61
using Numpy 01
Aim:
Write a Python program to create a NumPy array and perform data filtering and selection
3 9 3 0 1 0 3 S C 5 2 N E : DI
based on the following conditions:
• Select all elements greater than 20.
• Select all elements between 15 and 35 (inclusive).
• Select all even numbers from the array.
Program Requirements
• Read an integer n representing the number of elements in the array.
• Read n space-separated integers to create a NumPy array.
• Display the original array.
• Filter and display:
1. Elements greater than 20
Input Format:
• The first line contains an integer n, representing the size of the array.
• The second line contains n space-separated integers representing the elements of
the array.
Output Format:
• First, print the original array.
• Then print the following, each in a line:
1. Elements greater than 20
2. Elements between 15 and 35
3. Even numbers
Medicaps University
Constraints:
• 1 ≤ n ≤ 100
• Array elements are integers.
Note:
• Refer to the visible test cases and strictly match with the input and outputs.
Source Code:
[Link]
Page No: 62
import numpy as np
3 9 3 0 1 0 3 S C 5 2 N E : DI
# Read array elements
# 3. Even numbers
print("Even numbers:")
print(arr[arr%2==0])
Medicaps University
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
7
10 15 20 25 30 35 40
Original Array:
[10 15 20 25 30 35 40]
Elements greater than 20:
[25 30 35 40]
Elements between 15 and 35:
Even numbers:
[10 20 30 40]
Page No: 63
Test Case - 2
User Output
3 9 3 0 1 0 3 S C 5 2 N E : DI
5
13579
Original Array:
[1 3 5 7 9]
Elements greater than 20:
[]
Elements between 15 and 35:
[]
Even numbers:
Medicaps University
Exp. Name: Statistical Analysis using Date: 2026-04-
[Link]: 28
Page No: 64
Pandas 01
Aim:
Write a Python program that uses the Pandas library to read data from a CSV file and
3 9 3 0 1 0 3 S C 5 2 N E : DI
compute the following statistical measures:
1. Mean
2. Median
3. Standard Deviation
Input Format:
• The first line contains a string representing the CSV file name.
Output Format:
• Display the computed statistics in the following format:
Mean:
Medicaps University
[Link]
Page No: 65
import pandas as pd
3 9 3 0 1 0 3 S C 5 2 N E : DI
# Read CSV file
df = pd.read_csv(filename)
# Compute statistics
print("Mean:")
print([Link](numeric_only=True))
print("Median:")
print([Link](numeric_only=True))
Test Case - 1
User Output
[Link]
Medicaps University
Mean:
A 17.5
B 27.5
C 37.5
dtype: float64
Median:
A 17.5
B 27.5
C 37.5
dtype: float64
Standard Deviation:
A 6.454972
B 6.454972
Page No: 66 3 9 3 0 1 0 3 S C 5 2 N E : DI nahuohC ayviD forP-B_11P_2003SC_5202 Medicaps University
dtype: float64
Exp. Name: DataFrame Manipulation Date: 2026-04-
[Link]: 29
Page No: 67
Operations 01
Aim:
Write a Python program to load a dataset from a CSV file into a Pandas DataFrame and
3 9 3 0 1 0 3 S C 5 2 N E : DI
perform the following DataFrame manipulation operations:
• Display the original DataFrame.
• Sort the DataFrame based on the first column.
• Filter and display rows where the values in the first column are greater than the
mean of that column.
• Display the second column of the DataFrame, if it exists.
• Display the first five rows of the DataFrame.
Input Format:
• The first line contains a string representing the CSV file name.
Note:Refer to the visible test cases and strictly match with the input and outputs.
Source Code:
Medicaps University
[Link]
Page No: 68
import pandas as pd
3 9 3 0 1 0 3 S C 5 2 N E : DI
# Load dataset
df = pd.read_csv(filename)
fo = [Link][0]
# Sort DataFrame by first column
print("Sorted DataFrame:")
mo = df[fo].mean()
# Filter rows where values in first column are greater than its
mean
print("Filtered DataFrame:")
print(df[df[fo] > mo])
print("Selected Column:")
if len([Link]) >1:
print(df[[Link][1]])
else:
print("No second column")
# Display first 5 rows
print("First 5 rows of DataFrame:") Medicaps University
print([Link]())
[Link]
Page No: 69
A,B,C
10,20,30
15,25,35
20,30,40
3 9 3 0 1 0 3 S C 5 2 N E : DI
25,35,45
30,40,50
35,45,55
Test Case - 1
User Output
Medicaps University
2 20 30 40
3 25 35 45
4 30 40 50
5 35 45 55
Filtered DataFrame:
A B C
3 25 35 45
4 30 40 50
5 35 45 55
Selected Column:
0 20
1 25
4 40
5 45
Page No: 70
Name: B, dtype: int64
First 5 rows of DataFrame:
A B C
0 10 20 30
3 9 3 0 1 0 3 S C 5 2 N E : DI
1 15 25 35
2 20 30 40
3 25 35 45
4 30 40 50