
Pre-Quiz
Q1 – If a=[20,30,40,50,60], show the value of a[3].
Answer: 50
Explanation:
List index starts with 0. Hence, a[3] refers to the 4th element in the list which is 50.
Q2 – Consider the following code snippet:
1. list = [ [ ] ] * 5
2. list # output?
3. list[0].append(10)
4. list # output?
5. list[1].append(20)
6. list # output?
7. list.append(30)
8. list # output?
Analyse and select the outputs for the lines 2, 4, 6, and 8?
Answer: [[], [], [], [], []]
[[10], [10], [10], [10], [10]]
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]]
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30]
Explanation:
list =[ [ ] ] * 5 creates a list containing 5 empty lists.
list[0].append(10) appends value 10 to the 0th index of every list within the list of lists.
list[1].append(20) appends value 20 to the 1st index of every list within the list of lists.
list.append(30) appends value 30 to the list of lists.
Q3 – Analyse the following code and predict the output.
list1 = [2,4,6,8,10,12,14,16,18,20]
print (list1[0:1],list1[5:7])
Answer: [2] [12, 14]
Explanation:
[0:1] means the element at the 0th index and (1-1)th index, which is again 0th index.
[5:7] means the elements at 5th and 6th index .
Q4 – Create a string with the numbers from 0 to 100, “0123456789101112…”. Choose the correct statement.
Answer: “”.join([`x` for x in range(101)])
Explanation:
The method xrange is deprecated in Python3. Hence eliminate the choices with xrange.
A string with the numbers from 0 to 100 requires the range-end to be 101.
The method join returns a string in which the elements of sequence have been joined by a str separator.
The only statement relevant to our scenario is “”.join([`x` for x in range(101)])
Q5 – “The ‘break’ statement can be used to terminate the ‘if’ condition”. State True or False:
Answer: False
Explanation:
The ‘break’ statement is used to break out of a loop.
Practice Session – Functions
Q1 – Get integer inputs for var1 and var2 from the user.
Dene a function ‘add’, that takes var1 and var2 as arguments and returns its sum (return without parenthesis).
Call the function. Capture the sum in var3 and print.
# variable declaration
var1=int(input())
var1=int(input())
# function definitiondef add(var1,var2):
return var1+var2
# function call and print result[var3=add(var1,var2)]
print(var3)
Practice Session – Module
Q1 – Import sqrt function from math module. Print the square root of 25 using this function.
from math import sqrt
print(sqrt(25))
Practice Session – Numpy package
Q1 – Fill up the code to generate and print 15 random integers between 1 to 100.
import numpy as np
print(np.random.randint(1,100,15))
Post-Quiz function and modules
Q1 –
Veena, a beginner in Python programing has written the code for counting the number of elements in the list. The code that
she has written is given below. Analyse and display the output for the given code.
def addItem(listParam):
listParam += [1]
mylist = [1, 2, 3, 4]
addItem(mylist)
print len(mylist)
Answer: 5
Explanation:
The list – mylist has 4 elements in it. On calling addItem() once, element 1 is added to this list. Length of mylist is 5 after the
function execution.
Q2 – Analyze the code given below and choose the correct output.
d = lambda p: p * 2
t = lambda p: p * 3
x = 2
x = d(x)
x = t(x)
x = d(x)
print x
Answer: 24
Explanation:
After assigning x with 2, d(x) is the rst call to lambda function. The function returns 4(2*2) and is assigned to x.
t(x) is the second call and the function returns 12(4*3) and is assigned to x.
d(x) is the third call and the function returns 24(12*2) and is assigned to x. Now, x is 24.
Q3 – Identify which of the following methods return a string that represents the present working directory.
Answer: os.getcwd()
Explanation:
The function getcwd() (get current working directory) returns a string that represents the present working directory.
Q4 – Describe the process of pickling in Python.
Answer: conversion of a Python object hierarchy into byte stream
Explanation:
Pickling is the process of serializing and de-serializing Python object structures. Serialization is the process of converting an
object in memory into a byte stream which can be stored on disk or sent over network.
Q5 – Select the right set of functions to sterilize an object hierarchy and to desterilize a data stream respectively.
Answer: dumps(), loads()
Explanation:
To sterilize an object hierarchy, the dumps() function must be called. Whereas to desterilize a data stream, the loads function
must be called.
Q6 – Identify which of the following functions can be used to create a symbolic link.
Answer: os.symlink()
Explanation:
The method os.symlink() creates a symbolic link which contains a text string that is automatically interpreted and followed
by the os as a path to another le or directory.
Q7 – Which of the following functions can be used to read data from a le using a le descriptor?
Answer: os.read()
Explanation:
The method read() reads at most n bytes from the le descriptor and will return a string containing all the bytes read.
Q8 – Describe what os.name contains.
Answer: the name of the operating system dependent module imported
Explanation:
You can get the name of the current running Operating System by checking : os.name.
Q9 – State what print(os.geteuid()) prints?
Answer: the user id of the current process
Explanation:
The method os.geteuid() gives the user id of the current process while the method os.getegid() gives the group id of the current process.
Q10 – State what os.close(f) does?
Answer: closes the le descriptor f
Explanation:
When a le descriptor is passed as an argument to os.close() method, it is closed.