COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

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.
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.
Lists Victor Norman CS104. Reading Quiz Q1: What is printed by the following statements? alist = [3, 67, “cat”, [56, 57, “dog”], [ ], 3.14, False] print(3.14.
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 © 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
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
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
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON LISTS Jehan-François Pâris
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
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.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
String and Lists Dr. José M. Reyes Álamo.
COMPSCI 107 Computer Science Fundamentals
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Generating Random Numbers
Containers and Lists CIS 40 – Introduction to Programming in Python
CS 100: Roadmap to Computing
CSc 120 Introduction to Computer Programing II
CSC 108H: Introduction to Computer Programming
JavaScript: Functions.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Lists Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bryan Burlingame 03 October 2018
Ruth Anderson University of Washington CSE 160 Spring 2018
CHAPTER THREE Sequences.
Chapter 10 Lists.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data types Numeric types Sequence types float int bool list str
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1111 Introduction to Programming Fall 2018
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
Lists Part 1 Taken from notes by Dr. Neil Moore
Python I Some material adapted from Upenn cmpe391 slides and other sources.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1111 Introduction to Programming Spring 2019
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
CISC101 Reminders Assignment 2 due today.
Topics Sequences Introduction to Lists List Slicing
Python Review
CS 100: Roadmap to Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 231 Lab 6.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Presentation transcript:

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh.edu Fall 2016 1 1

THE ONLINE BOOK CHAPTER X LISTS

Introduction Lists are ordered sequences of elements 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 related objects Lists of people, courses, exams Vectors and matrices of algebra and physics Store records Contact list entry ['Bill', '713-555-1234', 'bill.tran@ xyx.com'] Anything else

Lists and strings Lists Ordered sequences Similar primitives Elements can be any Python objects Including lists Mutable Strings Ordered sequences Similar primitives Can only contain characters Non mutable

Operations Finding the length of a list Access the elements of a list Deciding on list membership Concatenating lists Defining slices Modify individual elements Lists are mutable

Accessing Elements (I) >>> names = ['Ann', 'Bob', 'Carol', 'end'] >>> names ['Ann', 'Bob', 'Carol', 'end'] >>> print(names) >>> names[0] 'Ann' We can access individual elements

Accessing Elements (II) >>> names = ['Ann', 'Bob', 'Carol', 'end'] >>> names[1] 'Bob' >>> names[-1] 'end' List indexing uses the same conventions as string indexing

Finding the length of a list names = ['Ann', 'Bob', 'Carol', 'end'] >>> len(names) 4 >>>alist = ['Ann', 'Bob', 'Carol', [1,2,3]] >>> len(alist) 4 New list has still four elements, the last one is list [1, 2, 3]

List membership names = ['Ann', 'Bob', 'Carol', 'end'] >>> 'Ann' in names True >>> 'Lucy' in names False >>> 'Alice' not in names True Same operators as for strings

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

List concatenation (I) >>> names = ['Alice'] + ['Bob'] >>> names ['Alice', 'Bob'] >>> names =['Carol'] >>> names = names + 'Dean' … TypeError: can only concatenate list (not "str") to list

List concatenation (II) >>> mylist = ['Ann'] >>> mylist*3 ['Ann', 'Ann', 'Ann'] >>> newlist = [0]*8 >>> newlist [0, 0, 0, 0, 0, 0, 0, 0] >>> [[0]*2]*3 [[0, 0], [0, 0], [0, 0]]

List slices >>> names = ['Ann', 'Bob', 'Carol', 'end'] Two observations A list slice is always a list names[0:1] starts with names[0] but stops before names[1]

More list slices >>> names[0:2] ['Ann', 'Bob'] Includes names[0] and names[1] >>> names[0:] ['Ann', 'Bob', 'Carol', 'end'] The whole list >>> names[1:] ['Bob', 'Carol', 'end']

TRAP WARNING More list slices >>> names[-1:] ['end'] A list slice is a list >>> names[-1] 'end' Not the same thing! TRAP WARNING

Let us check names = ['Ann', 'Bob', 'Carol', 'end'] >>> names[-1] 'done' >>> names[-1:] ['done'] >>> names[-1] == names[-1:] False What's true for strings is not always true for lists

Mutable and immutable quantities Python lists are mutable They can be modified in place names = ['Ann', 'Bob', 'Carol', 'end'] >>> names[-1] = 'Astrid' >>> names ['Ann', 'Bob', 'Carol', 'Astrid'] Can cause surprises!

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

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 b 6 5

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 The old value of a was not saved! >>> a = ['Ann', 'Bob', 'Carol'] >>> b = a >>> b ['Ann', 'Bob', 'Carol'] >>> a[0] = 'Lucy' >>> a ['Lucy', 'Bob', 'Carol'] >>> b ['Lucy', 'Bob', 'Carol'] The old value of a was not saved!

What happened (I) a b ['Ann', 'Bob', 'Carol'] >>> a = ['Ann', 'Bob', 'Carol'] >>> b = a >>> b ['Ann', 'Bob', 'Carol'] a ['Ann', 'Bob', 'Carol'] b

What happened (II) a b ['Lucy', 'Bob', 'Carol'] >>> a[0] = 'Lucy' >>> a ['Lucy', 'Bob', 'Carol'] >>> b ['Lucy', 'Bob', 'Carol'] a b ['Lucy', 'Bob', 'Carol']

Why this mess? Making lists immutable would have made Python much slower Lists can be very large 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 = ['Ann', 'Bob', 'Carol'] >>> b = a[:] >>> b ['Ann', 'Bob', 'Carol'] >>> a[0] = 'Lucy' >>> a ['Lucy', 'Bob', 'Carol']

Deletions Can delete individual elements or entire slices by specifying their location names = ['Ann', 'Bob', 'Carol', 'David'] >>> del names[2] >>> names['Ann', 'Bob', 'David'] >>> del names[0:2] >>>names ['David']

Object and references (I) >>> a = ['Ann'] >>> b = ['Ann'] >>> a == b True >>> a is b False a and b point to different objects

No sharing a b ['Ann'] ['Ann']

Object and references (II) >>> a = ['Ann'] >>> b = a >>> a == b True >>> a is b True a and b now point to the same object

Same object is shared a b ['Ann']

Aliasing and cloning (I) When we do >>> a = ['Ann'] >>> b = a we do not make a copy of a, we just assign an additional variable name to the object pointed by a b is just an alias for a

Aliasing and cloning (II) To make a true copy of a, we must do >>> a = ['Ann'] >>> b = a[:] we make a true copy of a b is a clone of a

A weird behavior >>> pets = ['cats', 'dogs'] >>> oddlist =[pets]*2 >>> oddlist [['cats', 'dogs'], ['cats', 'dogs']] >>> pets[0] = 'snake' >>> pets ['snake', 'dogs'] >>> oddlist [['snake', 'dogs'], ['snake', 'dogs']]

What did happen? (I) pets 'cats' 'dogs'

What did happen? (II) Nothing is copied! oddlist pets 'cats' 'dogs'

What did happen? (III) oddlist pets 'snakes' 'dogs'

My take Example of an odd behavior Must be aware of it Might encounter it In a complex not too well written program In an exam question (Not in COSC 1306) Should stay away for it If possible

Editing list >>> mylist = [11, 12, 13, 'done'] >>> mylist[-1] = 'finished' >>> mylist [11, 12, 13, 'finished'] >>> mylist[0:4] = ['XI', 'XII', 'XIII'] >>> mylist ['XI', 'XII', 'XIII', 'finished'] We can change the values of the existing list entries but cannot add or delete any of them

List methods Four groups of methods Adding an item to a list and telling where to put it Extracting an item from a list Modifying the order of a list Locating and removing individual items

Adding an element to a list Two methods append() insert() Both methods return no value

Append(I) >>> mylist = [11, 12, 13, 'finished'] >>> mylist.append('Not yet!') >>> mylist [11, 12, 13, 'finished', 'Not yet!']

Appending means adding at the end. Append (II) >>> listoflists = [[14.5,'1306'], [17.5,'6360']] >>> listoflists.append([16.5, 'TAs']) >>> listoflists [[14.5,'1306'],[17.5,'6360'],[16.5, 'TAs']] Appending means adding at the end.

Insert (I) >>> mylist = [11, 12, 13,'finished'] >>> mylist.insert(0, 10) #BEFORE mylist[0] >>> mylist [10, 11, 12, 13, 'finished'] >>> mylist.insert(4, 14) #BEFORE mylist[4] [10, 11, 12, 13, 14,'finished']

Insert(II) mylist.insert(index, item) index specifies entry before which the new item should be inserted mylist.insert(0, item) inserts the new item before the first list entry mylist.insert(1, item) inserts the new item before the second element and after the first one

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

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

Example (III) a Where to insert 25 and keep the list sorted? a[4] a[1] 10 20 50 30 40 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] 10 20 50 30 40 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] 10 20 50 30 40 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)

