Font size
WorksheetsUnderstanding the concept of list
Total questions: 25
Worksheet time: 20mins
What will be the output of the following Python code?
>>> a=(1,2,3,4)
>>> del(a[2])
Now, a=(1,2,4)
Now, a=(1,3,4)
Now a=(3,4)
Error as tuple is immutable
Choose the correct way to access value 20 from the following tuple
tuple1= ("Orange", [10, 20, 30], (5, 15, 25))
tuple1[1:2](1)
tuple1[1:2][1]
tuple1[1][1]
All of the above
Predict the output:
1
2
[13, 18, 11, 16, 13, 18, 13, 2]
0
2
[13, 18, 11, 16, 13, 18, 13, 3]
5
2
[13, 18, 11, 16, 13, 18, 13, 3]
1
2
[13, 18, 11, 16, 13, 18, 13, 3]
What will the following code result in?
True
[9, 7, 5, 3, 1]
False
[9, 7, 5, 3, 1]
True
[1, 3, 5, 7, 9]
None of the above
Out of the following, what is correct syntax to copy one list into another?
listA=listB[]
listA=listB[:]
listA=listB[]()
listA=list(listB)
What gets printed?
H
a
Hasan
Dia
What will be the output of following Python code?
(15,11,16,12)
(15,11,17,16)
(15,11,17,16,12)
Error
Carefully read the given code fragments and figure out the errors that the code may produce.
t5=t3*(3)
t4=t3*3
t3=(6,7)
t6=t3*(3,)
Predict the output :-
3
2
False
3
2
2
2
False
3
3
3
3
False
2
2
2
3
False
2
3
Find the output generated by the code fragment :
plane=("Passengers","Luggage")
plane[1]="Snakes"
plane=("Passengers","Snakes")
plane=("Passengers","Snakes","Luggage")
plane=("Snakes","Passengers","Luggage")
Error
What is the output of the following code?
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48]
[4, 8, 16, 20, 28, 32, 40, 44]
[0, 12, 24, 36, 48]
None of the above
Find the error :
L1=[1,11,21,31]
An=L1.remove(41)
(a) Value error : 41 not in list
(b) remove() doesn't return any value
(c) remove() function is not applicable on lists
(d) both (a) & (b)
Given a list L1=[3,4.5,12,25.7,[2,1,0,5],88], which function can change the list to [3,4.5,12,25.7,88]?
del L1[4]
L1.pop(4)
L1.pop(-2)
All the above
Write the output of the following code:
>>> L=[“Amit”,”Anita”,”Zee”,”Longest Word”,123]
>>> print(max(L))
Longest Word
Zee
Amit
Error
What is the output of the following list operation
aList = [10, 20, 30, 40, 50, 60, 70, 80]
print(aList[2:5])
print(aList[:4])
print(aList[3:])
[20, 30, 40, 50]
[10, 20, 30, 40]
[30, 40, 50, 60, 70, 80]
[30, 40, 50]
[10, 20, 30, 40]
[40, 50, 60, 70, 80]
None of these
If list=[17,23,41,10] then list.append(32) will result
[32,17,23,41,10]
[17,23,41,10,32]
[10,17,23,32,41]
[41,32,23,17,10]
Which of the following are true of Python lists?
A list may contain any type of object except another list
A given object may appear in a list more than once
All elements in a list must be of the same type
These represent the same list:
['a', 'b', 'c']
['c', 'a', 'b']
There is no conceptual limit to the size of a list
List a is defined as follows:
a = ['a', 'b', 'c']
Which of the following statements adds 'd' and 'e' to the end of a, so that it then equals ['a', 'b', 'c', 'd', 'e']:
a += ['d', 'e']
a[-1:] = ['d', 'e']
a += 'de'
a.append(['d', 'e'])
a[len(a):] = ['d', 'e']
You have a list a defined as follows:
a = [1, 2, 7, 8]
Write a Python statement using slice assignment that will fill in the missing values so that a equals [1, 2, 3, 4, 5, 6, 7, 8].
a[2:2] = [3, 4, 5, 6]
a[1:2] = [3, 4, 5, 6]
a[2:3] = [3, 4, 5, 6]
a[3:5] = [3, 4, 5, 6]
Consider this assignment statement:
a, b, c = (1, 2, 3, 4, 5, 6, 7, 8, 9)[1::3]
Following execution of this statement, what is the value of b:
5
4
3
6
7
Q-2. The code below manipulates a Python list using the slicing operator. What will be the output?
a=[1,2,3,4,5,6,7,8,9]
a[::2]=10,20,30,40,50,60
print(a)
ValueError: Attempt to assign a sequence of size 6 to an extended slice of size 5
[10, 2, 20, 4, 30, 6, 40, 8, 50, 60]
[1, 2, 10, 20, 30, 40, 50, 60]
[1, 10, 3, 20, 5, 30, 7, 40, 9, 50, 60]
The code below uses a Python list and iterates using the range() function. What will be the output?
arr = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
arr[i - 1] = arr[i]
for i in range(0, 6):
print(arr[i], end = " ")
1 2 3 4 5 6
2 3 4 5 6 1
1 1 2 3 4 5
2 3 4 5 6 6
Which of the following statements would create a tuple in python?
mytuple = ("apple", "banana", "cherry")
mytuple[123] = ("apple", "banana", "cherry")
mytuple = ("2" * ("apple", "banana", "cherry"))
None of the these
What will be the output of the following Python code?
>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]
olleh
hello
h
o
