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
- Make loops right
- Provide you more background on list basics
To do list
- Review class artifacts.
- Review epistles
- Complete the big_production.py homework
Downloads
- Program first_name.py
- Introduction to for character in a string loop.
- Program your_name.py
- Introduction to for word in text loop.
- Program esrever.py
- Performs string accumulation using a loop.
- Program on_average.py
- Performs summing accumulation using a loop.
- Program all_int.py
- Performs list accumulation using a loop.
Looping through the characters in a string — program first_name.py
- Suppose what we want do is process the letters in a string one-by-one. Such an algorithm would like
for each letter in string of interest :
# process the current letter
...
- Python provides the
for
loop for doing such processing. It would like the following
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 instring_of_interest
; the next time, it has the value of the second letter instring_of_interest
; and so on.
- If our program task is to just print the letter one-by-one for a user-supplied first name. It would like the following
# 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 )
- Some possible program runs could be
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
- Suppose what we want do is process the items in a list one-by-one. Such an algorithm would like
for each item in list of interest :
# process the current item
...
- The
for
loop for can do such processing. The entity following the keywordin
is not limited to strings, it can be any type of sequence (e.g., a list of words or numbers, or a range of numbers).
for current_item in list_of_interest :
# process the current item
...
- For this loop,
current_item
is the loop variable andlist_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 inlist_of_interest
; the next time, it has the value of the second item inlist_of_interest
; and so on.
- If our program task is to individually print the words for a user-supplied name accompanied with their lengths, the program would like the following
# 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
- Some possible program runs could be
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
- Many times problem solving requires we analyze some sequence (e.g, string or list) to determine a property or build a new sequence. The determination can require each element in the sequence, add (accumulate) to the answer. For example, from text determine average word length or from numbers determine their absolute values or from a string determine its reverse.
- To build the reverse of a string, each character in the string needs to be part of the reverse. The reverse is gotten by repeatedly adding (accumulating) the characters to the reversal. For this to work out, the reversal needs to be initialized before the characters in the string are considered. A string accumulator needs to be intialized to the empty string
''
.
- If the answer requires the items in list contribute to a total, then an accumulation is needed of the individual terms — the accumulator is updated by repeatedly adding to it. For this to work out, the accumulator needs to be initialized before the list is processed. For totaling, the accumulator is initialized to zero.
- Other data processing can require a list be built out of some input data. Here, the accumulator needs to be initialized to the empty list
[]
.
- Other data processing can require the product of some numeric data. Here, the accumulator needs to be initialized to 1.
String accumulation — program esrever.py
Task
- Computes the reverse of a user-suppplied string
Realizations
- Because the length of text is not know when the program is being written, a loop is needed to process the input characters.
- Each input character contributes to the reversal
- The accumulator need to be initiliazed to the empty string
''
.
Some program runs
Enter text: tab
bat
Enter text: stressed
desserts
Enter text: abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba
Totalling accumulation — program on_average.py
Task
- Examines a list of user-supplied words and reports determine average word length.
Realizations
- Because the number of words is not know when the program is being written, a loop is needed to process the words.
- To compute the average word length, need to know the number of words and the sum of the word lengths.
- The loop needs to two things for each word: compute its length and add its length to a running total.
- The running total needs to be initialized before the loop.
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
- Produce a list of integers from user-supplied data.
Realizations
- Function
int()
can only be applied to one string at a time.
- List of integers will need to be accumulated by processing the numeric strings one at a time.
- For each numeric string it will need to be converted to an integer and added to the list of integers being built
- The list of integers needs to be initalized to an empty list.
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
- Nationwide more than 10% of college students are not able to afford nutrious food. This problem occurs at UVA also. I am aware of two sources at UVA to help out.
- The Engineering School has a food pantry in Thornton A121. It operates on the honor system and is meant to help students with need.
- The unversity assists with the Community Food Pantry in the Runk Green Room. See the website for details.
- If you find yourself experiencing some financial hardships, please consider talking with anyone in the Undergraduate Programs Office. They can help you find even more resources.
- If you can pay it forward, please help by donating items or money to either pantry.
Coney Island Cyclone roller coaster
Willy Mays
Marcus Aurelius
Evander of Pallantium
© 2020 Jim Cohoon | Resources from previous semesters are available. |