Test Index
Functions in Python
© examsnet.com
Question : 22 of 23
Marks:
+1,
-0
(a) What possible output(s) are expected to be displayed on screen at the time of execution of the following program :
import randomM=[5,10,15,20,25,30]for i in range(1,3): first=random.randint(2,5)- 1 sec=random.randint(3,6)- 2 third=random.randint(1,4) print(M[first],M[sec],M[third],sep="#")(i)
10#25#1520#25#25(ii)5#25#2025#20#15(iii)30#20#2020#25#25(iv)10#15#25#15#20#10# [2](b) The code given below deletes the record from the table employee which contains the following record structure :E_code - StringE_name - StringSal - IntegerCity - StringNote the following to establish connectivity between Python and MySQL :• Username is root• Password is root• The table exists in a MySQL database named emp.• The details (E_code,E_name,Sal,City) are the attributes of the table.Write the following statements to complete the code :Statement 1 – to import the desired library.Statement 2 – to execute the command that deletes the record with E_code as ‘E101’.Statement 3 – to delete the record permanently from the database.import ___________ as mysql # Statement 1def delete( ) : mydb=mysql.connect(host="localhost",user="root", passwd="root",database="emp") mycursor=mydb.cursor( ) ___________________ # Statement 2 ___________________ # Statement 3 print ("Record deleted") [3]OR(a) Predict the output of the code given below :def makenew(mystr): newstr="" count=0 for i in mystr: if count%2!=0: newstr=newstr+str(count) else : if i.lower(): newstr=newstr+i.upper() else: newstr=newstr+i count+=1 print(newstr)makenew("No@1") [2](b) The code given below reads the following records from the table employee and displays only those records who have employees coming from city ‘Delhi’:E_code - StringE_name - StringSal - IntegerCity - StringNote the following to establish connectivity between Python and MySQL :• Username is root• Password is root• The table exists in a MySQL database named emp.• The details (E_code,E_name,Sal,City) are the attributes of the table.Write the following statements to complete the code :Statement 1 – to import the desired library.Statement 2 – to execute the query that fetches records of the employees coming from city ‘Delhi’.Statement 3 – to read the complete data of the query (rows whose city is Delhi) into the object named details, from the table employee in the database.import ______________ as mysql # Statement 1def display(): mydb=mysql.connect(host="localhost",user="root", passwd="root",database="emp") mycursor=mydb.cursor() ______________________________ # Statement 2 details = ______________________ # Statement 3 for i in details: print (i)[3]
[CBSE 2023 Set 4]
Solution:
(a)
Output:
first = randint(2,5) - 1 gives 1 to 4, so M[first] is 10, 15, 20 or 25.sec = randint(3,6) - 2 gives 1 to 4, so M[sec] is 10, 15, 20 or 25.third = randint(1,4) gives 1 to 4, so M[third] is 10, 15, 20 or 25.So 5 (M[0]) and 30 (M[5]) can never be printed, and sep="#" puts # only between the values, never at the end.(ii) has 5: not possible. (iii) has 30: not possible. (iv) ends with #: not possible.Possible output: (i)10#25#1520#25#25(b)
import mysql.connector as mysql # Statement 1def delete(): mydb=mysql.connect(host="localhost",user="root", passwd="root",database="emp") mycursor=mydb.cursor() mycursor.execute("DELETE FROM employee WHERE E_code='E101'") # Statement 2 mydb.commit() # Statement 3 print("Record deleted")Statement 1 imports the MySQL connector library; Statement 2 runs the DELETE query through the cursor; Statement 3 commit() makes the deletion permanent in the database.OR (a)| count | i | Rule | Added |
| 0 | N | even: i.lower() is 'n' (non-empty, true), add i.upper() | N |
| 1 | o | odd: add str(count) | 1 |
| 2 | @ | even: '@'.lower() is '@' (true), add '@'.upper() | @ |
| 3 | 1 | odd: add str(count) | 3 |
N1@3OR (b)
import mysql.connector as mysql # Statement 1def display(): mydb=mysql.connect(host="localhost",user="root", passwd="root",database="emp") mycursor=mydb.cursor() mycursor.execute("SELECT * FROM employee WHERE City='Delhi'") # Statement 2 details = mycursor.fetchall() # Statement 3 for i in details: print(i)fetchall() reads all the rows of the result into details as a list of tuples, which the loop prints one by one.© examsnet.com
Go to Question: