Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.

Similar presentations


Presentation on theme: "Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07."— Presentation transcript:

1 Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07

2 Computing Science 1P Lecture 13 - Simon Gay2 Lists: main points Use a list for a collection of data, often of the same type. NEVER use a collection of separate variables e.g. x1, x2, x3,… for a collection of data (e.g. in the exercise on multiple bouncing balls) Iteration over lists: - using for, if we just need each item but not its position - using while, if we need the position as well Positional access and update: x[1] x[i] = x[i]+1 etc. Other operations described in Chapter 8.

3 2006/07Computing Science 1P Lecture 13 - Simon Gay3 Lists: more advanced use (8.14, 8.15) In the barchart exercise (Unit 9) we used two lists: labels is the list of labels for the chart data is the list of data values e.g. labels = [ “Jan”, “Feb”, “Mar”, … ] data = [ 12, 10, 5, … ] This is a little unsatisfactory: - labels and data together form a data structure - they should always have the same length Can we use a single variable, record say, to combine the labels and the data?

4 2006/07Computing Science 1P Lecture 13 - Simon Gay4 Lists: more advanced use record = [ [“Jan”,12], [“Feb”,10], [“Mar”,5], … ] This is a list of lists or nested lists. There are 12 elements and each one is a list of 2 elements. record[0] is [“Jan”,12] record[0][0] is “Jan” record[0][1] is 12

5 2006/07Computing Science 1P Lecture 13 - Simon Gay5 What is len( [ [] ] ) ? 0 1 Something else Don't know

6 2006/07Computing Science 1P Lecture 13 - Simon Gay6 Joining lists together To convert from two lists into a list of lists, we can define the function join, so that (for example): join([“Jan”, “Feb”, “Mar”, … ], [12, 10, 5, … ]) returns [ [“Jan”,12], [“Feb”,10], [“Mar”,5], … ] Two approaches, depending on how you like to think about lists.

7 2006/07Computing Science 1P Lecture 13 - Simon Gay7 Joining lists: 1 def join(labels,data): newdata = [] i = 0 while i < len(labels): newdata = newdata + [ [labels[i],data[i]] ] i = i + 1 return newdata Start with an empty list, and build it up by adding elements to the end.

8 2006/07Computing Science 1P Lecture 13 - Simon Gay8 Joining lists: 2 def join(labels,data): newdata = [ [“”,0] ] * len(labels) i = 0 while i < len(labels): newdata[i] = [ labels[i], data[i] ] i = i + 1 return newdata Start with a list of the right length, with dummy elements, then assign the desired value to each position.

9 2006/07Computing Science 1P Lecture 13 - Simon Gay9 Which approach do you prefer? 1 (build a list up from empty) 2 (initialise a list of the right length, then update it)

10 2006/07Computing Science 1P Lecture 13 - Simon Gay10 List of lists: searching record = [ [“Jan”,12], [“Feb”,10], [“Mar”,5], … ] We don’t have a direct way to find the data for a given month. But we can easily define a function to do it. def findData(month,record): for x in record: if x[0] == month: return x[1] return -1 -1 could be replaced by something suitable for a particular program. This point will come up again next week.

11 2006/07Computing Science 1P Lecture 13 - Simon Gay11 Tuples: a new data structure (9.1, 9.2, 9.3) Python provides a data structure called a tuple. The name is a generalisation of double, triple, quadruple, quintuple, … A tuple is very similar to a list. What are the differences? Syntax: a tuple is written with round brackets, which can even be left out. x = ( 1, 2, 3, 4 ) y = “a”, “b”, “c” z = “hello”,

12 2006/07Computing Science 1P Lecture 13 - Simon Gay12 Tuples: a new data structure Python provides a data structure called a tuple. The name is a generalisation of double, triple, quadruple, quintuple, … A tuple is very similar to a list. What are the differences? Operations: everything you can do with a list can also be done with a tuple, except modifying an element. x = ( 1, 2, 3, 4 ) x[1] = 5 ERROR The reason for this will become clear later…

13 2006/07Computing Science 1P Lecture 13 - Simon Gay13 Tuples: a useful feature It is possible to assign to a tuple of variables simultaneously: ( x, y ) = ( 10, 20 ) but with a tuple we can leave out the brackets: x, y = 10, 20 This is also possible with a list of variables: [ x, y ] = [ 10, 20 ] which gives very nice syntax for simultaneous assignment.

14 2006/07Computing Science 1P Lecture 13 - Simon Gay14 Tuples: a useful feature It is possible to return a tuple from a function: def f(x,y): return (x+1, y+2) and again the brackets can be left out: def f(x,y): return x+1, y+2 Combining this with the simultaneous assignment idea: a, b = 10, 20 c, d = f(a,b) we have a nice way of returning several results from a function.

15 2006/07Computing Science 1P Lecture 13 - Simon Gay15 Example: multiple results from a function We defined join to combine labels and data into a list of lists. Now we can define split to do the opposite. split([ [“Jan”,12], [“Feb”,10], [“Mar”,5], … ]) returns ( [ “Jan”, “Feb”, … ], [12, 10, … ] ) Example: record = [ [“Jan”,12], [“Feb”,10], … ] labels, data = split(record)

16 2006/07Computing Science 1P Lecture 13 - Simon Gay16 Example: multiple results from a function def split(record): labels = [] data = [] for x in record: labels = labels + [x[0]] data = data + [x[1]] return labels, data

17 2006/07Computing Science 1P Lecture 13 - Simon Gay17 Program Development and Testing Testing is very important, even for the relatively small and simple programs that we are working with in this course. It’s worth adopting a style of working that makes it as easy as possible to test your programs. The Unit 9 worksheet had some suggestions for testing, but I did not see many people following them. Let’s look at an example based on the maxList function.

18 2006/07Computing Science 1P Lecture 13 - Simon Gay18 Can testing prove that a program always works correctly? Yes No Yes, but only in simple cases

19 2006/07Computing Science 1P Lecture 13 - Simon Gay19 Edsger W Dijkstra (1930-2002) Testing can prove the presence of errors, but not their absence.


Download ppt "Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07."

Similar presentations


Ads by Google