Class 9 – Monday, February 3
Do that again
Did you not expect – The repetition to come – Did you not expect
Look both ways
Agenda
- Looping
- Expanding string manipulation repertoire
- String, loop, and list chrestomathics
Word of the day
- Immutable
- Definition: cannot be changed
- Usage: Strings are immutable.
- Pronunciation: link
Downloads
- Program wordy.py
- Program m_and_ns.py
- Program n_and_ms.py
- Program case_by_case.py
- Program countability.py
- Program find_the_spot.py
- Program replacement.py
- Program vowely.py
To do list
- Review class artifacts.
- Read the epistle on lists.
- Check out my documentation for the
random
module.
- Check out my documentation for the
list
module.
Program wordy.py
— examines a list of user-supplied words and reports their lengths
- Some program runs
Enter text: All hoos got your back
Supplied text = All hoos got your back
words = ['All', 'hoos', 'got', 'your', 'back']
All : 3
hoos : 4
got : 3
your : 4
back : 4
Enter text: It was a dark and stormy night
Supplied text = It was a dark and stormy night
words = ['It', 'was', 'a', 'dark', 'and', 'stormy', 'night']
It : 2
was : 3
a : 1
dark : 4
and : 3
stormy : 6
night : 5
Enter text:
Supplied text =
words = []
- A variant to consider — determine average word length
Enter text: Everyone has the right to education
Supplied text = Everyone has the right to education
words = ['Everyone', 'has', 'the', 'right', 'to', 'education']
Everyone : 8
has : 3
the : 3
right : 5
to : 2
education : 9
Average word length: 5.0
Program m_and_ns.py
— prints a user-specified upward interval of numbers
- Some program runs
Enter start and end: 2 7
2
3
4
5
6
7
Enter start and end: 4 8
4
5
6
7
8
Program n_and_ms.py
— prints a user-specified downward interval of numbers
- Some program runs
Enter start and end: 10 1
10
9
8
7
6
5
4
3
2
1
Enter start and end: 12 11
12
11
Program case_by_case.py
— introduces string case functions capitalize()
, lower()
, and upper()
- A program run
Enter text: thanks VERY mucH.
text: thanks VERY mucH.
text.lower() = thanks very much. # all lower case
text.upper() = THANKS VERY MUCH. # all upper case
text.capitalize() = Thanks very much. # initial character capitalized, rest lower case
Program countability.py
— introduces string function count()
- A program run
Enter text: any bananas
Enter substring: an
Enter index: 7
text.count( substring ) = 3 # count all
text.count( substring, 7 ) = 1 # count starting from index 7
Program find_the_spot.py
— introduces string member function find()
- A program run
Enter text: aardvark
Enter substring: a
text.find( substring ): 0 # first occurrence
text.find( substring, 1 ): 1 # second occurrence
text.find( substring, 2 ): 5 # third occurrence
Program replacement.py
— introduces string function replace()
- A program run
Enter text: yellow bellied sapsucker
Enter substring (s): ll
Enter substring (r): L L
text.replace( s, r ): yeL Low beL Lied sapsucker # text's s's replaced with r's
Enter text: yellow bellied sapsucker
Enter substring (s): l
Enter substring (r):
text.replace( s, r ): yllow bllid sapsuckr # text's s's replaced with r's
I can smile, see
Schrödinger' cat
© 2020 Jim Cohoon | Resources from previous semesters are available. |