HW For Ch4 List
HW For Ch4 List
1. Start with the list [8, 9, 10]. Do the following using list functions:
(a) Set the second entry to 17. Ans- L[1]=17
(b) Add 4, 5 and 6 to the end of the list. Ans- L.extend([4,5,6])
(c) Remove the first entry from the list. Ans- L.pop(0)
(d) Sort the list Ans- L.sort()
(e) Double the list Ans- L=L*2
(f) Insert 25 at index 3. Ans- L.insert(3,25)
2. What’s A[1:1] if a list of at least two elements? And what if the list is shorter?
3. If A is [1,2,3]
(a) What is the difference (if any) between A*3 and [A,A,A]?
(b) Is A*3 equivalent to A+A+A?
(c) What is the meaning of A[1:1]=9?
(d) What’s the difference between A[1:2]=4 and A[1:1]=4?
4. What’s the purpose of the pop method?
5. What are list slices?
6. Does the slice operator always produce a new list?
7. An index out of bounds given with a list name causes error, but not with list slices.
Why?
8. What is the difference between appending a list and extending a list?
9. Do functions max( ), min( ),sum( ) work with all types of lists?
10.What is the difference between sort( ) and sorted ( )?
11.Predict the output:
Odd=[1,3,5]
print( ( Odd+[2,4,6] )[4] )
print( ( Odd+[12,14,16] )[4] – ( Odd+[2,4,6] )[4] )
12.Find the errors:
L1=[1,11,21,31]
L2=L1+2
L3=L1*2
Idx=L1.index(45)
13.Find the errors:
(a) L1=[1,11,21,31]
An=L1.remove(41)
(b) L1=[1,11,21,31]
An=L1.remove(31)
print(An+2)
14.Write a program that inputs two lists and creates a third, that contain all elements of
the first followed by all elements of a second.
1
15.Ask the user to enter a list containing numbers between 1 and 12. Then replace all of
the entries in the list that are greater than 10 with 10.
16.Create the following lists using a for loop:
(a) A list consisting of the integers 0 through 49.
(b) A list containing the squares of the integers 1 through 50.
(c) The list [‘a’,’bb’,’ccc’,’dddd’,……] that ends with 26 copies of the letter z.
17.Write a program to display the maximum and minimum values from the specified
range of indecies of a list.
18.Write a program to move all duplicate values in a list to the end of the list.
19.What is the output of the following code?
Numbers=list(range(0,51,4))
Results=[ ]
for i in Numbers:
if not i%3:
Results.append(i)
print(Results)