Extracting items One by one thislist.pop(i) removes thislist[i] from thislist returns the removed entry thislist.pop( ) removes the last entry from thislist

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 [22, 33, 55]

Examples (III) >>> waitlist = ['Ann', 'Cristi', 'Dean'] >>> waitlist.pop(0) 'Ann' >>> waitlist ['Cristi', 'Dean'] >>> waitlist.append('David') ['Cristi', 'Dean', 'David']

Reordering a list Two methods mylist.sort() sorts the list returns no value mylist.reverse() reverse the order of the list entries

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

Sorting lists of strings >>> names = ['Alice', 'Carol', 'Bob'] >>> names.sort() >>> print(names) ['Alice', 'Bob', 'Carol'] names.sort() is a method applied to the list names 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 lists with sublists >>> schedule = [[14.5, '1306'], [17.5, '6360'], [16.5, 'TA']] >>> schedule.sort() >>> schedule [[14.5, '1306'], [16.5, 'TA'], [17.5, '6360']] >>>

Reversing the order of a list >>> names = ['Alice', 'Bob', 'Carol'] >>> names.reverse() >>> names ['Carol', 'Bob', 'Alice'] namelist.reverse() is a method applied to the list namelist

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

Searching and retrieving mylist.index(item) Returns the position of first occurrence of item in the list mylist.count(item) Returns the number of occurrences of item in the list mylist.remove(item) Removes the first occurrence of item from the list

