Class 10 – Wednesday, September 15

Listing

Lest we go forward — Without knowing about lists — We would be wanting


Look both ways


Agenda


Alert


To do list


Downloads


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 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 # text's s's replaced with r's

Enter text: yellow bellied sapsucker

Enter substring (s): e

Enter substring (r):

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



Program slice_of_pie.py

Enter favorite pie: chocolate

Enter two indices i and j: 3 8

s[ i : j ]: colat

s[ i : ]: colate

s[ : j ]: chocolat

s[ : ]: chocolate

Enter favorite pie: pizza

Enter two indices i and j: 2 3

s[ i : j ]: z

s[ i : ]: zza

s[ : j ]: piz

s[ : ]: pizza

Enter favorite pie: garlic tart

Enter two indices i and j: 1 8

s[ i : j ]: arlic t

s[ i : ]: arlic tart

s[ : j ]: garlic t

s[ : ]: garlic tart



Program wowzers.py

Enter your favorite color: teal

Enter some names: willy evander aurelius ann

color = teal

names = ['willy', 'evander', 'aurelius', 'ann' ]

numbers = [12, 7, -11, -12, 59]

T

E

A

L

Willy

Evander

Aurelius

Ann

-12

-7

11

12

-59

t e a l

['willy', 'evander', 'aurelius', 'ann' ]

[5, 7, 8, 3]

length of longest name: 8

first alphabetically = ann



Accumulation


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


 


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