Outline for February 13, 2012
Reading:
§14
Objects, references, aliasing (
Correction from last lecture
)
Assign “banana” to variables
a
,
b
For strings, one copy (
a is b
is
True
) [immutable]
For strings,
a = "hello"; b = a
; then
a is b
is
True
Also,
a = "hello"; b = a[:]
; then
a is b
is
True
For lists, multiple copies (
a is b
is
False
[mutable]
For lists,
a = [ 1, 2, 3 ]; b = a
; then
a is b
is
True
But . . .
a = [ 1, 2, 3 ]; b = a[:]
; then
a is b
is
False
Lists as parameters: can change list elements in function and they are changed in caller [
args2.py
]
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)
Example use: linear search [
linsearch.py
]
Tuples
Used to group data
Like lists, but immutable
A PDF version is available here.
ECS 10, Basic Concepts of Computing
Winter Quarter 2012