Examples >>> names ['Ann', 'Carol', 'Bob', 'Alice', 'Ann'] >>> names.index('Carol') 1 >>> names.count('Ann') 2 >>> names.remove('Ann') ['Carol', 'Bob', 'Alice', 'Ann']

Sum: a very useful function >>> list = [10, 20 ,20] >>> sum(list) 50 >>> list = ['Alice', ' and ', 'Bob'] >>> sum(list) Does not work!

Easy averages >>> prices = [1.899, 1.959, 2.029, 2.079] >>> sum(prices) 7.966000000000001 >>> len(prices) 4 >>> sum(prices)/len(prices) # GETS AVERAGE 1.9915000000000003

Appending vs. Concatenating We append a new entry to a list >>> names = ['Ann', 'Bob', 'Alice'] >>> names.append('Carol') >>> names ['Ann', 'Bob', 'Alice', 'Carol'] We have updated the list

Appending vs. Concatenating We concatenate two lists >>> names = ['Ann', 'Bob', 'Alice'] >>> names + ['Carol'] ['Ann', 'Bob', 'Alice', 'Carol'] >>> names ['Ann', 'Bob', 'Alice'] # UNCHANGED >>> names = names +['Carol'] ['Ann', 'Bob', 'Alice', 'Carol'] # OK

