TEXT EXERCISES-Lists and Dictionary
TEXT EXERCISES-Lists and Dictionary
list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)
Ans :
[10, 12, 26, 32, 65, 80]
list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)
Ans:
[12, 32, 65, 26, 80, 10]
list1 = [1,2,3,4,5,6,7,8,9,10]
print(list1[::-2])
print(list1[:3] + list1[3:])
Ans:
[10, 8, 6, 4, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1 = [1,2,3,4,5]
print(list1[len(list1)-1])
Ans:
5
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])
Ans:
[10, 20, 30, 40, [50, 60]]
b) myList.extend([80,90])
Ans:
[10, 20, 30, 40, 80, 90]
Ans:
Element 1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
extend( )- Appends each element of the list at the end of the given list.
List1=[10,20,30]
List2=[40,50]
List1.extend(List2)
print(List1)
OUTPUT:
[10,20,30,40,50]
6. Consider a list:
list1 = [6,7,8,9]
What is the difference between the following operations on list1:
a) list1 * 2
Ans:
[6, 7, 8, 9, 6, 7, 8, 9]
b) list1 *= 2
Ans:
[6, 7, 8, 9, 6, 7, 8, 9]
c) list1 = list1 * 2
Ans:
[6, 7, 8, 9, 6, 7, 8, 9]
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.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’
Ans:
List Name Name Roll No Marks in 5 subjects Percentage
stRecord Raman A-36 [56, 98, 99, 72, 69] 78.8
Index 0 1 2 3
Programming Problems
1. Write a program to find the number of times an element occurs in the list.
#defining a list
list1 = [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
#printing the list for the user
print("The list is:",list1)
OUTPUT:
The list is: [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
Which element occurrence would you like to count? 20
The count of element 20 in the list is: 2