Outline for February 6, 2012
Reading:
§8
Sequences
Accustomed to use in for loops (
for i in range(5):
)
Sequences are a series of values in a particular order
In Python predominantly strings and lists but also sets and tuples
Strings
Sequence of characters (characters are strings of length 1)
Strings are immutable; really important for functions
Basic string operations
+
, concatenation for strings
*
, repetition repeats given value
len()
returns length of sequence
Indexing, var[position]
Count from 0 to
len(var)
−1
Position can be a negative number to count from right
Assignment with indexing doesn’t work as strings immutable
Slicing,
var[start:end]
Value at index end not included in slice
If omitted, starting value defaults to 0 and ending value defaults to last index + 1
Can use negative index
String module
import string
String methods:
s.capitalize()
,
s.replace(old, new)
,
s.find(sub)
,
s.rfind(sub)
,
s.upper()
,
s.lower()
A PDF version is available here.
ECS 10, Basic Concepts of Computing
Winter Quarter 2012