Lists and for loops By item for item in alist : Processes successively each item in the list Most natural

Lists and for loops By position for index in range(len(alist)) : Goes through the indices of all the list entries Gives us access to their positions

Examples >>> names ['Ann', 'Bob', 'Alice', 'Carol'] >>> for item in names : print(item) Ann Bob Alice Carol

Usage >>> scores = [50, 80, 0, 90] >>> def countzeroes(alist) : count = 0 for item in alist : if item == 0 : count += 1 return count >>> countzeroes(scores) 1

Examples >>> names ['Ann', 'Bob', 'Alice', 'Carol'] >>> for i in range(len(names)): print(names[i]) Ann Bob Alice Carol

Another countzeroes() function >>> scores = [50, 80, 0, 90] >>> def countzeroes(alist) : count = 0 for index in range(len(scores)) : if scores[index] == 0 : count += 1 return count >>> countzeroes(scores) 1

Using lists as arguments Can modify a list from inside a function def capitalizeList(l) : for i in range(len(l)) : l[i] = l[i].capitalize() names = ['Ann', 'bob', 'lIZ'] capitalizeList(names) print(names) ['Ann', Bob', 'Liz']

Notes Only works for lists that were passed to the function as an argument Must traverse the list by position for entry in l: … does not work Function returns no value Operates through a side effect

Functions returning a list def capitalizeList(l) : newlist = [] for item in l : newlist.append(item.capitalize()) return newlist names= ['ann', 'Bob', 'lIZ'] print(capitalizeList(names)) ['Ann', Bob', 'Liz']

Pure functions Return a value Have no side effects Like mathematical functions Implement a black box Black box Inputs Output

Main advantage of pure functions Easier to understand Preferred whenever possible

Another function returning a list def minSec(miltime) : """ Converts hhmm into hors and minutes""" hours = miltime//100 minutes = miltime % 100 return([hours, minutes]) [hh, ss] = minSec(1538) print(hh) print(ss)

One more def primes_upto(n) : """ Return a list of prime numbers < n.""" result = [] # START WITH EMPTY LIST for i in (2, n) : # SKIP ONE if is_prime(i) : result.append(i) # ADD TO LIST return result

Guess the output myname = "Edgar Allan Poe" namelist = myname.split() init = "" for aname in namelist: init = init + aname[0] print(init)

The answer myname = "Edgar Allan Poe" namelist = myname.split() init = "" for aname in namelist: init = init + aname[0] print(init) "EAP"

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 'Cougars'] [ 'C', 'o', 'u', 'g', 'a', 'r', 's'] >>> names = ['Ann', 'bob', 'liz'] >>> new = [n.capitalize() for n in names] >>> new ['Alice', 'Bob', 'Liz'] >>> names ['Alice', 'bob', 'liz']

More comprehensions >>> first5 = [n for n in range(1,6)] >>> first5 [1, 2, 3, 4, 5] >>> first5squares = [n*n for n in first5] [1, 4, 9, 16, 25]

An equivalence [n*n for n in range(1, 6)] [1, 4, 9, 16, 25] is same as a = [ ] for n in range(1,6) : a.append(n*n)

Filtered comprehensions Filtered through a condition def primes_upto(n) : """ Return a list of prime numbers < n.""" result = [x for x in range(2,n) if isprime(x)] return result

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 !

More filtered comprehensions >>> s = [['Ann', 'CS'],['Bob', 'CE'],['Liz', 'CS']] >>> [x[0] for x in s if x[1] =='CS'] ['Ann', 'Liz'] >>> sum([1 for _ in s if _[1] =='CS']) 2

Computingπ I have sometimes asked students to compute π by throwing simulated random darts at a target Imagine a target consisting of a circle inscribed in a square

Computing π If r denotes the radius of the circle The area of the circle is πr2 The area of the square is 4 r2 The circle occupies π/4 of the area of the circle If we throw n random darts at the target An average of nπ/4 will and inside the circle

My solution import random def computePi(niterations) : incount = 0 for i in range(niterations) : x = random.uniform(-1, 1) y = random.uniform(-1, 1) if x*x + y*y <= 1 : incount += 1 return 4*incount/niterations

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) 3.13396 I did not write this code float(..) required by Python 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

