0% found this document useful (0 votes)
11 views12 pages

Python Sem-1

For bsc Computer science hons pyq

Uploaded by

hs1762366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

Python Sem-1

For bsc Computer science hons pyq

Uploaded by

hs1762366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 12
it) ee (T hi is Westion Pet contains 12 printed pages] Your Roll Nowe" Sr. N, 0. * Qtetn Paper: 1920 Uniqu ; NS PSP Code > 2342011101 Nam morte Paper : Programming using Python (DSC-1) Name of the Course - BSe. (H) Computer Scienté Semester - Duration : 3 Hours vfasiemrn Blois «88 Instructions for Candidates 1. Write your Roll No. on the top immediately on receipt of this question paper. Section A is compulsory. wm 3, Attempt any 4 questions from Section B. 4. Parts of a question must be answered together. SECTION A (Compulsory) tis an algorithm? Write an algorithm to solve ha to (iW atic equation ne the quadt P.T.O, 1020 0 check n is prime or not. The Write the python ev operations : 1020 3 pemistry: (i) Access the value of the § (ii) Extract the value of the if key is use the default value § ° , found. ses ict i (iii) Create a copy of 4 groupDic dictionary object newGroup- : : fter (iv) print the dictionary newGroup 4 : s ion. removing the element with the key secti XN (oy Define a class Rectangle having .the following (6) structure: Attributes: length, breadth Methods: __init__() for initializing the attributes. getLength() which returns the length of the rectangle. perimeter( ) which retums the perimeter of the rectangle. (f) Find the output for the following python script Pts: (8) P.T.o, 1020 4 (i) myString = Hibs Rveryong Wethome athe Session! Q print(myString [len(myString) :: -1}) © print(myString [:-15] + myString [-15:]) print(myString.partition('Welcome')) print(myString.rfind('to')) eo = ['Red', 'Blue', 'Green’] Y= ['Yellow','White'] X.extend(Y) X.append(23) X.pop() X.remove("Yellow') print(X) print(Y) Gi try: num = 8 print(num + ‘hello') Print(num / 4) except ZeroDivisionError: print(‘Divided by zero’) except(ValueEtror, TypeError): print('Error occurred’) finally: print('Stop') 1020 5 1.28, asc: 31} (a) monthDays = (anuray' 31, see Month = monthDays Month{'February'] += ! \ onthDays) Print(‘monthDays before clea-7™ print(Month before cleat" Month) Month.clear() + monthD2ys) Print(‘monthDays after clear-”» mon print(Month after clear’, Month) SECTION B WS Write a program that accepts x and n as input to compute the following series. (6) x? x3 4 x-at a7 qty mterms (hf Consider the following code snippet (9) for k in ‘Computer Science': if k = 'e: S1 print(k, end=",") Compare the output when S1 is replaced with a of the following statements: c PTO, 1020 6 (i) break (ii) continue (iii) pass CY (3 Consider the following tuples (6) T1 =(100, 200, 300) T2 = (‘Monday', 'Tuesday', 'Wednesday') AK, wire a function swapTuple(T1, T2) to swap the values of T1 and T2. The expected output is as follows: Tl = (‘Monday', 'Tuesday’', 'Wednesday'). T2 = (100, 200, 300) i) Write a function mergeTuple(T1, T2) to return a list of tuples containing the Corresponding element from tuples T1 and T2. The expected output is as follows: T3 = [(‘Monday', 100), (‘Tuesday', 200), (‘Wednesday', 300)] 1020 , 7 : (9) (b) Consider list L1 as follows * L1 = [100, 200, 300, [400,500]] ; ing lists : Write code snippets to create the following Phas as a shallow copy of Ll G13 as a deep copy of LI. Demonstrate the effect of the following modifications in L1, L2 and L3: G11) = 900 Gd LIBI(01 = 700 x (a) Write a function doubleDict() that creates the dictionary Dict1 where the keys are numbers between 1 and 5 and values are twice the keys. For example: if the key is 5, its value is 10. The function should return the dictionary Dict1. Write a program that calls doubleDict( ) and print : in the values of the following operations when appli 4 to the Dict1. . (i) maximum key (ii) sum of keys (7) P.T.o, 8 1020 ; ists Lstl (b) Consider two lists Pstl and po) dec] follows : aed ag (8) st] = ['green!, "blue'] Lst2 = ['blue'; 'yellow'] Write a python script to do the following : (i) Convert list Lstl as set gy and Lstd 4 S set $2. (ii) Add the elements of the list ['black’, 'cyan’] to $2. (iii) Find the symmetric difference between sets S1 and S2. (iv) Create a set mewSet using set comprehension containing elements of SI with ‘s’ added at the end of each element. The newSet should appear 4s: newSet = {'blues’, 'greens'} s, total) ‘a A . e(mark ) Consider the function percentag ¢ "for a ark that computes the percentage of m Student, def percentage (mar! 9 ks, total): try: percent = (marks/total) * 100 except ValueError: print('Value Error’) except TypeError: print('Type Error') except ZeroDivisionError: print('Zero Division Error') except: print(‘Any other error’) else: print(percent) finally: print('Completed!') Explain the output corresponding to the follows ng function calls. \ of percentage (17.0, 20.0) (iiY percentage (19.0, 0.0) PTo. 1020 10 oe percentage ('200.0', 200.0) A Define the following functions to perform the operations on a string. (9) (i) countVowel(Str1) to count the vowels in a string. (ii) replaceChar(Str1) to replace all occurrence of the character 'a' with a space. rs reverseString(Str1) to reverse a string Write a program to call these functions and provide the output for the given string Strl. fee a ei Strl = 'Happiness depends upon ourselves! se (a) Find the error(s) in the following code snippets: ; (3) (i) filel = open('Myfile', 'w') filel.read() filel .close() (ii) tuplel = (2120, 'abc’) del tuple1 [0] > 020 11 (b) Explain the type of exception raised in the following | statements : (4) | (i) x=0 ~ | print(5/x) Gi) print('The amount for the day is :' + 300) | (ii) int(‘Morning’y (iv) L1 = (11,22,33,44,55} print(L1[5]) (c) Describe the following metho ds for the class objects with suitable examples. (8) (@) _init_ (i) _str_ ii) __del__ (iv) __main_ Koya rite a function sumDigits(Num) which computes the sum of digits of a number Num ang returns it. (5) PTo, 1020 ite a program to read a file MYFile ang Perfor, ae llowing operations: : the fo 12 (10) UG print the total number o¢ lines in the file (iif copy even lines of the fite 44 evenFile and odd lines to another file Named oddFile. a file named (500)

You might also like