Class 9 – Monday, February 22

Do that again

Did you not expect – The repetition to come – Did you not expect


Look both ways


Agenda


Word of the day


String processing downloads


List processing Downloads


To do list


Program case_by_case.py

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

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

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

Enter text: yellow bellied sapsucker

Enter substring (s): ll

Enter substring (r): L L

text.replace( s, r ): yeL Low beL Lied sapsucker # s's in text replaced with r's

Enter text: yellow bellied sapsucker

Enter substring (s): e

Enter substring (r):

text.replace( s, r ): yllow bllid sapsuckr # s's in text replaced with r's



Acccumulation


Program on_average.py

  • Compute length of the current word of interest.
  • Add that length to running total of the word lengths.

Enter text: over the hill and through the dale

words = ['over', 'the', 'hill', 'and', 'through', 'the', 'dale']

Average word length: 4.0

Enter text: row row row your boat

words = ['row', 'row', 'row', 'your', 'boat']

Average word length: 3.4



List accumulation — program all_int.py

  • Get integer representation of the string.
  • Add integer to the list accumulation
  • Print accumulation.

Enter integers: 3 1 4 1 5 9

nbr_strings: ['3', '1', '4', '1', '5', '9']

numbers: [3]

numbers: [3, 1]

numbers: [3, 1, 4]

numbers: [3, 1, 4, 1]

numbers: [3, 1, 4, 1, 5]

numbers: [3, 1, 4, 1, 5, 9]


Enter integers: 58 2 37 16 99 1 9 23

nbr_strings: ['58', '2', '37', '16', '99', '1', '9', '23']

numbers: [58]

numbers: [58, 2]

numbers: [58, 2, 37]

numbers: [58, 2, 37, 16]

numbers: [58, 2, 37, 16, 99]

numbers: [58, 2, 37, 16, 99, 1]

numbers: [58, 2, 37, 16, 99, 1, 9]

numbers: [58, 2, 37, 16, 99, 1, 9, 23]

# get input

reply = input( 'Enter integers: ' )

print()

# split reply into list of numeric strings

nbr_strings = reply.split()

# see what we got

print( 'nbr_strings:', nbr_strings )

print()

# convert numeric text into list of numbers

# begin with setting up the accumulator

numbers = [] # initialize holder to prepare for adding elements

# process the strings of nbr_strings one-by-one

for ns in nbr_strings :

  # process the current numeric string

  nbr = int( ns ) # get numeric equivalent of ns

  numbers.append( nbr ) # add equivalent to list of numbers

  print( 'numbers:', numbers ) # print the updated list of numbers



I can smile, see

scary person

Schrödinger's cat

cat in a box

 


  🦆 © 2022 Jim Cohoon   Resources from previous semesters are available.