Class 09 – Monday, September 13
Do that again
Did you not expect – The repetition to come – Did you not expect
We are humankind — What a mantra that could be — I'm human and kind
Look both ways
Agenda
- String processing
- List basics
- List chrestomathics (e.g., greedy programming)
Word of the day
- Queueing
For the fun of it
- Reveal one of your super power(s).
Downloads
- Program kinda_stringy.py
- Introduces character by character looping
- Program adage.py
- Introduces
range()
looping
- Program in_support.py
- Shows basic list operations
- Program building.py
- Introduces list building
To do list
- Review class artifacts.
- Read the epistle on lists.
- Check out my documentation for the
list
module.
Program kinda_stringy.py
-
Purpose
- Introduce character by character looping
-
Some possible program runs
Enter text: four score
f
o
u
r
s
c
o
r
e
Enter text: step by step, inch by inch, Niagara Falls
s
t
e
p
b
y
s
t
e
p
,
i
n
c
h
b
y
i
n
c
h
N
i
a
g
a
r
a
F
a
l
l
s
Program adage.py
-
Purpose
- Introduce
range()
looping
- Prints a user-supplied adage an indicated number of times.
-
Discussion
- The built-in function
range()
when used returns a sequence of integers.
- The function has some flexibility in its number of arguments. Except for a one or two exceptions we will always invoke it with two integer arguments. There will be no exceptions today.
- Suppose
a
andb
are integers, then invocation
range( a, b )
Produces the sequence of integers
a
,a
+ 1, ...,b
- 1. Pay attention to this fact — the sequence ends before the value of the second argument.
-
Some program runs
Enter adage: We are humankind
Enter number of lines to be printed: 5
We are humankind
We are humankind
We are humankind
We are humankind
We are humankind
Enter adage: Love is all you need
Enter number of lines to be printed: 3
Love is all you need
Love is all you need
Love is all you need
Program in_support.py
-
Task
- Show some built-in list functions
s = "we are in it together"
values = [ ]
stuff = [ 'abc', 1112, 2.71, ]
digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]
words = s.split()
vlen = len( values )
slen = len( stuff )
dlen = len( digits )
wlen = len( words )
print( "size of", values, "=", vlen )
print( "size of", stuff, "=", slen )
print( "size of", digits, "=", dlen )
print( "size of", words, "=", wlen )
dmax = max( digits )
dmin = min( digits )
wmax = max( words )
wmin = min( words )
print( "max of", digits, "=", dmax )
print( "min of", digits, "=", dmin )
print( "max of", words, "=", wmax )
print( "min of", words, "=", wmin )
-
Program run
size of [] = 0
size of ['abc', 1112, 2.71] = 3
size of [3, 1, 4, 1, 5, 9, 2, 6] = 8
size of ['we', 'are', 'in', 'it', 'together'] = 5
max of [3, 1, 4, 1, 5, 9, 2, 6] = 9
min of [3, 1, 4, 1, 5, 9, 2, 6] = 1
max of ['we', 'are', 'in', 'it', 'together'] = we
min of ['we', 'are', 'in', 'it', 'together'] = are
Program building.py
-
Task
- Introduce list
append()
function
values = [ ]
values.append( "u" )
values.append( "v" )
values.append( "a" )
values.append( "!" )
values.append( " " )
values.append( "u" )
values.append( "v" )
values.append( "a" )
values.append( "!" )
print( "values =", values )
ints = []
for i in range( 0, 10 ) :
ints.append( i )
print( "ints =", ints
-
Program run
values = ['u', 'v', 'a', '!', ' ', 'u', 'v', 'a', '!']
ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Coney Island Cyclone roller coaster
Schrödinger's cat