Review

Lists

Lists

Lists

Lists

Lists

Lists

Lists

List Methods

Access many list manipulation functions using dot-notation (just like modules)

L.append(x)- add an item to the end of L
L.extend(M)- append all items in M to the end of L
L.insert(i, x)- insert x into L at position i
L.remove(x)- remove the first occurance of x from L
L.pop(i)- remove the item at position i and return it (or from end if no i)
L.index(x)- find the index of the first item that matches x on L
L.count(x)- return the number of times x occurs in L
L.sort()- sort the items in L, in place
L.reverse()- reverse the order of items in L, in place.

Sequence Types

Tuples

More Iteration: for-in Statement

Use the for-in statement to loop over all elements of a sequence type (list, string, ...)

for variable in list :
    statement1
    statement2
>>> for op in ['tall', 'grande', 'venti']:
>>>    print( op + ' is an option' )

tall is an option
grande is an option
venti is an option

More Iteration: for-in Statement

The built-in range() function helps us with for-in statements

range(min, max)- generates a list of numbers from min to max-1
range(max)- generates a list of numbers from 0 to max-1
range(min, max, skip)- generates a list of every skip number from min to max-1
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> range(4, 10)
[4, 5, 6, 7, 8, 9]

>>> range(4, 10, 2)
[4, 6, 8]

More Iteration: for-in Statement

The built-in range() function helps us with for-in statements

sizes = ['tall', 'grande', 'venti']

for i in range( len(sizes) ):
    print( i, sizes[i] )

0 tall
1 grande
2 venti

Problem Solving with Loop Patterns

/

#