
Pre-Quiz
Q1 – Predict the right output for the following code.
i = 5
while True:
if i%9 == 0:
break
print(i)
i += 1
Answer: 5 6 7 8
Explanation:
Here, i is assigned the value 5. Execution of while loop stops when i%9 evaluates to 0. That is, when i is 9. Until then, i is
printed and incremented by 1. Hence, 5 6 7 8.
Q2 – Predict the correct output for the following code.
x = “abcdef”
i = “a”
while i in x:
print(‘i’, end = ” “)
Answer: i i i i i i …
Explanation:
Since a in abcdef (i.e, i in x) is always true, the character i will get printed innite no of times in the same line, separated by
white spaces since end = ” “.
Q3 – Analyse and predict the value of b after the execution of this loop?
b = 0
for a in range(0, 10, 2):
b += a + 1
Answer: 25
Explanation:
Here, a will take values from 0 to 10-1, stepping by 2. Hence, a takes values 0,2,4,6 an 8.
b+=a+1 is equivalent to b=b+a+1.
When a is 0, b=0+0+1=1
When a is 2, b=1+2+1=4
When a is 4, b=4+4+1=9
When a is 6, b=9+6+1=16
When a is 8, b=16+8+1=25
After loop execution, b is 25.
Q4 – Select the statement that you can use for iterating over a block of statements N times
Answer: for n in range(N):
Explanation:
“for n in range(N):” is the only correct syntax for iterating over a block of statements N times.
Q5 – Analyse and select the statements that correctly explain the output of the given sample code.
if(True):
print(“A”)
if(100):
print(“B”)
if(” “):
print(“C”)
if ([[]]):
print(“D”)
Answer: B
Explanation:
The correct answer is: In the context of Boolean operations, the following values are interpreted as true:
True, numeric non-zero of all types, non-empty strings and containers containing empty containers.
Practice Session – Lists
Q1 – Create a list L1 with the elements 1,2,3,4,5,6.
L1=[1,2,3,4,5,6]
Q2 – Display the third element of the list L1 using negative indexing.
L1[-4]
Q3 – Given the list : L1=[1,2,3,4,5,6], how will you display from the third element to the end of the list without specifying the end index?
L1[2:]
Q4 – Given the list : L1=[1,2,3,4,5,6], how will you display the third and forth element by specifying positive start and end index ?
L1[2:4]
Q5 – Given the list : L1=[1,2,3,4,5,6], ll up the code to create a new list L2 that contains only the even numbers from the list L1 using
the concept of list comprehension.
L2=[element for element in L1
if(element%2)==0
Practice Session – Tuples
Q1 – Create a tuple (with parenthesis), new_tuple with 5 elements – 101,102,103,104,105.
new_tuple=(101,102,103,104,105)
Q2 – Given the tuple new_tuple = (101, 102, 103, 104,105), how will you display its third element ?
Answer: D
new_tuple[2]
Q3 – Fill up the code pertaining to enumerate method that adds index to the tuple, to bind index with the associated value using a
for loop.
tup=(1,2,3,4,5)
for index,tup in
enumerate(tup)
print(index, tup)Practice Session – Dictionaries
Q1 – Create a dictionary – Student_detail with the following key-value pair. Key – Name and Value – Ram.
Student_detail={‘Name’:’Ram’}
Q2 – To the dictionary Student_detail, add a new key-value pair. Key – Languages known and Value – English, Tamil, Hindi Student_detail={‘Name’:’Ram’}
Student_detail[‘Languages known’]=[‘English’,’Tamil’,’Hindi’]
Post Quiz
Q1 – Analyse the code and predict the output.
myList = [1, 5, 5, 5, 5, 1]
max = myList[0]
indexOfMax = 0
for i in range(1, len(myList)):
if myList[i] > max:
max = myList[i]
indexOfMax = i
print(indexOfMax)
Answer: 1
Explanation:
The max is assigned with the rst value in the list (value at 0th index). The for loop ranges from 1 to 6-1. For every iteration, the value of max is updated, when a value greater than max is encountered. Finally, the index of max is printed. The index of max
value 5 is 1 because there had not been a value greater than 5 after 5 had been encountered at the 1st index.
Q2 – Evaluate the given code snippet. Ignoring the order, which line of code will you use in the place of “# LINE A” to
generate the output shown here ?
keys = [‘key1’, ‘key2’, ‘key3’, ‘key4’, ‘key5’]
vals = [‘val1’, ‘val2’, ‘val3’, ‘val4’, ‘val5’]
# LINE A
print(mydict)
# Output: {‘key1’: ‘val1’, ‘key2’: ‘val2’, ‘key3’: ‘val3’, ‘key4’: ‘val4’, ‘key5’: ‘val5’}
Answer: for x in range(len(keys)):
mydict[keys[x]] = vals[x]
Explanation:
We have a list of keys and a list of values. We must associate keys with values based on the list index. Create an empty
dictionary. Iterate through this dictionary, length of key_list no. of times. During each iteration, assign each key (from the
key_list) with value at the equivalent index (in the value_list).
Q3 – Evaluate the given code. What is the value pertaining to (a == b, a is b) after the execution ?
a = [1, 2, 3, 1]
b = [1, 2, 3, 1]
Answer: (True, False)
Explanation:
The == operator compares values of both the operands (a and b) and checks for value equality. Hence, true.
The is operator checks whether both the operands refer to the same object or not. Hence, false.
Q4 – Describe what will happen during the dictionary update dict[k] = v, if k isn’t present in dict.
Answer: The entry (k, v) is added to the dictionary
Explanation:
When trying to assign a value(v) to the key(k) that’s not present in the dict, a new key-value pair gets created.
Q5 – Select the statement(s) that is/are true.
Answer: Tuples are structured, lists are ordered, Tuples are immutable, lists are mutable.
Explanation:
Tuples are immutable and structured.
Lists are mutable and ordered.
Recommended
- Pass or Fail
- Arrange Names
- Create Quarters
- Non-Working Doctors
- AEIMA’s Online Courses
- Search Student Data
- Residents Information
- Password Protection