Outline for October 29, 2020
Reading
: §8
Due
: Homework 2, due October 30, 2020
I have to change my office hours tomorrow. I am moving them to 3:30pm–4:30pm.
Lists
Sequence of values (ints, floats, strings, other lists, etc.)
Denoted by square brackets
[ ]
with values separated by commas
Lists are mutable
How to create a list
Lists and strings [
datecvt.py
]
Program to print words in a line [
lines.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 element
list[i]
;
i
can be a slice
List methods
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)
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
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email:
mabishop@ucdavis.edu
MHI 289I, Programming for Health Informatics
Version of October 28, 2020 at 11:20PM
You can also obtain a PDF version of this.