Class 11 – Friday, September 20
Looping and nested looping
An optimist thinks — Elevator close button — Really does something
Look both ways
Agenda
- String manipulation and analysis
- String, loop, and list chrestomathics
- Extend
print()
function functionality
- Introduce the pillow.
Word of the day
- Immutable
- Definition: cannot be changed
- Usage: Strings are immutable.
- Pronunciation: link
Downloads
- Program strung_out.py
- Program vowely.py
- Program grabby.py
- Program aeschylus.py
- Program thats_printastic.py
- Program test-pillow.py
To do list
- Install pillow.
- Reveal one of your super power(s)
- Share a selfie
- Review class artifacts.
- Complete the homework
Strings
- Program strung_out.py
- Introduces string member functions
capitalize()
,lower()
,upper()
, andreplace()
for producings variants of its string.
- Introduces string member functions
count()
, andfind()
for determining properties of its string.
# some strings
s = "immUTAble!"
t = "banana"
u = "AN"
v = "an"
w = "___"
# produce variants of a string
s_c = s.capitalize()
s_l = s.lower()
s_u = s.upper()
r1 = t.replace( u, w )
r2 = t.replace( v, w )
r3 = t.replace( v, "" )
# examine a string for its substrings
c_u = t.count( u )
c_v = t.count( v )
c_v1 = t.count( v, 1 )
c_v2 = t.count( v, 2 )
c_v8 = t.count( v, 8 )
i_u = t.find( u )
i_v = t.find( v )
i_v1 = t.find( v, 1 )
i_v2 = t.find( v, 2 )
i_v8 = t.find( v, 8 )
Program run
s: immUTAble!
t: banana
u: AN
v: an
w: ___
s.lower(): immutable! # all lower case version of s
s.upper(): IMMUTABLE! # all upper case version of s
s.capitalize(): Immutable! # version of s initial character capitalized, rest lower case
t.replace( u, w ): banana # version of t with its u's replaced with w
t.replace( v, w ): b______a # version of t with its v's replaced with w
t.replace( v, "" ): ba # version of t with its v's replaced with ""
t.count( u ): 0 # number of string u in t
t.count( v ): 2 # number of string v in t
t.count( v, 1 ): 2 # number of string v in t, starting at index 1
t.count( v, 2 ): 1 # number of string v in t, starting at index 2
t.count( v, 8 ): 0 # number of string v in t, starting at index 8
t.find( u ): -1 # first occurrence of string u in t
t.find( v ): 1 # first occurrence of string v in t
t.find( v, 1 ): 1 # first occurrence of string v in t, starting at index 1
t.find( v, 2 ): 3 # first occurrence of string v in t, starting at index 2
t.find( v, 8 ): -1 # first occurrence of string v in t, starting at index 8
Chrestomathics
- Program vowely.py
- Gets a single word from the user and separately reports its length and its number of vowels
Two possible program runs
Enter a word: subbookkeeper
length: 13
vowels: 6
Enter a word: supercalifragilisticexpialidocious
length: 34
vowels: 16
- Program grabby.py
- Separately prompt for a string
s
and a list of integersnumbers
. The program displays the string formed by usingnumbers
as indices intos
.
- For example, suppose
s
equals'abcdefghijklmnopqrstuvwxyz-'
andnumbers
equals[ 4, 8, 26, 4, 8, 26, 14 ]
the output is the string'ei-ei-o'
, becauses[ 4 ]
equals'e'
,s[ 8 ]
equals'i'
,s[ 14 ]
, equals'o'
, ands[ 26 ]
equals'-'
. There should be no other output.
Two possible program runs with preferred form of output
Enter string: computer
Enter list of indices: 5 1 7
tor
Enter string: gosh look what the cat is up to now
Enter list of indices: 24 5 17 17 27 23 32 0
sleeping
Two possible program runs whose output form is acceptable for today
Enter string: computer
Enter list of indices: 5 1 7
t
o
r
Enter string: gosh look what the cat is up to now
Enter list of indices: 24 5 17 17 27 23 32 0
s
l
e
e
p
i
n
g
- Program thats_printastic.py
- Introduces the optional closing parameter
end="..."
A program run
Wa-Hoo-Wa
Rah-Rah-Rah
Wa-Hoo-Wa!!!Rah-Rah-Rah
Wa-Hoo-Wa !!! Rah-Rah-Rah
t-a-t-t-a-r-r-a-t-t-a-t-
saippuakivikauppias
- Program nested_looper.py
- Introduces nested looping. Where for a user-supplied
n
, the program loopsn
times and on thei
th iteration prints a line of the form
i: 0 1 2 ... i
Two possible program runs
Enter number: 5
0: 0
1: 0 1
2: 0 1 2
3: 0 1 2 3
4: 0 1 2 3 4
Enter number: 12
0: 0
1: 0 1
2: 0 1 2
3: 0 1 2 3
4: 0 1 2 3 4
5: 0 1 2 3 4 5
6: 0 1 2 3 4 5 6
7: 0 1 2 3 4 5 6 7
8: 0 1 2 3 4 5 6 7 8
9: 0 1 2 3 4 5 6 7 8 9
10: 0 1 2 3 4 5 6 7 8 9 10
11: 0 1 2 3 4 5 6 7 8 9 10 11
I can smile, see
© 2019 Jim Cohoon | Resources from previous semesters are available. |