Chapter 4
WORKING WITH LISTS AND DICTIONARIES
Exercise
1. What will be the output of the following statements?
a) list1 = [12,32,65,26,80,10] list1.sort() print(list1)
b) list1 = [12,32,65,26,80,10] sorted(list1) print(list1)
c) list1 = [1,2,3,4,5,6,7,8,9,10] list1[::-2] list1[:3] + list1[3:]
d) list1 = [1,2,3,4,5] list1[len(list1)-1]
Answer:
2. Consider the following list myList. What will be the elements of myList after each of the following operations? myList = [10,20,30,40]
a) myList.append([50,60])
b) myList.extend([80,90])
Answer:
3. What will be the output of the following code segment? myList = [1,2,3,4,5,6,7,8,9,10] for i in range(0,len(myList)): if i%2 == 0: print(myList[i])
Answer:
4. What will be the output of the following code segment?
a) myList = [1,2,3,4,5,6,7,8,9,10] del myList[3:] print(myList)
b) myList = [1,2,3,4,5,6,7,8,9,10] del myList[:5] print(myList)
c) myList = [1,2,3,4,5,6,7,8,9,10] del myList[::2] print(myList)
Answer:
5. Differentiate between append() and extend() methods of list.
Answer:
6. Consider a list: list1 = [6,7,8,9] What is the difference between the following operations on list1:
a) lis t1 * 2
b) lis t1 *= 2
c) lis t1 = lis t1 * 2
Answer:
7. The record of a student (Name, Roll No, Marks in five subjects and percentage of marks) is stored in the following list: stRecord = [‘Raman’,’A-36′,[56,98,99,72,69], 78.
Answer:
8] Write Python statements to retrieve the following information from the list stRecord.
a) Percentage of the student
b) Marks in the fifth subject
c) Maximum marks of the student
d) Roll No. of the student
e) Change the name of the student from ‘Raman’ to ‘Raghav’
Answer:
8. Consider the following dictionary stateCapital: stateCapital = {“Assam”:”Guwahati”, “Bihar”:”Patna”,”Maharashtra”:”Mumbai”, “Rajasthan”:”Jaipur”} Find the output of the following statements:
a) print(stateCapital.get(“Bihar”))
b) print(stateCapital.keys())
c) print(stateCapital.values())
d) print(stateCapital.items())
e) print(len(stateCapital))
f) print(“Maharashtra” in stateCapital)
g) print(stateCapital.get(“Assam”))
h) del stateCapital[“Assam”] print(stateCapital)
Answer: