Regulation: R22 Code No:
22CS104/7
I B. Tech II Semester Regular/Supplementary Examinations –May, 2024
PYTHON PROGRAMMING
Time: 150 Min (CS&BS/CSE/CSE(CS)/CSE(DS)/CSE(AI&ML)) Max. Marks:80M
SECTION – A
Answer all Four questions 4×10M=40M
1. a) Can keys in a dictionary be mutable types like lists? Justify your answer?
b) Create a dictionary called students where the keys are the names of students and values
are their total marks in an examination. Use appropriate input function to read the
student names and marks and construct the dictionary.
Develop a python function to print the names and the corresponding marks such that the
names appear in alphabetical order.
c) Design and develop a solution for the following problem
Generate mulsum numbers in the given range n1 and n2
Example: n=6, factors=1,2,3,6
Sum of factors=12
Product of factors=36
if 36%12=0, then n is mulsum number
2. a) In what way tuple is different from list. How do you modify the tuple?
b) A tuple of numbers is given. This tuple contains duplicates. Create a new tuple from the
original that contains no duplicates. Print both the tuples.
c) Given a string ‘s’ consisting of words and spaces, return the length of the last word in the
string. A word is a maximal substring consisting of non-space characters only.
(For example, in the string s ="fly me to the moon”, the length of the last substring is 4)
3. a) How do append() and extend() methods in Python lists differ from each other?
b) You are given a large integer represented as an integer array named ‘digits’, where each
digits[i] is the ith digit of the integer. The digits are ordered from most significant to least
significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
For example, if digits = [1,2,3], then the result is [1,2,4]
c) Create a function to generate the below line graph using library package in python?
4. a) Membership operators and Identity operators in Python perform specific functions. What are
these operators, and what tasks do they execute?
1 of 2
Regulation: R22 Code No:
22CS104/7
b) Two matrices A and B are given. You need to find the sum and product of A and B using
the ‘numpy’ arrays and relevant operations. The resultant matrix should be displayed.
c) Create a function to draw a square using the ‘turtle’ package left( ), right( ), forward( )
and backward( ) functions.
SECTION - B
Answer all Two questions 2×20M=40M
5. a) What are the consequences if exception handling is omitted from a program? How do you
handle the exceptions which are encountered in the program?
b) Design and develop the solutions for the following using user defined exceptions
i. Create a function to display first ‘n’ natural numbers with Raise StopIteration
exception when iterations cross first ‘n’?
ii. Develop a function to generate a number randomly. Raise RandomError exception
if number < 0.1
iii. Create a function for voter eligibility check and Raise InvalidAge exception if age
of the candidate is below 18.
iv. Rasie TypeError exception in developing any function when unintentionally try to
apply addition of ‘int’ and ‘string’ variables with ‘ + ’ operator?
c) Develop a function to return array of odd rows and even columns from below numpy
array?
Example:
sampleArray = numpy.array([[3, 6, 9, 12], [15, 18, 21, 24],
[27 ,30, 33, 36], [39 ,42, 45, 48], [51 ,54, 57, 60]]).
Expected Output:
Printing Input Array
[[ 3 6 9 12]
[15 18 21 24]
[27 30 33 36]
[39 42 45 48]
[51 54 57 60]]
Printing array of odd rows and even columns
[[ 6 12]
[30 36]
[54 60]]
6. a) Differentiate 'r', 'w', and 'a' file modes in Python using programming examples?
b) Design a function to create a file called “numbers.dat” that stores only single digit and
double digit numbers. Also develop another function to create a file
“single_digit_fact.dat” to store the factorial of all single digit numbers of the file
“numbers.dat”. Create another file called “sum_digits.dat” to store sum of the digits of
two-digit numbers of the file “numbers.dat”.
Display the contents of both files of “single_digit_fact.dat” and “sum_digits.dat”.
c) i) Design and develop the solution to find the difference between the strings Write a
function in Python that accepts two string parameters. The first parameter will be a string
of characters, and the second parameter will be the same string of characters, but they’ll
be in a different order and have one extra character. The function should return that extra
character.
Sample input: first parameter is “eueiieo” and the second is “iieoedue,”
Sample output: “d.”
ii) For the purpose of this challenge, shadow sentences are sentences where every word is
the same length and order but without any of the same letters. Write a function that
accepts two parameters that may or may not be shadows of each other. The function
should return True if they are and False if they aren’t.
2 of 2
Regulation: R22 Code No:
22CS104/7
1. Sample Input: “they are round” and “fold two times,” which are shadow sentences
Sample Output: True
2. Sample Input: “his friends” and “our company”
Sample Output: False
2 of 2