COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON LISTS Jehan-François Pâris

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Dictionaries: Keeping track of pairs
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists Introduction to Computing Science and Programming I.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON TUPLES, SETS AND DICTIONARIES Jehan-François Pâris
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
String and Lists Dr. José M. Reyes Álamo.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Module 5 Working with Data
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
CS 100: Roadmap to Computing
Ruth Anderson University of Washington CSE 160 Spring 2015
Basic operators - strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson University of Washington CSE 160 Spring 2018
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data types Numeric types Sequence types float int bool list str
Ruth Anderson University of Washington CSE 160 Winter 2017
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Lists Copying Lists Processing Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Python Review
CS 100: Roadmap to Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 231 Lab 6.
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Presentation transcript:

COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON LISTS Jehan-François Pâris

Introduction Lists are sequences of items –separated by commas –enclosed in square brackets ("[…]") Examples – [ 1, 2, 3,4,5] – ['Alice', 'Bob', 'Carol', Dean'] – ['Alice', 'freshman', [100, 90, 70]]

Usages Group together similar objects –Lists of people, courses, exams –Vectors and matrices of algebra and physics Store records –Contact list entry ['Bill', , xyx.com'] –Anything else

Operations Access the elements of a list –Individual elements –Slices Modify its elements Insert elements –Anywhere Remove elements

Lists >>> mylist = [11, 12, 13, 14, 'done'] >>> mylist [11, 12, 13, 14, 'done'] >>> print(mylist) [11, 12, 13, 14, 'done'] >>> mylist[0] 11 – We can access individual elements

List slices >>> mylist[0:1] [11] –Two observations A list slice is a list mylist[0:1] starts with list[0] but stops before mylist [1]

More list slices >>> mylist[0:2] [11, 12] –Includes mylist[0] and mylist[1] >>> mylist[0:] [11, 12, 13, 14, 'done'] – The whole list >>> mylist[1:] [12, 13, 14, 'done']

More list slices >>> mylist[-1:] ['done'] A list slice is a list >>> mylist[-1] 'done' Not the same thing!

An example a = [10, 20, 30, 40, 50] a designates the whole list a[4] a[1] a[2]a[0]a[3]

Lists in geometry (I) Consider a point A in the Cartesian plane –It has two coordinates x A and y A A xAxA yAyA

Lists in geometry (II) We will represent each point in the Cartesian plane by a list a with two elements such that – a[0] represents x A – a[1] represents y A Can now speak of the origin as point [0, 0]

Distance between two points The distance between two points A and B is the square root of –( A x – B x ) 2 + ( A y – B y ) 2 Pythagora's theorema

Why? xAxA yAyA A xBxB yByB B

Function computing this distance >>> def distance( a, b) : """ Cartesian distance between a and b ""“ import math return math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)

Function computing this distance >>> origin = [0, 0] >>> a = [1, 1] >>> distance(origin, a) >>>

Lists in physics (I) Speed, accelerations can be expressed by vectors In Cartesian plane force F has two components – F x and F y We will represent each force F by a list f with two elements such that – f[0] represents F x – f[1] represents F y

Lists in geometry (II) F FxFx FyFy θ

Absolute value of a force >>> def vecmodule(f) """ Module of a two-dimensional vector ""“ import math return math.sqrt(f[0]**2 + f[1]**2) >>> vecmodule([0,2]) 2.0 >>> vecmodule([3,4]) 5.0

Adding two vectors (I) [0, 2] + [0, 3] [0, 2, 0, 3] –Not what we want –+ operator concatenates lists

Adding two vectors (I) x y F G F+G GxGx FxFx GyGy FyFy

Adding two vectors (III) >>> def vecadd(f, g) : """ adds two two-dimensional vectors ""“ return [f[0] + g[0], f[1] + g[1]] >>> vecadd([0, 2], [0, 3]) [0, 5] – A function can return a list

Multiplying two vectors Which product? –Dot product returns a scalar –Cross product returns a vector –Applies to three-dimensional spaces

Dot product def dotproduct (f, g): “”” dot product of two two-dimensional vectors ""“ return f[0]*g[0] + f[1]*g[1] >>> dotproduct([1,0], [2, 2]) 2

List of lists (I) >>> a = [[1, 2], [3, 1]] >>> a [[1, 2], [3, 1]] >>> a[0] [1, 2] >>> a[1][1] 1

List of lists (II) Can be used to represent matrices () a 00 a 01 a 00 a01a01

Sorting lists >>> mylist = [11, 12, 13, 14, 'done'] >>> mylist.sort() Traceback (most recent call last): File " ", line 1, in mylist.sort() TypeError: unorderable types: str() < int() – Cannot compare apples and oranges

Sorting lists of strings >>> namelist = ['Alice', 'Carol', 'Bob'] >>> namelist.sort() >>> print(namelist) ['Alice', 'Bob', 'Carol'] namelist.sort() is a method applied to the list namelist – In-place sort

Sorting lists of numbers >>> newlist = [0, -1, +1, -2, +2] >>> newlist.sort() >>> print(newlist) [-2, -1, 0, 1, 2] In-place sort

Sorting into a new list >>> newlist = [0, -1, +1, -2, +2] >>> sortedlist= sorted(newlist) >>> print(newlist) [0, -1, 1, -2, 2] >>> print(sortedlist) [-2, -1, 0, 1, 2] –sorted(…) is a conventional Python function that returns a new list

Modifying the elements of a list >>> mylist = [11, 12, 13, 14, 'done'] >>> mylist[-1] = 'finished' >>> mylist [11, 12, 13, 14, 'finished'] >>> mylist[0:4] = ['XI', 'XII', 'XIII', 'XIV'] >>> mylist ['XI', 'XII', 'XIII', 'XIV', 'finished']

Lists of lists (I) >>> listoflists = [[17.5, "1306"], [13, "6360"]] >>> listoflists.sort() >>> print(listoflists) [[13, '6360'], [17.5, '1306']] >>> listoflists[0] [13, '6360'] >>> listoflists[1] [17.5, '1306']

Lists of lists (II) >>> listoflists[0][0] 13 >>> listoflists[1][1] '1306' >>> listoflists[0][1] ='6360 quiz' >>> listoflists [[13, '6360 quiz'], [17.5, '1306']]

Adding elements to a list >>> mylist = [11, 12, 13, 14, 'finished'] >>> mylist.append('Not yet!') >>> mylist [11, 12, 13, 14, 'finished', 'Not yet!']

Adding elements to a list (I) >>> mylist = [11, 12, 13, 14, 'finished'] >>> mylist.append('Not yet!') >>> mylist [11, 12, 13, 14, 'finished', 'Not yet!']

Adding elements to a list (II) >>> listoflists = [[13, '6360 quiz'], [17.5, '1306']] >>> listoflists.append([15.3, 'ABET']) >>> listoflists [[13, '6360 quiz'], [17.5, '1306'], [15.3, 'ABET']] Appending means adding at the end.

Adding elements inside a list (I) >>> mylist = [11, 12, 13, 14, 'finished'] >>> mylist.insert(0, 10) >>> mylist [10, 11, 12, 13, 14, 'finished'] >>> mylist.insert(5, 15) >>> mylist [10, 11, 12, 13, 14, 15, 'finished']

Adding elements inside a list (II) >>> mylist = [11, 12, 13, 14, 'finished'] >>> mylist.insert(0, 10) >>> mylist [10, 11, 12, 13, 14, 'finished'] >>> mylist.insert(5, 15) >>> mylist [10, 11, 12, 13, 14, 15, 'finished']

Adding elements inside a list (III) mylist.insert(index, item) – index specifies element before which the new item should be inserted mylist.insert(0, item) inserts the new item before the first element mylist.insert(1, item) inserts the new item before the second element and after the first one

Example (I) a = [10, 20, 30, 40, 50] a designates the whole list a[4]a[1]a[2] a[0] a[3]

Example (II) Where to insert 25 and keep the list sorted? a a[4]a[1]a[2] a[0] a[3]

Example (III) Where to insert 25 and keep the list sorted? a a[4]a[1]a[2] a[0] a[3] 25

Example (IV) We do – a.insert(2, 25) after a[1] and before a[2]

Let us check >>> a = [10, 20, 30, 40, 50] >>> a.insert(2,25) >>> a [10, 20, 25, 30, 40, 50]

Example (V) Where to insert 55 and keep the list sorted? a a[4]a[1]a[2] a[0] a[3]

Example (VI) Where to insert 55 and keep the list sorted? a a[4]a[1]a[2] a[0] a[3] 55

Example (VII) We must insert –After a[4] – Before no other element We act as if a[5] existed – a.insert(5, 55) It works! Same as a.append(55)

Let us check >>> a = [10, 20, 30, 40, 50] >>> a.insert(5, 55) >>> a [10, 20, 30, 40, 50, 55]

Let us make it a bit easier len(list) –returns the length of a list Same as number of its elements Equal to index of last element plus one We could have written – a.insert(len(a), 55) Same as a.append(55)

Removing items One by one thislist.pop(i) –removes thislist[i] from thislist –returns the removed element thislist.pop( ) –removes the last element from thislist –returns the removed element

Examples (I) >>> mylist = [11, 22, 33, 44, 55, 66] >>> mylist.pop(0) 11 >>> mylist [22, 33, 44, 55, 66]

Examples (II) >>> mylist.pop() 66 >>> mylist [22, 33, 44, 55] >>> mylist.pop(2) 44 >>> mylist [22, 33, 55]

Sum: one more useful function >>> list = [10, 20,20] >>> sum(list) 50 >>> list = ['Alice', ' and ', 'Bob'] >>> sum(list) –Does not work! Would get same results with tuples

Initializing lists Use list comprehensions >>> [ 0 for n in range(0, 9)] [0, 0, 0, 0, 0, 0, 0, 0, 0] >>> [n for n in range (1,11)] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> [2*n+1 for n in range(0, 6)] [1, 3, 5, 7, 9, 11]

Warning! The for n clause is essential [0 in range(0, 10)] [True] – Because 0 is in range(0, 10)

More comprehensions >>> [ c for c in 'COSC 1306'] ['C', 'O', 'S', 'C', ' ', ‘1', '3', '0', '6'] >>> names = ['Alice', 'bob', 'carol'] >>> cap_names =[n.capitalize() for n in names] >>> names ['Alice', 'Bob', 'Carol'] >>> cap_names ['Alice', 'bob', 'carol']

An equivalence [2*n+1 for n in range(0, 6)] [1, 3, 5, 7, 9, 11] is same as a = [ ] for n in range(0,6) : a.append(2*n + 1)

Filtered comprehensions >>> a = [11, 22, 33, 44, 55] >>> b = [ n for n in a if n%2 == 0] >>> b [22, 44] >>> c = [n//11 for n in a if n > 20] >>> c [2, 3, 4, 5] A very powerful tool !

Making things more complicated >>> from random import uniform >>> >>> def gilligan(k):... return 4 * sum([1 for c in [uniform(0,1)**2+uniform(0,1)**2 for _ in range(k)] if c < 1]) / float(k)... >>> gilligan(100000) I did not write this code float(..) required by 2.7.2

Understanding the function (I) My friend used two comprehensions [uniform(0,1)**2+uniform(0,1)**2 for _ in range(k)] –creates a list containing k instances of uniform(0,1)**2+uniform(0,1)**2 – the underscore ('_') denotes the last evaluated expression

Understanding the function (II) [1 for c in […] if c < 1] –creates a list containing a 1 for each value in c such that c < 1 –each entry of the new list represents one dart that felt into the circle

Understanding the function (III) The last step is summing the contents of the outer list sum([1 for c in […] if c < 1]) –returns total number of darts each entry of the new list represents one dart that felt into the circle

Programming styles My friend's programming style favors conciseness over clarity I prefer clarity –Code should be so dumb that anyone could understand it without any effort Our definitions of clarity shift over time –What appears obscure to us today will appear trivial once we are familiar with the language

The for statement (I) for i in range(low, high): –Repeats high – low times –For all integer values of i between low and hi – 1 – low  i < hi

Example >>> for i in range(1, 5) : print ('Iteration %s' %i) Iteration 1 Iteration 2 Iteration 3 Iteration 4 5 is NOT in range(1, 5)

Observation If you want to execute a loop n times, use – for i in range(0,5) – for i in range (1, 6) – for i in range (2, 7) – …

The for statement (II) for i in a : –a must be a list –Repeats len(a) times –For all elements of a – for names in [‘Alice’, ‘Bob’, ‘Carol’]

Example (I) >>> names = ['Alice', 'Bob', 'Carol'] >>> for name in names : print('Hi ' + name + '!') Hi Alice! Hi Bob! Hi Carol! >>>

Example (II): better dot product def dotproduct(a, b) : if len(a) != len(b) : print("We must raise an exception!") return "Error" product = 0 for i in range(0, len(a)) product += a[i]*b[i] return product

Membership Can test membership in a list – a in alist returns True is a is in alist and False otherwise Will use it in if statements – if a in alist :

Example (I) >>> def ismember (a, list) : if a in alist : return '%s in %s' %(str(a), str(list)) else : return '%s NOT in %s' %(str(a), str(list))

Example (II) >>> ismember('Bob', ['Alice', 'Carol']) "Bob NOT in ['Alice', 'Carol']" >>> ismember('Alice', ['Alice', 'Carol']) "Alice in ['Alice', 'Carol']"

Copying/Saving a list >>> a = ["Alice", "Bob", "Carol"] >>> saved = a >>> a.pop(0) 'Alice' >>> a ['Bob', 'Carol'] >>> saved ['Bob', 'Carol'] We did not save anything!

Mutable and immutable quantities By default, Python quantities are immutable – Each time you modify them, you create a new value Expensive solution that works as we expect it

Mutable and immutable quantities Python lists are mutable – They can be modified in place – a = [1, 5, 3] can be sorted without making a new copy of the list Much cheaper solution – Can cause surprises!

How Python implements variables A Python variable contains the address of its current value a = 5 a 5

How Python implements variables A Python variable contains the address of its current value a = 5 b = a a 5 b

How Python implements variables A Python variable contains the address of its current value a = 5 b = a a = a +1 a 5 b 6

How Python implements variables This work as we expected –We saved the old value of a into be before modifying it People write – a = 5 # initial value – b = a # save initial value – a = a +1 # increment

A big surprise >>> a = [11, 33, 22] >>> b = a >>> b [11, 33, 22] >>> a.sort() >>> a [11, 22, 33] >>> b [11, 22, 33] The old value of a was not saved!

What happened (I) >>> a = [11, 33, 22] >>> b = a >>> b [11, 33, 22] a [11, 33, 22] b

What happened (II) >>> a = [11, 33, 22] >>> b = a >>> b [11, 33, 22] >>> a.sort() >>> a [11, 22, 33] >>> b [11, 22, 33] a [11,22, 33] b

Why this mess? Making lists immutable would have made Python much slower Conflict between ease of use and efficiency –This time efficiency won! Because efficiency penalty would have been very big

How to copy a list Copy a slice containing the whole list >>> a = [11, 33, 22] >>> b = a[0:len(a)] >>> a.sort() >>> a [11, 22, 33] >>> b [11, 33, 22] Silly but it works!