# ISY104 Lists And Tuples: Activity Sheet Week 6 Assessment Answer

Pages: 4 Words: 890

## Question :

ISY104 – Foundations of Programming Week 6 – Lists and Tuples

Objective:

This week we will work on lists

Your tutor will discuss the answers to question 1 on pages 1 and 2 in the first 30 minutes. Then you will work with your tutor to solve the questions in the latter part of page 4.

1. State the reason for the output generated by the following program segments. It is important to remember the terminology used in the reasoning. The terms are shown in bold text in the “reasoning” column in the table below.

 Code Segments Answer Reasoning 1 list1 = ['physics', 'chemistry', 1997, 2000] list1[0] ‘physics’ The first element of the list is at index 0 2 list1 = ['physics','chemistry', 1997, 2000] list1[3] 2000 The last element is at index 3 3 list1 = ['physics','chemistry', 1997, 2000] list1[4] error list1 does not have index 4 4 list1 = [] len(list1) 0 There are no elements in the list, so the len function returns 0 5 list1 = [“aih”]len(list1) 1 There is one element in the list 6 list1 = list() len(list1) 0 list1 has no elements 7 list1 = list() list1.append(2)len(list1) 1 The append method of the list object adds an element to the back of the lst 8 list1 = [] list1[0] = 2 error List is empty. Cannot add elements using the assignment operator 9 list1 = [2]list1[0] = 4 list1[0] 4 The previous value of 2 gets updated by 4 10 m = [2]y = [4]x = m + y x [2, 4] A new list is created by concatenating two lists 11 m = [ 2, 4, 5]5 in m True Element 5 exists in m 12 m = [ 2, 4, 5]6 in m False Element 6 does not exists in m 13 m = [ 2, 4, 5]

 m[0] 2 Index 0 element 14 m = [ 2, 4, 5]m[-1] 5 Last index element 15 m = [ 2, 4, 5] m[2] 5 Last index element 16 m = [ 2, 4, 5]m[1:] [4,5] Sub-list from index 1 to end, using slice operator 17 m = [ 2, 4, 5, 8, 6] m[1:3] [4, 5] Sub-list from index 1 to 3-1, using slice 18 m = [1, 3, 7, 2, 8, 10] m[1:-2] [3, 7, 2] Sub-list from index 1 to -2-1. That is from index 1 to index len(m)-2-1 19 m = [1, 3, 7, 2, 8, 10]m.append(3) m [1, 3, 7, 2, 8,10, 3] Appends element 3 to the end of the list 20 m = [1, 3, 7, 2, 8, 10, 3]m.index(3) 1 The first occurrence of element 3 is at index 1. 21 m = [1, 3, 7, 2, 8, 10, 3]m.index(13) Value error Element 13 is not in the list 22 m = [1, 3, 7, 2, 8, 10, 3]7 in m True Element 7 is in list m 23 m = [1, 3, 7, 2, 8, 10, 3]13 in m False Element 13 is not in list m 24 m = [1, 3, 7, 2, 8, 10, 3]13 not in m True Element 13 is not in list m 25 m = [1, 3, 7, 2, 8, 10, 3]m.sort() m [1, 2, 3, 3, 7, 8, 10] Elements in m are sorted in ascending order 26 m = [1, 3, 7, 2, 8, 10, 3]m.sort(reverse=True) m [10, 8, 7, 3, 3, 2, 1] Elements in m are sorted in descending order 27 m = [1, 3, 7, 2, 8, 10]m.reverse() m [10, 8, 2, 7, 3, 1] Elements in m are reversed 28 m= [10, 8, 2, 7, 3, 1, 2]m.insert(2, 15)m [10, 8, 15, 2, 7, 3, 1, 2] Inserts element 15 at index 2 29 m =[10, 8, 15, 2, 7, 3, 1, 2]del m[2] [10, 8, 2, 7, 3, 1, 2] Deletes element at index 2. That is deletes 15 from m 30 m = [10, 8, 2, 7, 3, 1, 2]m.remove(7)m [10, 8, 2, 3, 1, 2] Deletes first occurrence of 7 in m 31 m = [10, 8, 2, 3, 1, 2]max(m) 10 The maximum value of elements in m 32 m = [10, 8, 2, 3, 1, 2]min(m) 1 The min value of elements in m 33 m = [10, 8, 2, 3, 1, 2] n=[]n = n + m n [10, 8, 2, 3, 1, 2] Creates a new list with the copy of m 34 m = [10, 8, 2, 3, 1, 2] n=[]

 n = n + m n is m False n references a different memory location from m. That is n is copy 35 m = [10, 8, 2, 3, 1, 2]n = m n is m True n references the same memory location of m. That is n is not a copy of m. 36 students = [["sam", 85.5, "hd"],["ann", 67, "cr"]]Students[0] ["sam", 85.5, "hd"] The first element in students is a list with values ["sam", 85.5, "hd"] 37 students = [["sam", 85.5, "hd"],["ann", 67, "cr"]]Students[0][0] “sam” The first element in ["sam",85.5, "hd"] is “sam” 38 students = [["sam", 85.5, "hd"],["ann", 67, "cr"]]for aStudent in students: print(aStudent) ['sam', 85.5, 'hd']['ann', 67, 'cr'] First time aStudent will be['sam', 85.5, 'hd']Second time aStudent will be['ann', 67, 'cr'] 39 students = [["sam", 85.5, "hd"],["ann", 67, "cr"]]for aStudent in students:avg = avg + aStudent[1] avg = avg/2print(avg) 76.25 The for loop finds the sum of all marks. Dividing by 2 finds the averageSo (85.5+67)/2 is 76.25 40 daysInAWeek = ("Sunday", “Sunday” The first element in the tuple "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") daysInAWeek[0] 41 daysInAWeek = ("Sunday", Type error Cannot change the elements in a tuple. Tuples are immutable. Tuples are read only. "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") daysInAWeek[0]= “sunday” 42

You will find solutions to the following with the help of your tutor.

Activity – 1:

Write a python program that has a list of even numbers up to 20. Display the first 3 elements from the list one by one, then display all numbers in the list, using range function.

Extend your program to display an average of the given even numbers in the list.

Activity – 2:

Write a python program that has a list of 5 numbers [1,2,3,4,5]. Print the first 3 elements from the list using slice expression.

Extend this program in a manner that the elements in the list are changed to [2,4,6,8,10] that means each element is times 2 of the previous value.

Extend your program to display the min and max value in the list.

Activity – 3:

Write a python program that has four lists:

summer = ['Dec', 'Jan', 'Feb']

autumn = ['Mar', 'Apr', 'May']

winter = ['Jun', 'Jul', 'Jul', 'Aug']

spring = ['Oct', 'Nov']

First, remove the excess month ‘Jul’ from the list winter and add month ‘Sep’ at the beginning of the spring list.

Then, make two new lists: MonthsISleep and MonthsIParty, copying the appropriate parts from the existing lists so that the list MonthsISleep contains all the months from ‘Feb’ to ‘Nov’ and the second list MonthIParty contains the remaining 2 months.

• Display elements form both lists: MonthsISleep and MonthsIParty.
• Display the elements of list summer in reverse order.