Outline for May 13, 2014
Reading
:
text
, § 11, 18
Due
: Homework #3, due May 21, 2014
Program to compute some statistics [
addup.py
]
What you can do with lists
Check membership:
in
,
not in
+
: concatenation
*
: repetition
list[a:b]
: slice list from
a
to
b
−1
del list[
i
]
: delete
list[i]
;
i
can be a slice
Objects, references, aliasing
For strings, one copy: assume
a = "banana"
After
b = a
or
b = a[:]
, then
a is b
is
True
For lists, multiple copies: assume
A = [ 1, 2, 3 ]
After
B = A
, then
A is B
is
True
After
B = A[:]
, then
A is B
is
False
enumerate(L)
produces pairs (
index
,
list element
)
Lists as parameters: can change list elements in function and they are changed in caller [
args2.py
]
Add elements to, remove elements:
L.append(x)
,
L.extend(ls)
,
L.insert(i, x)
,
L.pop()
,
L.remove(x)
Element ordering:
L.reverse()
,
L.sort()
Other:
L.count(x)
,
L.index(x)
Tuples
Used to group data
Like lists, but immutable
Recursion
n
factorial [
nfact.py
]
Fibonacci numbers [
rfib.py
]
Sum of digits [
sumdigits.py
]
You can also obtain a PDF version of this.
Version of May 13, 2014 at 10:36PM