Download presentation
Presentation is loading. Please wait.
Published byWillis Neal Modified over 8 years ago
1
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2016 Week 10: Lists
2
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
3
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
4
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
5
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
6
Lists A mechanism for collecting together multiple values. A way of allocating names to multiple variables. 11 March 2016PFE Section 6.16
7
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
8
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, -4 11 March 2016PFE Section 6.28
9
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 6.1.39
10
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 6.1.210
11
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 6.1.311
12
The List Variable as a Pointer 11 March 2016PFE Section 6.1.312 list stored in memoryscores values 109745 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
13
Example values = [1, 2, "text", range] 11 March 2016PFE Section 6.113 # Correct but not recommended. Where possible, # list elements should have the same type.
14
Appending an Element friends = [] # empty list friends.append("Emily") friends.append("Bob") print(friends) # prints ["Emily", "Bob"] 11 March 2016PFE Section 6.2.114
15
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 6.2.215
16
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 6.2.316
17
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
18
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 6.3.718
19
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 6.3.719
20
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: 32 29 67.5 Q 11 March 2016PFE 6.3.920
21
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
22
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 6.1922
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.