11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2016 Week 10: Lists
Mock In Lab Test: exactDivision Define the function exactDivision(m, n) exactDivision returns True if the integer m is divisible by the integer n with remainder 0, otherwise it returns False. Solution: remember the properties of % def exactDivision(m, n) : return m%n == 0 11 March 2016Birkbeck College2
Mock In Lab Test: prime Define the function prime (m) prime returns True if the only integers which divide the integer m with a remainder of 0 are 1 and m, otherwise prime returns False. Solution: check every integer n between 2 and m-1 to see if n divides m with remainder 0 11 March 2016Birkbeck College3
Mock In Lab Test: code for prime def prime(m) : for i in range(2, m) : if exactDivision(m, i) : return False return True Note the use of the function exactDivision, which is already defined. How can the code be improved? 11 March 2016Birkbeck College4
Mock In Lab Test: counting primes Count the number of times prime returns True for integers in the range 2 to 100. n = 0 for i in range(2, 101) : if prime(i) : n = n+1 print("The number of primes in [2,100] is", n) 11 March 2016Birkbeck College5
Lists A mechanism for collecting together multiple values. A way of allocating names to multiple variables. 11 March 2016PFE Section 6.16
Creation of a List values = [32, 54, 67, 5] 11 March 2016PFE Section 6.17 name of list variable initial data Names of variables: values[0], values[1], values[2], values[3] The numbers 0, 1, 2, 3 are indices print(values[2]) # prints 67 print(values) # prints entire list
Indices and Length values = [32, 54, 67, 5] values[2] = 10 print(values[2]) # prints 10 print(len(values)) # prints length 4 of values print(values[-1]) # prints 5 Allowed negative indices are -1 to –len(values), i.e. -1, -2, -3, March 2016PFE Section 6.28
Lists and for Loops Both these loops have the same effect for i in range(len(values)) : print(values[i]) for element in values : print(element) 11 March 2016PFE Section
Bounds Error values = [32, 54, 67, 5] values[len(values)] = 3 # bounds error # A bounds error causes a run time exception. # The error is not detected at compile time. 11 March 2016PFE Section
List References scores = [10, 9, 7, 4, 5] values = scores scores[3] = 10 print(values[3]) # prints 10! # A list variable such as values is a pointer to the place # in memory where the list is stored. Values and scores # both reference the same list of numbers in memory 11 March 2016PFE Section
The List Variable as a Pointer 11 March 2016PFE Section list stored in memoryscores values The value of the variable score is a pointer to the list. The value of the variable values is a pointer to the same list
Example values = [1, 2, "text", range] 11 March 2016PFE Section # Correct but not recommended. Where possible, # list elements should have the same type.
Appending an Element friends = [] # empty list friends.append("Emily") friends.append("Bob") print(friends) # prints ["Emily", "Bob"] 11 March 2016PFE Section
Inserting an Element friends = ["Harry", "Bob"] friends.insert(1, "Cindy") print(friends) # prints ["Harry", "Cindy", "Bob"] friends.insert(i, "Emily") # i = 0, 1, 2: insert "Emily" before the element # with index i # i = 3: insert "Emily" after "Bob" (same as append) 11 March 2016PFE Section
Finding an Element if "Cindy" in friends : print("She's a friend") friends = ["Harry", "Emily", "Emily"] n = friends.index("Emily") # index of first occurrence n = friends.index("Tom") # error, run time exception 11 March 2016PFE Section
Removing an Element friends = ["Harry", "Cindy", "Emily", "Bob"] name = friends.pop(1) print(name) # prints "Cindy" print(friends) # prints ["Harry", "Emily", "Bob"] friends.pop() # remove the last element "Bob" print(friends) # prints ["Harry", "Emily"] 11 March 2016PFE Section 6.217
Removing Matches Remove all strings of length < 4 from the list words i = 0 while i < len(words) : word = words[i] if len(word) < 4 : words.pop(i) else : i = i+1 11 March 2016PFE
Removing Matches 2 Remove all strings of length < 4 from the list words for i in range(len(words)) # This code fails but why? word = words[i] if len(word) < 4 : words.pop(i) 11 March 2016PFE
Reading Input values = [] print("Please enter values, Q to quit: ") userInput = input("") while userInput != "Q" : values.append(float(userInput)) userInput = input("") # The Shell looks like this Please enter values, Q to quit: Q 11 March 2016PFE
Quiz Score A final quiz score is computed by adding all the scores except for the lowest two. For example, if the scores are 8, 4, 7, 8.5, 9.5, 7, 5, 10 then the final score is 50. Write a program to compute the final score in this way. 11 March 2016PFE How To 6.121
Insert Suppose that values is a sorted list of integers. Write a function to insert a new value into its proper position. 11 March 2016PFE R