List Worksheet
List Worksheet
S, 1
4. [50, 60, 70] ✓ 3. I, 3 ✓
Multiple Choice Question 10 4. I, 1
QuestionsQuestion 1 To find the last element of list namely Question 18
'smiles' in Python, .......... will be used. Which of the following is a standard
List can contain values of these types:
1. smiles[0] Python library function and not an
1. Integers floats lists tuples all
of these 2. smiles[-1] ✓ exclusively list function?
3. smiles[lpos] 1. append( )
Question 2
4. smiles[:-1] 2. remove( )
Which of the following will create an
Question 11 3. pop( )
empty list?
Out of the following, what is correct 4. len( ) ✓
1. L = [ ] ✓
syntax to copy one list into another? Question 19
2. L = list(0)
1. listA = listB[ ] Which of the following can add only
3. L = list( ) ✓
2. listA = listB[:] ✓ one value to a list?
4. L = List(empty) 1. add( )
3. listA = listB[ ]( )
Question 3
4. listA = list(listB) ✓ 2. append( ) ✓
Which of the following will return the
Question 12 3. extend( )
last element of a list L with 5
What is printed by the Python code? 4. none of these
elements?
print(list(range(3))) Question 20
1. L[5]
1. [0, 1, 2, 3] Which of the following can add a list
2. L[4] ✓ of elements to a list?
2. [1, 2, 3]
3. L[-1] ✓
3. [0, 1, 2] ✓ 1. add( )
4. L[6] 2. append( )
4. 0, 1, 2
Question 4 3. extend( ) ✓
Question 13
If L = [1, 2] then L * 2 will yield 4. none of these
Which of the following commands will
1. [1, 2] * 2 Question 21
create a list?
2. [1, 2, 2] Which of the following will always
1. listl = list( )
3. [1, 1, 2, 2] return a list?
2. listl = [ ]
4. [1, 2, 1, 2] ✓ 3. listl = list([1, 2, 3]) 1. max( )
Question 5
4. all of these ✓ 2. min( )
If L1 = [1, 3, 5] and L2 = [2, 4, 6] then 3. sort( )
Question 14
L1 + L2 will yield 4. sorted( ) ✓
What is the output when we execute
1. [1, 2, 3, 4, 5, 6] Question 22
list("hello")?
2. [1, 3, 5, 2, 4, 6] ✓
1. ['h', 'e', 'l', 'l', 'o'] ✓ Which of the following can delete an
3. [3, 7, 11] element from a list if the index of the
2. ['hello']
4. [1, 3, 5, [2, 4, 6]] element is given?
3. ['llo']
Question 6 1. pop( )
4. ['olleh']
Given a list L= [10, 20, 30, 40, 50, 60, 2. remove( )
Question 15
70], what would L[1 : 4] return? 3. del ✓
What gets printed?
1. [10, 20, 30, 40] 4. all of these
names = ['Hasan', 'Balwant',
2. [20, 30, 40, 50] Question 23
'Sean', 'Dia']
3. [20, 30, 40] ✓ Which of the following can delete an
print(names[-1][-1])
4. [30, 40, 50] element from a list, if its value is
1. H
Question 7 given?
2. n
Given a list L= [10, 20, 30, 40, 50, 60, 1. pop( )
3. Hasan
70], what would L[2 : -2] return? 2. remove( ) ✓
4. Dia
1. [10, 20, 30, 40]
2. [20, 30, 40, 50] 5. a ✓ 3. del
Question 16 4. all of these
3. [20, 30, 40] Question 24
What is the output of the following
4. [30, 40, 50] ✓ Which of the following searches for an
l = [None] * 10
Question 8 element in a list and returns its index?
print(len(l))
Given a list L= [10, 20, 30, 40, 50, 60, 1. search( )
70], what would L[-4 : -1] return? 1. 10 ✓
2. 0 2. find( )
1. [20, 30, 40] 3. index( ) ✓
2. [30, 40, 50] 3. Syntax Error
4. None 4. lsearch( )
3. [40, 50, 60] ✓ Question 25
Question 17
4. [50, 60, 70] Which of the following can copy a list
Consider the list aList - ["SIPO", [1, 3,
Question 9 to another list?
5, 7] ]. What would the following code
Given a list L= [10, 20, 30, 40, 50, 60, 1. list( ) ✓
print?
70], what would L[-3 : 99] return? 2. new( )
print(aList[0][1],
1. [20, 30, 40]
aList[1][1]) 3. copy( ) ✓
2. [30, 40, 50]
1. S, 3 4. = operator
Fill in the Blanks Question 7 5. Double the list
The append( ) can add an element in 6. Insert 25 at index 3
Question 1 the middle of a list. Answer
Lists are mutable data types and thus False listA = [8, 9, 10]
their values can be changed. Question 8 1. listA[1] = 17
Question 2 The insert( ) can add an element in the 2. listA.extend([4, 5, 6])
To create an empty list, middle of a list. 3. listA.pop(0)
function list() can used. True 4. listA.sort()
Question 3 Question 9 5. listA = listA * 2
The + operator adds one list to the end The del statement can only delete list 6. listA.insert(3, 25)
another list. slices and not single elements from a Question 4
Question 4 list. If a is [1, 2, 3]
The * operator replicates a list. False 1. what is the difference (if any)
Question 5 Question 10 between a * 3 and [a, a, a]?
To check if an element is in The del statement can work similar to 2. is a * 3 equivalent to a + a +
list, in operator is used. the pop( ) function. a?
Question 6 True 3. what is the meaning of a[1:1]
To delete a list slice from a list, del = 9?
statement is used
Type A : Short Answer
4. what's the difference between
Question 7 Questions/Conceptual a[1:2] = 4 and a[1:1] = 4?
A nested list contains another list as its Questions Answer
member. 1. a * 3 ⇒ [1, 2, 3, 1, 2, 3, 1, 2,
Question 8 Question 1 3]
The insert() function is used to insert Discuss the utility and significance of [a, a, a] ⇒ [[1, 2, 3], [1, 2, 3],
element at a designated position in a Lists in Python, briefly. [1, 2, 3]]
list. Answer So, a * 3 repeats the elements
Question 9 Python lists are containers that can of the list whereas [a, a, a]
The pop() function is used to delete store an ordered list of values of same creates nested list.
element to remove an element from or different data types together in a 2. Yes, both a * 3 and a + a + a
designated index in a list. single variable. The fact that elements will result in [1, 2, 3, 1, 2, 3,
Question 10 of a list need not be homogeneous 1, 2, 3]
The extend() function can append a list makes them highly adaptable and 3. a[1:1] = 9 will cause an error
elements to a list. powerful data structure in Python. because when list is modified
Question 11 Lists provide fast access to its elements using slices, the value being
The sort() function sorts a list and using index numbers. Python lists are assigned must be a sequence
makes changes in the list. mutable which makes them memory but 9 is an integer not a
Question 12 efficient. They serve as the basic sequence.
The sorted() function sorts a list and building blocks for programs that 4. Both a[1:2] = 4 and a[1:1] = 4
returns another list. process large amounts of data. will cause error because when
Question 2
True/False Questions What do you understand by
list is modified using slices,
the value being assigned must
Question 1 mutability? What does "in place" be a sequence but 4 is an
The list( ) and copy( ) are the similar memory updation mean? integer not a sequence.
functions. Answer Assuming the question was
False Mutability means that the value of an a[1:2] = [4] and a[1:1] = [4],
Question 2 object can be updated by directly a[1:2] = [4] will change
The pop( ) and remove( ) are similar changing the contents of the memory element at index 1 to 4 as
functions. location where the object is stored. a[1:2] gives a slice with a[1]
False There is no need to create another copy as its only element. Thus, a
Question 3 of the object in a new memory location becomes [1, 4, 3]. Coming to
A = [ ] and A = list( ) will produce the with the updated values. This updation a[1:1] = [4], a[1:1] returns an
same result. of the existing memory location of the empty slice so 4 is inserted
True object is called as in place memory into the list at index 1. Thus, a
Question 4 updation. becomes [1, 4, 2, 3].
Lists once created cannot be changed. Question 3 Question 5
False Start with the list [8, 9, 10]. Do the What's a[1 : 1] if a is a list of at least
Question 5 following using list functions: two elements? And what if the list is
To sort a list, sort( ) and sorted( ), both 1. Set the second entry (index 1) shorter?
can be used. to 17 Answer
True 2. Add 4, 5 and 6 to the end of a[x:y] returns a slice of the sequence
Question 6 the list from index x to y - 1. So, a[1 : 1] will
The extend( ) adds a single element to 3. Remove the first entry from return an empty list irrespective of
a list. the list whether the list has two elements or
False 4. Sort the list
less as a slice from index 1 to index 0 a = pop(1) modify the required parts of the list.
is an invalid range. For example,
Question 6 # removes the last element lst = [1, 2, 3, 4, 5]
How are the statements lst = lst + 3 and # i.e. 8 from the list and lst2 = lst[1:4] #lst2 is [2,
lst += [3] different, where lst is a list? # stores in variable b 3, 4]
Explain. b = pop()
Answer Question 9 #Using Slices for list
The statement lst = lst + 3 will give What does each of the following modification
error as + operator in Python requires expressions evaluate to? Suppose that lst[0:2] = [10, 20] #lst
that both its operands should be of the L is the list becomes [10, 20, 3, 4, 5]
same type but here one operand is list ["These", ["are", "a", "few", "words"], Question 11
and other is integer. The statement lst "that", "we", "will", "use"]. Does the slice operator always produce
+= [3] will add 3 at the end of the lst as 1. L[1][0::2] a new list?
+= when used with lists requires the 2. "a" in L[1][0] Answer
operand on the right side to be an 3. L[:1] + L[1] Slice operator copies only the
iterable and it will add each element of 4. L[2::2] requested elements of the original list
the iterable to the end of the list. 5. L[2][2] in L[1] into a new list.
Question 7 Answer Question 12
How are the statements lst += "xy" and 1. ['are', 'few'] Compare lists with strings. How are
lst = lst + "xy" different, where lst is a 2. True they similar and how are they
list? Explain. 3. ['These', 'are', 'a', 'few', different?
Answer 'words'] Answer
The statement lst = lst + "xy" will give 4. ['that', 'will'] The similarity between Lists and
error as + operator in Python requires 5. True Strings in Python is that both are
that both its operands should be of the Explanation sequences. The differences between
same type but here one operand is list 1. L[1] returns ["are", "a", them are that firstly, Lists are mutable
and other is string. The statement lst "few", "words"]. L[1][0::2] but Strings are immutable. Secondly,
+= "xy" will add 'x' and 'y' at the end of returns a slice of ["are", "a", elements of a list can be of different
the lst as += when used with lists "few", "words"] starting at types whereas a String only contains
requires the operand on the right side index 0 covering every characters that are all of String type.
to be an iterable and it will add each alternate element till the end Question 13
element of the iterable to the end of the of the list. So, final output is What do you understand by true copy
list. ['are', 'few']. of a list? How is it different from
Question 8 2. L[1][0] is "are". As "a" is shallow copy?
What's the purpose of the del operator present in "are" so output is Answer
and pop method? Try deleting a slice. True. True copy of a list means that the
Answer 3. L[:1] return L[0] i.e. elements of the original list are copied
The del statement is used to remove an ["These"]. L[1] returns ["are", to new memory locations and the new
individual element or elements "a", "few", "words"]. + list contains references to these new
identified by a slice. It can also be used operator adds the two in a memory locations for each element of
to delete all elements of the list along single list to give the final the list. Hence, in case of true copy
with the list object. For example, output as ['These', 'are', 'a', changes made to the original list will
lst = [1, 2, 3, 4, 5, 6, 7, 'few', 'words']. not reflect in the copied list and vice
8] 4. L[2::2] returns a slice of L versa.
del lst[1] # delete starting at index 2 covering Incase of shallow copy of a list, the
element at index 1 every alternate element till the elements of the original list are not
del lst[2:5] # delete end of the list. So, final output copied to new memory locations. Both
elements from index 2 to 4 is ['that', 'will']. the new list and the original list refer to
del lst # delete complete 5. L[1] is ["are", "a", "few", the same memory locations for the
list "words"]. L[2][2] is "a". As elements of the list. Hence, changes
pop() method is used to remove a "a" is present in L[1] so made to one of the list reflect in the
single element from the given position output is True. other list as well.
in the list and return it. If no index is Question 10 Question 14
specified, pop() removes and returns What are list slices? What for can you An index out of bounds given with a
the last element in the list. For use them? list name causes error, but not with list
example, Answer slices. Why?
lst = [1, 2, 3, 4, 5, 6, 7, List slice is an extracted part of the list Answer
8] containing the requested elements. The When we use an index, we are
list slice is a list in itself. All list accessing a constituent element of the
# removes element at operations can be performed on a list list. If the index is out of bounds there
# index 1 i.e. 2 from slice. List slices are used to copy the is no element to return from the given
# the list and stores required elements to a new list and to index hence Python throws list index
# in variable a out of range error whereas list slicing
always returns a subsequence and result back to lst so lst will be changed 2. L1[4]
empty subsequence is a valid sequence. to [1, 3, 5, 1, 3, 5, 1, 3, 5]. 3. L1[4:5]
Thus, when a list is sliced outside the (ii) 4. L1[1::2]
bounds, it still can return empty lst + 3 will cause an error as both Question 5
subsequence and hence Python gives operands of + operator should be of Given a list L1 = [3, 4.5, 12, 25.7, [2,
no errors and returns empty same type but here one operand is list 1, 0, 5], 88], which function can
subsequence. and the other integer. change the list to:
Question 15 lst += [3] will add 3 to the end of lst so 1. [3, 4.5, 12, 25.7, 88]
What is the difference between lst becomes [1, 3, 5, 3]. 2. [3, 4.5, 12, 25.7]
appending a list and extending a list? Question 2 3. [ [2, 1, 0, 5], 88]
Answer Given two lists: Answer
L1 = ["this", 'is', 'a', 'List'], L2 = 1. L1.pop(4)
Appending a list Extending a list
["this", ["is", "another"], "List"] 2. del L1[4:6]
For appending to a list, append() function is used. Which of the following expressions
For extending a list, extend() function is used. 3. del L1[:4]
will cause an error and why? Question 6
The append() function can add a single element to 1. L1The ==extend()
L2 function can add multipleWhat will the
elements fromfollowing code result
a list supplied to it in?
the end of a list. 2. L1.upper(
as argument.) L1 = [1, 3, 5, 7, 9]
3. L1[3].upper( ) print (L1 == L1.reverse( ) )
After append(), the length of the list will increase 4. L2.upper(After extend()
) the length of the list willprint
increase (L1)
by the length of the list
by 1 element only. 5. L2[1].upper(
given as argument
) to extend() Answer
6. L2[1][1].upper( ) Output
Question 16
Answer False
Do functions max( ), min( ), sum( )
work with all types of lists. • L1.upper( ) will cause an error [9, 7, 5, 3, 1]
as upper() method can be Explanation
Answer
called with Strings not Lists. L1 is not equal to its reverse so L1 ==
No, for max() and min() to work on a
list, the list must contain all elements • L2.upper( ) will cause an error L1.reverse( ) gives False but
as upper() method can be L1.reverse( ) reverses L1 in place so
of same type (non-complex type) and
called with Strings not Lists. after that statement executes, L1
for sum() to work, the list must contain
• L2[1].upper( ) will cause an becomes [9, 7, 5, 3, 1].
the elements which can be added such
error as L2[1] is a list — [ Question 7
as numbers.
"is", "another"] and upper() Predict the output:
Question 17
method cannot be called on my_list= [ 'p', 'r', 'o',
What is the difference between sort( )
Lists. 'b', 'l' , 'e', 'm']
and sorted( )?
Question 3 my_list[2:3] = []
Answer
From the previous question, give print(my_list)
sort( ) output of expressions that do not result my_list[2:5]
sorted( ) = []
in error. print(my_list)
It modifies the list it is called on. That is, the sorted
Answer Answer
It creates a new list containing a sorted version of the list passed to it as
list is stored in the same list; a new list is not
• L1 argument.
== L2 gives output
It does notasmodify the list Output
passed as a parameter.
created. ['p', 'r', 'b', 'l', 'e',
false because L1 is not equal
to L2. 'm']
It can take any iterable sequence type, such as'r',
['p', a list 'm']
or a tuple etc, and it
It works on a list and modifies it. • L1[3].upper( ) gives
always returns outputlist
a sorted as irrespective of the type of sequence passed to
Explanation
'LIST'
it. because L1[3] is 'List' my_list[2:3] = [] removes element at
and upper() function converts
index 2 of my_list so it becomes ['p',
It does not return anything (no return value). It it toIt uppercase.
returns a newly created sorted list.'r', It does
'b', 'l',not
'e',change the passed removes
'm']. my_list[2:5]
modifies the list in place. • L2[1][1].upper(
sequence. ) gives output elements at indexes 2, 3, and 4 so now
as 'ANOTHER' because L2[1]
Type B: Application Based my_list becomes ['p', 'r', 'm'].
["is", "another"] and L2[1][1]
Question 8
Questions is "another". upper() function
Predict the output:
converts it to uppercase.
Question 1 List1 = [13, 18, 11, 16, 13,
Question 4
What is the difference between 18, 13]
Given a list L1 = [3, 4.5, 12, 25.7, [2,
following two expressions, if lst is print(List1.index(18))
1, 0, 5], 88]
given as [1, 3, 5] print(List1.count(18))
1. Which list slice will return
(i) lst * 3 and lst *= 3 List1.append(List1.count(13)
[12, 25.7, [2, 1, 0, 5]]?
(ii) lst + 3 and lst += [3] )
2. Which expression will return
Answer [2, 1, 0, 5]? print(List1)
(i) 3. Which list slice will return Answer
lst * 3 will give [1, 3, 5, 1, 3, 5, 1, 3, 5] Output
[[2, 1, 0, 5]]?
but the original lst will remains 1
4. Which list slice will return
unchanged, it will be [1, 3, 5] only. 2
[4.5, 25.7, 88]?
lst *= 3 will also give [1, 3, 5, 1, 3, 5, [13, 18, 11, 16, 13, 18, 13,
Answer
1, 3, 5] only but it will assign this 3]
1. L1[2:5]
Explanation
List1.index(18) gives the first index of As we can see, outputs of the two parts L1 = [3, 3, 8, 1, 3, 0, '1',
element 18 in List1 which in this case are different. The reason is that in part '0', '2', 'e', 'w', 'e',
is 1. List1.count(18) returns how many (a), the statement L3 = L2 creates a 'r']
times 18 appears in List1 which in this shallow copy of L2 in L3 i.e. both the print(L1[: :-1])
case is 2. List1.count(13) returns 3 as variables L2 and L3 point to the same print(L1[-1:-2:-3])
13 appears 3 times in List1. list. Hence, when element at index 1 of print(L1[-1:-2:-3:-4])
List1.append(List1.count(13)) add this L2 is changed to 5, that change is Answer
3 to the end of List1 so it becomes [13, visible in L3 also. On the other hand in The line print(L1[-1:-2:-3:-4]) causes
18, 11, 16, 13, 18, 13, 3]. part (b), the statement L3 = list(L2) an error as its syntax is invalid. The
Question 9 creates a true copy (also called deep correct syntax for slicing a list is
Predict the output: copy) of L2 so L3 points to a different L1[start:stop:step].
Odd = [1,3,5] list in memory which has the same Question 15
print( (Odd +[2, 4, 6])[4] ) elements as L2. Now when element at What will be the output of following
print( (Odd +[12, 14, index 1 of L2 is changed to 5, that code?
16])[4] - (Odd +[2, 4, change is not visible in L3. x = ['3', '2', '5']
6])[4] ) Question 12 y = ''
Answer Find the errors: while x:
Output 1. L1 = [1, 11, 21, 31] y = y + x[-1]
4 2. L2 = L1 + 2 x = x[:len(x) - 1]
10 3. L3 = L1 * 2 print(y)
Explanation 4. Idx = L1.index(45) print(x)
Odd + [2, 4, 6] will return [1, 3, 5, 2, 4, Answer print(type(x), type(y))
6]. The element at index 4 of this list is • Line 2 — L2 = L1 + 2 will Answer
4 so the first output is 4. (Odd +[12, result in error as one element Output
14, 16])[4] is 14 and (Odd +[2, 4, of + is a list and other is an 523
6])[4] is 4. 14 - 4 = 10 which is the integer. In Python, operands []
second output. of + operator should be of <class 'list'> <class 'str'>
Question 10 same type. Explanation
Predict the output: • Line 4 — Idx = L1.index(45) The loop while x will continue
a, b, c = [1,2], [1, 2], [1, will cause an error as 45 is not executing as long as the length of list x
2] present in the list L1. is greater than 0. y is initially an empty
print(a == b) Question 13a string. Inside the loop, we are adding
print (a is b) Find the errors: the last element of x to y and after that
Answer L1 = [1, 11, 21, 31] we are removing the last element of x
Output An = L1.remove(41) from x. So, at the end of the loop y
True Answer becomes 523 and x becomes empty.
False L1.remove(41) will cause an error as Type of x and y are list and str
Explanation 41 is not present in L1. respectively.
As corresponding elements of list a and Question 13b Question 16
b are equal hence a == b returns True. Find the errors: Complete the code to create a list of
a is b returns False as a and b are two L1 = [1, 11, 21, 31] every integer between 0 and 100,
different list objects referencing two An = L1.remove(31) inclusive, named nums1 using Python,
different memory locations. print(An + 2) sorted in increasing order.
Question 11 Answer Answer
Predict the output of following two An + 2 will cause an error because nums1 = list(range(101))
parts. Are the outputs same? Are the remove() function does not return the Question 17
outputs different? Why? removed element so An will be None. Let nums2 and nums3 be two non-
(a) Addition operator (+) does not allow empty lists. Write a Python command
L1, L2 = [2, 4] , [2, 4] any of its operands to be None hence, that will append the last element of
L3 = L2 it will raise a TypeError. nums3 to the end of nums2.
L2[1] = 5 Question 14a Answer
print(L3) Find the errors: nums2.append(nums3[-1])
(b) L1 = [3, 4, 5] Question 18
L1, L2 = [2, 4] , [2, 4] L2 = L1 * 3 Consider the following code and
L3 = list(L2) print(L1 * 3.0) predict the result of the following
L2[1] = 5 print(L2) statements.
print(L3) Answer bieber = ['om', 'nom',
Answer The line print(L1 * 3.0) causes an error 'nom']
Output of part (a) is: as Python does not allow multiplying a counts = [1, 2, 3]
[2, 5] list with a non-int number and 3.0 is of nums = counts
Output of part (b) is: float type. nums.append(4)
[2, 4] Question 14b 1. counts is nums
Find the errors: 2. counts is add([1, 2], [3, 4])
Answer Write a program to increment the replace all of the entries in the list that
1. Output is True as both nums elements of a list with a number. are greater than 10 with 10.
and counts refer to the same Solution Solution
list. lst = eval(input("Enter a l = eval(input("Enter list
2. This will cause an error as add list: ")) having numbers between 1 &
function is not defined in the print("Existing list is:", 12: "))
above code. lst)
Question 19 for i in range(len(l)):
What is the output of the following n = int(input("Enter a if l[i] > 10:
code? number: ")) l[i] = 10
numbers = list(range(0, 51,
4)) for i in range(len(lst)): print("List after removing
results = [] lst[i] += n numbers greater than 10:")
for number in numbers: print(l)
if not number % 3: print("List after Output
increment:", lst) Enter list having numbers
results.append(number) Output between 1 & 12: [1, 3, 15,
print(results) Enter a list: [1, 2, 3, 4, 8, 20]
Answer 5] List after removing numbers
Output Existing list is: [1, 2, 3, greater than 10:
[0, 12, 24, 36, 48] 4, 5] [1, 3, 10, 8, 10]
Explanation Enter a number: 10 Question 5
list(range(0, 51, 4)) will create a list List after increment: [11, Ask the user to enter a list of strings.
from 0 to 48 with a step of 4 so 12, 13, 14, 15] Create a new list that consists of those
numbers will be [0, 4, 8, 12, 16, 20, 24, Question 2 strings with their first characters
28, 32, 36, 40, 44, 48]. For loop will Write a program that reverses a list of removed.
traverse the list one number at a time. integers (in place). Solution
if not number % 3 means if number % Solution l1 = eval(input("Enter a
3 is equal to 0 i.e. number is divisible l = eval(input("Enter a list of strings: "))
by 3. The numbers divisible by 3 are list: ")) l2 = []
added to the results list and after the print("Original list:", l)
loop results list is printed. l.reverse() for i in range(len(l1)):
Question 20 print("Reversed list:", l) l2.append(l1[i][1:])
Following code prints the given list in Output
ascending order. Modify the code so Enter a list: [1, 2, 3, 4, print("List after removing
that the elements are printed in the 5] first characters:")
reverse order of the result produced by Original list: [1, 2, 3, 4, print(l2)
the given code. 5] Output
numbers = list(range(0, 51, Reversed list: [5, 4, 3, 2, Enter a list of strings:
4)) 1] ["red", "green", "blue",
i = 0 Question 3 "pink", "cyan"]
while i < len(numbers): Write a program that inputs two lists List after removing first
print(numbers[i] , end = and creates a third, that contains all characters:
" ") elements of the first followed by all ['ed', 'reen', 'lue', 'ink',
i += 3 elements of the second. 'yan']
# gives output as : 0 12 24 Solution Question 6
36 48 l1 = eval(input("Enter first Write a program to check if a number
Answer list: ")) is present in the list or not. If the
numbers = list(range(0, 51, l2 = eval(input("Enter number is present, print the position of
4)) second list: ")) the number. Print an appropriate
i = len(numbers) - 1 l3 = l1 + l2 message if the number is not present in
while i >= 0: print("Joined List:", l3) the list.
print(numbers[i] , end = Output Solution
" ") Enter first list: [1, 2, 3, l = eval(input("Enter list:
i -= 3 4, 5] "))
Output Enter second list: [11, 12, n = int(input("Enter number
48 36 24 12 0 13, 14, 15] to search: "))
Type C: Programming Joined List: [1, 2, 3, 4, 5,
11, 12, 13, 14, 15] if n in l:
Practice/Knowledge based Question 4 print(n, "found at
Questions Ask the user to enter a list containing index", l.index(n))
numbers between 1 and 12. Then else :
Question 1
print(n, "not found in 1444, 1521, 1600, 1681, Output
list") 1764, 1849, 1936, 2025, Enter two lists of same size
Output 2116, 2209, 2304, 2401, Enter first list(L): [3, 1,
Enter list: [1, 3, 15, 8, 2500] 4]
20] Question 7c Enter second list(M): [1, 5,
Enter number to search: 15 Create the following lists using a for 9]
15 found at index 2 loop: List N:
The list ['a','bb','ccc','dddd', . . . ] that [4, 6, 13]
============================ ends with 26 copies of the letter z. Question 9
========= Solution Write a program rotates the elements
l = [] of a list so that the element at the first
Enter list: [1, 3, 15, 8, index moves to the second index, the
20] for i in range(1, 27): element in the second index moves to
Enter number to search: 25 l.append(chr(i + 96) * the third index, etc., and the element in
25 not found in list i) the last index moves to the first index.
Question 7a Solution
Create the following lists using a for print("Created List:") l = eval(input("Enter the
loop: print(l) list: "))
A list consisting of the integers 0 Output print("Original List")
through 49. Created List: print(l)
Solution ['a', 'bb', 'ccc', 'dddd',
l = [] 'eeeee', 'ffffff', l = l[-1:] + l[:-1]
'ggggggg', 'hhhhhhhh',
for i in range(50): 'iiiiiiiii', 'jjjjjjjjjj', print("Rotated List")
l.append(i) 'kkkkkkkkkkk', print(l)
'llllllllllll', Output
print("List with integers 'mmmmmmmmmmmmm', Enter the list: [8, 10, 13,
from 0 to 49:") 'nnnnnnnnnnnnnn', 25, 7, 11]
print(l) 'ooooooooooooooo', Original List
Output 'pppppppppppppppp', [8, 10, 13, 25, 7, 11]
List with integers from 0 to 'qqqqqqqqqqqqqqqqq', Rotated List
49: 'rrrrrrrrrrrrrrrrrr', [11, 8, 10, 13, 25, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 'sssssssssssssssssss', Question 10
9, 10, 11, 12, 13, 14, 15, 'tttttttttttttttttttt', Write a program that reads the n to
16, 17, 18, 19, 20, 21, 22, 'uuuuuuuuuuuuuuuuuuuuu', display nth term of Fibonacci series.
23, 24, 25, 26, 27, 28, 29, 'vvvvvvvvvvvvvvvvvvvvvv', The Fibonacci sequence works as
30, 31, 32, 33, 34, 35, 36, 'wwwwwwwwwwwwwwwwwwwwwww', follows:
37, 38, 39, 40, 41, 42, 43, 'xxxxxxxxxxxxxxxxxxxxxxxx', • element 0 has the value 0
44, 45, 46, 47, 48, 49] 'yyyyyyyyyyyyyyyyyyyyyyyyy', • element 1 has the value 1
Question 7b 'zzzzzzzzzzzzzzzzzzzzzzzzzz' • every element after that has
Create the following lists using a for ] the value of the sum of the
loop: Question 8 two preceding elements
A list containing the squares of the Write a program that takes any two The beginning of the sequence looks
integers 1 through 50. lists L and M of the same size and adds like:
Solution their elements together to form a new 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
l = [] list N whose elements are sums of the The program prompts for element and
corresponding elements in L and M. prints out the value of that element of
for i in range(1, 51): For instance, if L = [3, 1, 4] and M = the Fibonacci sequence.
l.append(i * i) [1, 5, 9], then N should equal [4,6,13]. Thus:
Solution • input 7, produces 13
print("List with square of print("Enter two lists of • input 9, produces 34
integers from 1 to 50:") same size") Hints:
print(l) L = eval(input("Enter first A Don't try to just type out the entire
Output list(L): ")) list. It gets big very fast. Element 25 is
List with square of integers M = eval(input("Enter second 75205. Element 100 is
from 1 to 50: list(M): ")) 354224848179261915075. So keep
[1, 4, 9, 16, 25, 36, 49, N = [] upper limit of n to 20.
64, 81, 100, 121, 144, 169, Solution
196, 225, 256, 289, 324, for i in range(len(L)): n = int(input("Enter n: "))
361, 400, 441, 484, 529, N.append(L[i] + M[i])
576, 625, 676, 729, 784, if (n > 20):
841, 900, 961, 1024, 1089, print("List N:") print("n should be less
1156, 1225, 1296, 1369, print(N) than or equal to 20")
else : Solution start = int(input("Enter
a = 0 l1 = eval(input("Enter list start index: "))
b = 1 of numbers: ")) stop = int(input("Enter stop
c = a + b num = int(input("Enter the index: "))
for i in range(3, n + number to sum with (num):
1): ")) slice = l[start : stop + 1]
a = b mx = max(slice)
b = c l2 = [] mi = min(slice)
c = a + b
for i in l1: print("Maximum =", mx)
print(n, "term of l2.append(i + num) print("Minimum =", mi)
Fibonacci series =", c) Output
Output print("New list:") Enter the list: [89, 42, 12,
Enter n: 7 print(l2) 56, 35, 2, 8, 7, 13, 69]
7 term of Fibonacci series = Output Enter start index: 3
13 Enter list of numbers: [10, Enter stop index: 8
20, 30, 40, 50] Maximum = 56
============================ Enter the number to sum with Minimum = 2
========= (num): 15 Question 14
New list: Write a program to move all duplicate
Enter n: 9 [25, 35, 45, 55, 65] values in a list to the end of the list.
9 term of Fibonacci series = Question 12 Solution
34 Write a program to read two lists num l = eval(input("Enter the
and denum which contain the list: "))
============================ numerators and denominators of same dedup = []
========= fractions at the respective indexes. dup = []
Then display the smallest fraction for i in l:
Enter n: 25 along with its index. if i in dedup:
n should be less than or Solution dup.append(i)
equal to 20 num = eval(input("Enter else:
Question 11a numerators list: ")) dedup.append(i)
Write programs as per following denum = eval(input("Enter
specifications: denominators list: ")) l = dedup + dup
'''Print the length of the longest
string in the list of strings str_list. small = 0.0 print("Modified List:")
Precondition : the list will contain smallIdx = 0 print(l)
at least one element.''' Output
Solution for i in range(len(num)): Enter the list: [20, 15, 18,
l = eval(input("Enter list t = num[i] / denum[i] 15, 7, 18, 12, 13, 7]
of strings: ")) if t < small: Modified List:
largeIdx = 0 small = t [20, 15, 18, 7, 12, 13, 15,
largeLen = 0 smallIdx = i 18, 7]
Question 15
for i in range(len(l)): print("Smallest Fraction =", Write a program to compare two equal
length = len(l[i]) num[smallIdx], "/", sized lists and print the first index
if length > largeLen: denum[smallIdx]) where they differ.
largeLen = length print("Index of Smallest Solution
largeIdx = i Fraction =", smallIdx) print("Enter two equal sized
Output lists")
print("Longest String:", Enter numerators list: [1, l1 = eval(input("Enter first
l[largeIdx]) 3, 1, 7, 3] list: "))
Output Enter denominators list: [2, l2 = eval(input("Enter
Enter list of strings: 4, 4, 13, 8] second list: "))
["apple", "orange", "pear", Smallest Fraction = 1 / 2
"strawberry", "kiwi"] Index of Smallest Fraction = for i in range(len(l1)):
Longest String: strawberry 0 if l1[i] != l2[i]:
Question 11b Question 13 print("Lists differ
Write programs as per following Write a program to display the at index", i)
specifications: maximum and minimum values from break;
'''L is a list of numbers. Print a new list the specified range of indexes of list. else:
where each element is the Solution print("Lists are equal")
corresponding element of list L l = eval(input("Enter the Output
summed with number num.''' list: ")) Enter two equal sized lists
Enter first list: [80, 60,
50, 40, 30]
Enter second list: [80, 60,
55, 42, 30]
Lists differ at index 2
============================
=========
Prev
String Manipulation
Next
Tuples
CONTENTS
• Multiple Choice Questions
• Fill in the Blanks