11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Slides:



Advertisements
Similar presentations
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Advertisements

Introduction to Programming
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Programming for Linguists An Introduction to Python 24/11/2011.
25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Pointers OVERVIEW.
29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
3 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
List Interface and Linked List Mrs. Furman March 25, 2010.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Introduction to Computer Systems
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Introduction to Programming
Introduction to Programming
Sections 10.1 – 10.4 Introduction to Arrays
Introduction to Computer Systems
Introduction to Programming
Introduction to Programming
4. Java language basics: Function
Introduction to Programming
Introduction to Programming
Containers and Lists CIS 40 – Introduction to Programming in Python
Introduction to Computer Systems
Introduction to Programming
Introduction to Programming
Introduction to Programming
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Programming
Chapter 8 Arrays Objectives
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Linked List and Selection Sort
Introduction to Programming
Introduction to Programming
Introduction to Programming
Chapter 8 Arrays Objectives
Data Structures & Algorithms
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Arrays and ArrayLists.
Presentation transcript:

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