Nested lists or lists of lists >>> nested = [['Ann', 90], ['Bob', 84]] >>> innerlist = nested[1] >>> innerlist ['Bob', 84] >>> innerlist[0] 'Bob' >>> nested[1][0]

Strings and lists (I) >>> s = 'No pain, no gain' >>> lst1 = s.split() >>> lst1 ['No', 'pain,', 'no', 'gain'] >>> lst2 = s.split(',') >>> lst3 = s.split('ai') >>> lst3 ['No p', 'n no g', 'n']

Strings and lists (II) list function applies to strings >>> list('Hello') ['H', 'e', 'l', 'l', 'o']

TUPLES LESS IMPORTANT

Tuples Same as lists but Immutable Enclosed in parentheses A tuple with a single element must have a comma inside the parentheses: a = ('Liz',)

Examples >>> mytuple = ('Ann', 'Bob', 33) The comma is required!

Why? No confusion possible between ['Ann'] and 'Ann' ('Ann') is a perfectly acceptable expression ('Ann') without the comma is the string 'Ann' ('Ann', ) with the comma is a tuple containing the string 'Ann' Sole dirty trick played on us by tuples!

Tuples are immutable >>> mytuple = ('Ann', 'Bob', 'Liz') >>> saved = mytuple >>> mytuple += ('Ed',) >>> mytuple ('Ann', 'Bob', 'Liz','Ed') >>> saved ('Ann', 'Bob', 'Liz')

Things that do not work mytuple += 'Phil' Traceback (most recent call last):Z … TypeError: can only concatenate tuple (not "string") to tuple Can understand that!

sorted( ) returns a list! Sorting tuples >>> atuple = ('Liz', 'Bob', 'Ann') >>> atuple.sort() Traceback (most recent call last): … AttributeError: 'tuple' object has no attribute 'sort' >>> atuple = sorted(atuple) >>> atuple ['Ann', 'Bob', 'Liz'] Tuples are immutable! sorted( ) returns a list!

Most other things work! >>> atuple = ('Ann', 'Bob', 'Liz') >>> len(atuple) 3 >>> 44 in atuple False >>> [ i for [i for i in atuple] ['Ann', 'Bob', 'Liz']

The reverse does not work >>> alist = ['Ann', 'Bob', 'Liz'] >>> (i for i in alist) <generator object <genexpr> at 0x02855DA0> Does not work!

Converting sequences into tuples >>> alist = ['Ann', 'Bob', 'Liz'] >>> atuple = tuple(alist) >>> atuple ('Ann', 'Bob', 'Liz') >>> newtuple = tuple('Hello!') >>> newtuple ('H', 'e', 'l', 'l', 'o', '!')

The following slides will never be on any COSC 1306 quiz Just for science fans The following slides will never be on any COSC 1306 quiz

Lists in geometry (I) yA xA Consider a point A in the Cartesian plane It has two coordinates xA and yA yA A xA

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 xA a[1] represents yA 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 (Ax – Bx)2 + (Ay – By)2 Pythagora's theorema

Why? xA yA A B yB xB

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) 1.4142135623730951 >>>

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

Lists in geometry (II) Fy F θ Fx

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) y Gy F F+G Fy G Gx Fx x

Adding two vectors (III) >>> def vecadd(f, g) : """ adds two 2-D 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]]

List of lists (II) Can be used to represent matrices ( ) a00 a01 a11