0% found this document useful (0 votes)
16 views5 pages

TEXT EXERCISES-Lists and Dictionary

Uploaded by

geekcraftai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

TEXT EXERCISES-Lists and Dictionary

Uploaded by

geekcraftai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TEXT EXERCISES- LISTS AND DICTIONARY

1. What will be the output of the following statements?

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]

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])

Ans:
Element 1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9

0%2=0, 1 will print


1%2 !=0, 2 will not print
2%2=0 ,3 will print
3%2 !=0, 4 will not print
4%2=0 , 5 will print
5%2 !=0, 6 will not print
6%2=0 , 7 will print
7%2 !=0, 8 will not print
8%2=0 , 9 will print
9%2 !=0, 10 will not print
OUTPUT:
1
3
5
7
9

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)
Ans:
[1, 2, 3]
b) myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)
Ans:
[6, 7, 8, 9, 10]
c) myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)
Ans:
[2, 4, 6, 8, 10]

5. Differentiate between append() and extend() methods of list.


Ans:
append( )- Appends a single element at the end of the list.
List1=[10,20,30,40]
List1.append(50)
print(List1)
OUTPUT:
[10,20,30,40,50]

A list can also be appended as an element to an existing list.


List1=[10,20,30,40]
List1.append([50,60])
print(List1)
OUTPUT:
[10,20,30,40,[50,60]]

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

a) Percentage of the student


print(stRecord[3])

b) Marks in the fifth subject


print(stRecord[2][4])

c) Maximum marks of the student


print(max(stRecord[2]))

d) Roll No. of the student


print(stRecord[1])

e) Change the name of the student from ‘Raman’ to ‘Raghav’


stRecord[0]="Raghav"

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)

#asking the element to count


inp = int(input("Which element occurrence would you like to count? "))

#using the count function


count = list1.count(inp)

#printing the output


print("The count of element",inp,"in the list is:",count)

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

2. Write a program to read a list of n integers (positive as well as negative). Create


two new lists, one having all positive numbers and the other having all negative
numbers from the given list. Print all three lists.

#Defining empty list


list1 = list()

#Getting the input of number of elements to be added in the list


inp = int(input("How many elements do you want to add in the
list? (Element can be both positive and negative) "))

#Taking the input of elements to be added


for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)

#Printing the list


print("The list with all the elements: ",list1)

#Defining list2 and list3 to store positive and negative elements


of the list
list2 = list()
list3 = list()

#Looping through list to segregate positive and negative numbers


for j in range(inp):
if list1[j] < 0:
#Appending negative elements to list3
list3.append(list1[j])
else:
#Appending positive elements to list2
list2.append(list1[j])
print("The list with positive elements: ",list2)
print("The list with negative elements: ",list3)

You might also like