Outline for October 12, 2023
Reading:
§6.1–6.8
Due:
Homework 1, due October 6, 2021
Sequences
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
s in str
returns
True
if
s
is a substring of
str
,
False
otherwise
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
x = ’hEllo’; x[1] = ’e’
produces an error
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
Looping over strings:
for i in str
Example program [
strstuff.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
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
Example of sets [
sets.py
]
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email:
mabishop@ucdavis.edu
ECS 235A, Computer and Information Security
Version of October 11, 2023 at 11:57PM
You can also obtain a PDF version of this.