Class 10 – Wednesday, September 16

Listing

Lest we go forward — We cannot come back again — But beware of baths

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


Look both ways


Agenda


To do list


Downloads


Looping through the characters in a string — program first_name.py

for each letter in string of interest :

# process the current letter

...

for current_letter in string_of_interest :

  # process the current letter

  ...

  • Variable current_letter is called the loop variable or iterator.
  • The block of code indented below the for statement line is the action performed each time through the loop. The block of code is called the body of the loop.
  • The body of this loop is executed once for each letter in string_of_interest.
  • The first time through the loop, current_letter has the value of the first letter in string_of_interest; the next time, it has the value of the second letter in string_of_interest; and so on.

# get name

name = input( "Enter first name: " )

# print the letters in the name one-by-one

for current_letter in name :

  # process the current letter by printing it

  print( current_letter )

Enter name: Annie

A

n

n

i

e

Enter name: Kim

K

i

m

Enter name: Spenser

S

p

e

n

s

e

r



Looping through the items of a list — program your_name.py

for each item in list of interest :

# process the current item

...

for current_item in list_of_interest :

  # process the current item

  ...

  • For this loop, current_item is the loop variable and list_of_interest is the sequence of interest.
  • The body of the loop is executed once for each item in list_of_interest.
  • The first time through the loop, current_item has the value of the first item in list_of_interest; the next time, it has the value of the second item in list_of_interest; and so on.

# get name

text = input( "Enter name: " )

# split the text into a list of words

list_of_words = text.split()

# print the words in list_of_words accompanied by their lengths

for current_word in list_of_words :

  # process the current word

  length_of_current_word = len( current_word ) # determine current word length

  print( current_word, ':', length_of_current_word ) # print wanted info

Enter name: Spiro Aurelius Evander Zambini

Spiro : 5

Aurelius : 8

Evander : 7

Zambini : 7

Enter name: Willie Howard Mays Jr.

Willie : 6

Howard : 6

Mays : 4

Jr. : 3



Accumulation


String accumulation — program esrever.py

Task

Realizations

Some program runs

Enter text: tab

bat

Enter text: stressed

desserts

Enter text: abcdefghijklmnopqrstuvwxyz

zyxwvutsrqponmlkjihgfedcba



Totalling accumulation — program on_average.py

Task

Realizations

Two program runs

Enter text: over the hill and through the dale

Average word length: 4.0

Enter text: row row row your boat

Average word length: 3.4



List accumulation — program all_int.py

Task

Realizations

Some program runs

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]

Code

# 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



Hoos got your back — food insecurity


Coney Island Cyclone roller coaster

Coney Island Cyclone



Willy Mays

Willy Mays button



Marcus Aurelius

Metropolitan Marcus Aurelius Roman 2C AD 2



Evander of Pallantium

Evander of Greek mythology

 


  © 2020 Jim Cohoon   Resources from previous semesters are available.