Abc Assignment Help

ISY104 Lists and Tuples: Activity Sheet Week 6 Assessment Answer

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








m[0]2Index 0 element
14m = [ 2, 4, 5]
m[-1]

5

Last index element
15m = [ 2, 4, 5] m[2]

5

Last index element
16m = [ 2, 4, 5]
m[1:]

[4,5]

Sub-list from index 1 to end, using slice operator
17m = [ 2, 4, 5, 8, 6] m[1:3]

[4, 5]

Sub-list from index 1 to 3-1, using slice
18m = [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
19m = [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
20m = [1, 3, 7, 2, 8, 10, 3]
m.index(3)

1
The first occurrence of element 3 is at index 1.
21m = [1, 3, 7, 2, 8, 10, 3]
m.index(13)

Value error

Element 13 is not in the list
22m = [1, 3, 7, 2, 8, 10, 3]
in m

True
Element 7 is in list m
23m = [1, 3, 7, 2, 8, 10, 3]
13 in m

False

Element 13 is not in list m
24m = [1, 3, 7, 2, 8, 10, 3]
13 not in m

True

Element 13 is not in list m
25m = [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
26m = [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
27m = [1, 3, 7, 2, 8, 10]
m.reverse() m

[10, 8, 2, 7, 3, 1]

Elements in m are reversed
28m= [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
29m =[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
30m = [10, 8, 2, 7, 3, 1, 2]
m.remove(7)
m

[10, 8, 2, 3, 1, 2]

Deletes first occurrence of 7 in m
31m = [10, 8, 2, 3, 1, 2]
max(m)

10
The maximum value of elements in m
32m = [10, 8, 2, 3, 1, 2]
min(m)

1
The min value of elements in m
33m = [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
34m = [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
35m = [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.
36students = [
["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"]
37students = [
["sam", 85.5, "hd"],
["ann", 67, "cr"]
]
Students[0][0]



“sam”
The first element in ["sam",
85.5, "hd"] is “sam”
38students = [
["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']
39students = [
["sam", 85.5, "hd"],
["ann", 67, "cr"]
]
for aStudent in students:
avg = avg + aStudent[1] avg = avg/2
print(avg)






76.25
The for loop finds the sum of all marks. Dividing by 2 finds the average
So (85.5+67)/2 is 76.25
40daysInAWeek = ("Sunday",




“Sunday”
The first element in the tuple
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") daysInAWeek[0]
41daysInAWeek = ("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.

Answer

For solution, connect with our online professionals. 

Customer Testimonials