PART A
C
O
1. Predict the output for the following code. 1
a=[12,13,14,15,16,17]
print(13 not in a)
print(18 not in a)
2. List any four operators in python programming with example 1
3. What is ‘len’ function? Give example for how it is used on strings. 2
4. Write a Python program to print numbers from 1 to 10, skipping 5 and stopping 2
if a number greater than 8 is encountered.
5. Write a Python program using list comprehension to create a list of squares of 3
all even numbers between 1 and 10.
6. What is the difference between shallow copy and deep copy operations 3
performed on lists?
7. Describe the purpose of the raise keyword in Python exception handling and 4
provide an example
8. List some few common Exception types 4
9. Write a Numpy program to create a 10x10 matrix, in which the elements on the 5
borders will be equal to 1, and inside 0.
10. How can you handle missing values in a DataFrame using Pandas? 5
PART B
C
O
11. (a) Write the following python program. 1
i) To check whether a given number is Armstrong or not.(6 Marks)
ii) To print Fibonacci series. (7 Marks)
(b i) Develop a python program to get the details from the user such as 1
) student name, age, address and CGPA and display them. (7 Marks)
ii) Build a python program to get the principal, rate and time from the
user and find the simple interest for it. (6 Marks)
12. (a) i) Write a Python function to find the maximum of three numbers using
recursive function. (4 Marks)
2
ii) Write a python function to calculate simple interest. Suppose the
customer is a senior citizen, he is being offered a 10 percent rate of
interest; for all other customers the rate of interest is 8 percent. (5
Marks)
iii) Write a python program to calculate the factorial of a number without
recursion function. (4 Marks)
(b i) Write a program to determine if a student passes or fails based on
) marks in 3 subjects (pass if average ≥ 50). (6 Marks)
2
ii) Write a python program to check whether entered string is palindrome
or not.(7 Marks)
13. (a) i) Kritika was asked to accept a list of even numbers but she did not put
the relevant condition while accepting the list. Write a user-defined
function oddtoeven(L) that accepts the List L as an argument and converts
3
all the odd numbers into even by multiplying them by 2. (6 Marks)
ii) Consider the following unsorted list: 95, 79, 19, 43, 52, 3. Write the
passes of bubble sort for sorting the list in ascending order till the 4th
iteration(7 Marks)
(b i) Write a Python program to flatten a nested list using list comprehension.
) (5 Marks)
3
ii) Write a program to count the frequency of each character in a string
using a dictionary. (5 Marks)
iii) Write a Python program to extract every second element from a list
using slicing. (3 Marks)
14. (a) i) Write a number game program. Ask the user to enter a number. If the
number is greater than the number to be guessed, raise a ValueTooLarge
exception. If the value is smaller than the number to be guessed then,
raise a ValueTooSmall exception and prompt the user to enter again. Quit
4
the program when the user enters the correct number. (6 Marks)
ii) Write a program which infinitely prints natural numbers. Raise the
StopIteration exception after displaying the first 20 numbers to exit from
the program. (3 Marks)
iii) Write a python program to Replace all spaces from text with – (dash) in
a text file.
(4 Marks)
(b i) Write a program that raises a user-defined exception if a negative
) number is input.(5 Marks)
4
ii) Write a program that accepts two filenames from the command line
and copies content from the first file to the second.(8 Marks)
15. (a) i) Write a Pandas program to split the following dataframe into groups
based on customer id and create a list of order date for each group. (6
Marks)
ord_no purch_amt ord_date customer_id salesman_id
5
0 70001 150.50 2012-10-05 3005 5002
1 70009 270.65 2012-09-10 3001 5005
2 70002 65.26 2012-10-05 3002 5001
3 70004 110.50 2012-08-17 3009 5003
4 70007 948.50 2012-09-10 3005 5002
5 70005 2400.60 2012-07-27 3007 5001
6 70008 5760.00 2012-09-10 3002 5001
ii) Write a Pandas program to join the two dataframes with matching
records from both sides where available.(7 Marks)
data1:
key1 key2 P Q
0 K0 K0 P0 Q0
1 K0 K1 P1 Q1
2 K1 K0 P2 Q2
3 K2 K1 P3 Q3
data2:
key1 key2 R S
0 K0 K0 R0 S0
1 K1 K0 R1 S1
2 K1 K0 R2 S2
3 K2 K0 R3 S3
(b Develop a Pandas program to analyze employee data with the following
) specifications:
employee_data = {'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma', 'Frank', 5
'Grace', 'Hank', 'Ivy', 'Jack'],
'salary': [75000, 60000, 80000, [Link], 55000, 95000, 72000, [Link],
50000, 88000],
'daycount': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'eligibility': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Perform the following task:
1. Select the rows where the daycount is less than 2 and salary is greater
than 60000.(5 Marks)
2. Display the names of employees whose salary is missing (NaN).(4
Marks)
3. Display the details of employees whose eligibility is 'no'. (4 Marks)
PART C
C
O
16. (a) A company tracks product sales in a tuple list: sales = [('Pen', 120),
('Notebook', 80), ('Pencil', 150), ('Eraser', 50)]. Complete the following
task.
3
1. Convert the tuple list to a dictionary (product → quantity). And Identify
products with sales above 100. (7 Marks)
2. Swap the highest and lowest selling product quantities. (4 Marks)
3. Generate a text-based histogram of sales using asterisks. (4 Marks)
(b i) Write a python program to get 3 students’ details such as student
) register no, name, department, blood group, English marks and store
3
them as a list of dictionaries. Display the student name who scores the
marks between 46 and 50. Add extra marks 3 and display the updated
marks with the student’s name. Also, display the students whose blood
group is "B+" (8 Marks)
ii) Discuss any three python list methods with example programs. (7
Marks)
PART A
C
O
1. Predict the output for the following code snippet. 1
x = 10.0//5
print(type(x))
2. Write a single Python statement to swap the values of two variables a and b 1
without using a temporary variable.
3. Write a for loop that prints numbers from 0 to 57 use range function in python. 2
4. Write a python program to add two numbers using anonymous function. 2
5. Compare and contrast lists and arrays in Python. 3
6. Given t = (1, 2, 3, 2, 2), write a Python statement to count how many times 2 3
appears.
7. What is the role of the __init__ method in a Python class? Provide an example 4
to illustrate its usage.
8. Create a user-defined exception called NegativeNumberError and write a 4
program to raise it if a negative number is input.
9. Demonstrate indexing and slicing of a 1-D NumPy array a = 5
[Link]([10,20,30,40,50]) to get elements 20, 30, 40.
10. Create a Pandas Series with elements [10, 20, 30] and index labels ['a','b','c']. 5
PART B
C
O
11. (a) i) Write a Python program to print all prime numbers between 1 and 50. (7 1
Marks)
ii) Develop a python program to find the area and perimeter of a circle. (6
Marks)
(b i) Develop a python program to get the details from the user such as 1
) student name, age, address and CGPA and display them. (7 Marks)
ii) Build a python program to get the principal, rate and time from the
user and find the simple interest for it. (6 Marks)
12. (a) i) Write a program to convert a string to uppercase and replace all spaces
with underscores using string methods.(6 Marks)
2
ii) Develop a python program to print a pattern using functions.(7 Marks)
(b i) Write a Python function to find the greatest common divisor (GCD) of
) two numbers using a recursive function. (6 Marks)
2
ii) Write a Python function to calculate the monthly payment on a loan.
Suppose the customer is a veteran, they are being offered a discount rate
of 3 percent; for all other customers the rate is 5 percent. (7 Marks)
13. (a) i) Write a python program to replace last value of tuples in a list. (5 Marks)
Sample input:[(10,20,40),(40,50,60),(70,80,90)]
3
Sample output:[(10,20,100),(40,50,100),(70,80,100)]
ii) Write a python program to return only negative values from the tuples
of positive and negative numbers. (4 Marks)
iii) Discuss the tuple methods with examples. (4 Marks)
(b i) Generate a list of all numbers between 1 and 50 that are divisible by
) both 3 and 5 using list comprehension. (6 Marks)
3
ii) Write a python program that scans an email address and forms a tuple
of user name and domain. (4 Marks)
iii) Write a python program to return only positive values from the tuples
of positive and negative numbers. (3 Marks)
14. (a) i) Implement a Python program using a class named "BankAccount" to
manage account transactions. Include methods for deposit, withdrawal,
4
and display of the account balance. (7 Marks)
ii) Create a Python program that uses a class to store and display
employee information, including name and salary. Print the total count of
employees whose details are available.
(6 Marks)
(b i) Write a program that handles multiple exceptions (ValueError and
) ZeroDivisionError) in a single try block.(7 Marks)
4
ii) Write a Python program to create a text file named [Link] and
write some text into it.(6 Marks)
15. (a) Develop a Pandas program to modify a DataFrame named
‘employee_data’ with the following specifications:
employee_data = {'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma', 'Frank',
'Grace', 'Hank', 'Ivy', 'Jack'],
'salary': [75000, 60000, 80000, [Link], 55000, 95000, 72000, [Link],
50000, 88000],
'experience': [2, 4, 3, 5, 2, 6, 3, 1, 2, 4],
'qualified': ['yes', 'no', 'yes', 'no', 'yes', 'yes', 'no', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Perform the following:
1. Replace the 'qualified' column with True for 'yes' and False for 'no'.(7
Marks)
2. Correct the spelling of 'Grace' to 'Greyce'.(6 Marks)
(b Develop a Python program to analyze data from the CSV file
) 'sales_data.csv' and perform the following task.
5
1. Read the CSV file 'sales_data.csv'. (3 Marks)
2. Display the first 5 rows of the data. (2 Marks)
3. Display the last 10 rows of the data. (2 Marks)
4. Find the number of rows in the file. (3 Marks)
5. Find the number of columns in the file. (3 Marks)
PART C
(Case study/Comprehensive type Questions)
C
O
16. (a) A publishing company wants to analyze the frequency of words in a set of
articles.
4
Write a Python program that:
1. Accepts a text file name from the command line. ( 3 Marks)
2. Reads the file and counts the frequency of each word. (4 Marks)
3. Displays the top 5 most frequent words with their counts. (4
Marks)
4. Handles exceptions if the file is missing or unreadable. (4 Marks)
(b i) Develop a Python program to manage a library system using classes to
) perform the following task.
1. Implement a class named 'Book' that keeps a record of book code, title,
and availability. (3 Marks)
4
2. Display a menu to the user with all available books and prompt them to
enter the book code and quantity required. (2 Marks)
3. Generate a receipt and display the total amount. (2 Marks)
ii) Create a Python program for a language learning app and perform the
following.
1. Create a language learning app using two files - '[Link]' and
'[Link]'. (2 Marks)
2. Read a vocabulary word from '[Link]' and display it with
options to the user. (2 Marks)
3. Prompt the user to select the correct meaning. (2 Marks)
4. Open '[Link]' and display the correct meanings. (2 Marks)
_____________________
PART A
C
O
1. List the literal collections in Python and give an example of each. 1
2. Predict the output for the following code snippet. 1
string1 = "Engineering College"
print("g" in string1)
print("a" in string1)
3. How to split strings and what function is used to perform that operation? 2
4. Find the error. 2
str = “Hello world”
str[6] = ‘W’
print(str)
5. Predict the output of the following code. 3
list = [[10, 20, [30, 40, [50.60]]]]
print(list[0])
print(list[0][2])
print(list[0][2][2])
6. Develop a program to count and print the number of occurrences of each 3
character in the message using dictionary.
7. Distinguish between files and modules. 4
8. Write a Python program to create a new text file named [Link] and write 4
some text into it.
9. Explain the difference between [Link]() and * operator for NumPy arrays. 5
10. What is a Pandas Series? How is it different from a DataFrame? 5
PART B
CO
11. (a) Write short notes on types of operators in python with appropriate 1
example. (13 Marks)
(b i) Write a python program to read two integers and perform simple 1
) arithmetic calculation.(+,-,*,/,//,%,**) (7 Marks)
ii) Develop a python program to get the details from the user such as
employee name, age, address and designation, salary and display them.
(6 marks)
12. (a) i) Write a program that prints numbers from 1 to 20, skips multiples of 5
using continue, and stops if the number exceeds 15 using break.(7 Marks)
2
ii) Write a function square_root(x) that returns the square root of a
number and call it for user input.(6 Marks)
(b i) Write a function gcd(a, b) that returns the greatest common divisor of
) two numbers using recursion.(6 Marks)
2
ii) Write a program to gather individual's details such as name, age, and
monthly income, and determine if the person is eligible for a housing
loan. Eligibility criteria: age between 25 to 50 years and monthly income
more than 30000. (7 Marks)
13. (a) i) Write a python program to add two matrices using nested lists. (5
Marks)
3
ii) Write a python program to create a list of numbers in the range 1 to
10. Then delete all the even numbers from the list and print the list with
odd numbers. (4 Marks)
iii) Write a python program to find the sum and mean of the elements in a
list. (4 Marks)
(b i) Write a program to remove all duplicate elements from a list. (6 Marks)
)
ii) Write a program to invert a dictionary (swap keys and values). 3
For example : original_dict = {'a': 1, 'b': 2, 'c': 3}
inverted_dict = {1: 'a', 2: 'b', 3: 'c'} (7 Marks)
14. (a) i) Write a Python program to know the cursor position and print the text
according to below-given specifications.
1. Print the initial position. (2 Marks)
2. Move the cursor to 4th position. And display next 5 characters. (2
Marks)
4
3. Move the cursor to the next 10 characters and print the current cursor
position. (2 Marks)
4. Print next 10 characters from the current cursor position. (2 Marks)
ii) Write a python program that uses class to store and display the
employee's name and salary and print the total employee count whose
details are available. (5 Marks)
(b i) Demonstrate using try, except, else, finally with file operations.(6
) Marks)
4
ii) Create a class Circle with a radius attribute and a method to calculate
its area.(7 Marks)
15. (a) Develop a Pandas program to split the following given DataFrame into
groups based on school code and class. Also split the following DataFrame
5
into groups based on class and create a list of names for each group. (13
Marks)
Original DataFrame:
School code class name date_Of_Birth age
S1 s001 V Alberto Franco 15/05/2002 12
S2 s002 V Gino Mcneill 17/05/2002 12
S3 s003 VI Ryan Parkes 16/02/1999 13
S4 s001 VI Eesha Hinton 25/09/1998 13
S5 s002 V Gino Mcneill 11/05/2002 14
S6 s004 VI David Parkes 15/09/1997 12
(b i) Develop a python program to read all the information from the csv file
) ‘[Link]’ and
display the following 5
1. First 5 rows. (2 Marks)
2. Last 10 rows. (2 Marks)
3. Find the number of rows in the file. (2 Marks)
4. Find the number of columns in the file. (2
Marks)
ii) Write a NumPy program to insert a new axis within a 2-D array. 2-D
array of shape (5, 3). New shape will be (5, 1, 3), (5, 3, 1), (1, 5, 3). (5
Marks)
PART C
C
O
16. (a) A retail company stores monthly sales data of 4 products over 6 months.
The sales values are stored in a NumPy array.
1. Create a NumPy array of shape (6, 4) representing sales data. (4 Marks) 5
2. Using indexing, extract the sales of the 2nd product for all 6 months. (4
Marks)
3. Convert the NumPy array into a Pandas DataFrame with columns =
[‘Product1’, ‘Product2’, ‘Product3’, ‘Product4’]. (4 Marks)
4. Using Pandas, find the product with the highest average sales. (3 Marks)
(b Develop a Pandas program to
)
1. Join the two dataframes with matching records from both sides
available. (5 Marks)
2. Join the two dataframes using keys from the left dataframe only. (5
Marks)
3. Join the two dataframes using keys from the right dataframe only. (5 5
Marks)
student_data1:
student_id name marks
0 S1 Danniella Fenton 200
1 S2 Ryder Storey 210
2 S3 Bryce Jensen 190
3 S4 Ed Bernal 222
4 S5 Kwame Morin 199
student_data2:
student_id name marks
0 S4 Scarlette Fisher 201
1 S5 Carla Williamson 200
2 S6 Dante Morse 198
3 S7 Kaiser William 219
4 S8 Madeeha Preston 201
_____________________