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

Slides:



Advertisements
Similar presentations
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Roles of Variables with Examples in Scratch
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Introduction to Python
Data Structures More List Methods Our first encoding Matrix.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
CS1Q Computer Systems Lecture 11 Simon Gay. Lecture 11CS1Q Computer Systems - Simon Gay2 The D FlipFlop A 1-bit register is called a D flipflop. When.
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
1 Modeling interactions and behavior Lecturer Dr. Mai Fadel.
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Built-in Data Structures in Python An Introduction.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 19: Friday 9 th March Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Types and Programming Languages
Search Engines WS 2009 / 2010 Prof. Dr. Hannah Bast Chair of Algorithms and Data Structures Department of Computer Science University of Freiburg Lecture.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
BMTRY 789 Lecture 6: Proc Sort, Random Number Generators, and Do Loops Readings – Chapters 5 & 6 Lab Problem - Brain Teaser Homework Due – HW 2 Homework.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
JavaScript and Ajax (JavaScript Arrays) Week 5 Web site:
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Announcements No Labs / Recitation this week On Friday we will talk about Project 3 Release late afternoon / evening tomorrow Cryptography.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Containers and Lists CIS 40 – Introduction to Programming in Python
Department of Computer Science,
CS 115 Lecture 8 Structured Programming; for loops
CMSC201 Computer Science I for Majors Lecture 11 – Program Design
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Ruth Anderson UW CSE 160 Spring 2018
Bryan Burlingame 03 October 2018
CHAPTER THREE Sequences.
CS190/295 Programming in Python for Life Sciences: Lecture 6
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Structured Programming Taken from notes by Dr. Neil Moore
Introduction to Computer Science
CISC101 Reminders Assignment 3 due today.
CMSC201 Computer Science I for Majors Lecture 12 – Program Design
Control flow: Loops UW CSE 160.
Tuple.
Introduction to Computer Science
Presentation transcript:

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

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.

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?

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

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

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.

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.

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.

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)

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 could be replaced by something suitable for a particular program. This point will come up again next week.

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”,

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…

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.

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.

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)

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

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.

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

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