Class 10 – Wednesday, February 5
Listing
Lest we go forward — Without knowing about lists — We would be wanting
Look both ways
Agenda
- Introduce lists
- Introduce basic list processing algorithms
To do list
- Review class artifacts.
- Review epistles
- Complete the big_production.py homework
Downloads
- Program the_great_reverso.py
- Program list_creation.py
- Program list_support.py
- Program list_building.py
- Program numbing.py
- Program summing.py
- Program slice_of_life.py
Warning
- Anyone who asks me today about a function
sum()
earns negative ducks.
Lists
- Tell me about them
String answer accumulation — program the_great_reverso.py
- Produce and print the reverse of a user-supplied word (through accummulation)
-
Some possible program runs
Enter word: desserts
stressed
Enter word: reverse
esrever
Enter word: abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba
List creation basics — program list_creation.py
-
Code
s = "we are in it together"
values = [ ]
stuff = [ 'abc', 1112, 2.71, ]
digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]
words = s.split()
print( "values =", values )
print( "stuff =", stuff )
print( "digits =", digits )
print( "words =", words )
-
Program run
values = []
stuff = ['abc', 1112, 2.71]
digits = [3, 1, 4, 1, 5, 9, 2, 6]
words = ['we', 'are', 'in', 'it', 'together']
List pythonics — program list_support.py
-
Code
s = "we are in it together"
values = [ ]
stuff = [ 'abc', 1112, 2.71, ]
digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]
words = s.split()
vlen = len( values )
slen = len( stuff )
dlen = len( digits )
wlen = len( words )
print( "size of", values, "=", vlen )
print( "size of", stuff, "=", slen )
print( "size of", digits, "=", dlen )
print( "size of", words, "=", wlen )
dmax = max( digits )
dmin = min( digits )
wmax = max( words )
wmin = min( words )
print( "max of", digits, "=", dmax )
print( "min of", digits, "=", dmin )
print( "max of", words, "=", wmax )
print( "min of", words, "=", wmin )
-
Program run
size of [] = 0
size of ['abc', 1112, 2.71] = 3
size of [3, 1, 4, 1, 5, 9, 2, 6] = 8
size of ['we', 'are', 'in', 'it', 'together'] = 5
max of [3, 1, 4, 1, 5, 9, 2, 6] = 9
min of [3, 1, 4, 1, 5, 9, 2, 6] = 1
max of ['we', 'are', 'in', 'it', 'together'] = we
min of ['we', 'are', 'in', 'it', 'together'] = are
List construction — program list_building.py
-
Code
values = [ ]
values.append( "u" )
values.append( "v" )
values.append( "a" )
values.append( "!" )
values.append( " " )
values.append( "u" )
values.append( "v" )
values.append( "a" )
values.append( "!" )
print( "values =", values )
ints = []
for i in range( 0, 10 ) :
ints.append( i )
print( "ints =", ints
-
Program run
values = ['u', 'v', 'a', '!', ' ', 'u', 'v', 'a', '!']
ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Processing a numeric list — program numbing.py
- Demonstrates accumulation involving a list of numbers
- Prompts and gets as input a list of integer numbers. Converts the gotten input into a list of numeric strings. Then by accumulation, converts the numeric strings one-by-one to get a list of integers (a loop is needed), where each time through the loop the current accumulation is printed.
-
Code
reply = input( 'Enter a list of numbers: ' )
slist = reply.split()
print( 'reply =', reply )
print( 'slist =', slist )
# first set up a holder of elements
nlist = [] # initialize holder to prepare for adding elements
# now get conversions of the elements of slist one-by-one
for s in slist :
# get numeric equivalent of s
nbr = int( s )
# add the equivalent to the numeric list
nlist.append( nbr )
# print the updated list
print( nlist )
-
Some possible program runs
Enter a list of numbers: 3 1 4 1 5 9
reply = 3 1 4 1 5 9
slist = ['3', '1', '4', '1', '5', '9']
nlist = [3]
nlist = [3, 1]
nlist = [3, 1, 4]
nlist = [3, 1, 4, 1]
nlist = [3, 1, 4, 1, 5]
nlist = [3, 1, 4, 1, 5, 9]
Enter a list of numbers: 58 2 37 16 99 1 9 23
reply = 58 2 37 16 99 1 9 23
slist = ['58', '2', '37', '16', '99', '1', '9', '23']
nlist = [58]
nlist = [58, 2]
nlist = [58, 2, 37]
nlist = [58, 2, 37, 16]
nlist = [58, 2, 37, 16, 99]
nlist = [58, 2, 37, 16, 99, 1]
nlist = [58, 2, 37, 16, 99, 1, 9]
nlist = [58, 2, 37, 16, 99, 1, 9, 23]
Numeric list paradigm — program summing.py
- Demonstrates accumulation involving a list of numbers
- Prompts, gets, and converts a list of integer inputs into a list of integers.
- Then by accumulation, sums the list of integers
-
Code
reply = input( 'Enter a list of numbers: ' )
slist = reply.split()
nlist = []
for s in slist :
nbr = int( s )
nlist.append( nbr )
# by accumulation get the sum of the numbers
total = 0
for nbr in nlist :
total = total + nbr
# print summation
print( "sum(", nlist, "):", total )
Some possible program runs
Enter a list of numbers: 3 1 4 1 5 9
sum( [3, 1, 4, 1, 5, 9] ): 23
Enter a list of numbers: 58 2 37 16 99 1 9 23
sum( [58, 2, 37, 16, 99, 1, 9, 23] ): 245
Program slice_of_life.py
— introduces the sequence slicing operator :
- Code of interest
s = 'of to in is on by it or be at'
words = s.split()
ns = len( s )
nw = len( words )
i1 = ns // 4
i2 = 3 * ns // 4
s1 = s[ i1 : i2 ]
s2 = s[ : i2 ]
s3 = s[ i1 : ]
s4 = s[ : ]
...
j1 = nw // 5
j2 = 4 * nw // 5
w_slice1 = words[ j1 : j2 ]
w_slice2 = words[ : j2 ]
w_slice3 = words[ j1 : ]
w_slice4 = words[ : ]
-
Program run
s = of to in is on by it or be at
words = ['of', 'to', 'in', 'is', 'on', 'by', 'it', 'or', 'be', 'at']
s[ 7 : 21 ] = n is on by it
s[ : 21 ] = of to in is on by it
s[ 7 : ] = n is on by it or be at
s[ : ] = of to in is on by it or be at
words[ 2 : 8 ] = ['in', 'is', 'on', 'by', 'it', 'or']
words[ : 8 ] = ['of', 'to', 'in', 'is', 'on', 'by', 'it', 'or']
words[ 2 : ] = ['in', 'is', 'on', 'by', 'it', 'or', 'be', 'at']
words[ : ] = ['of', 'to', 'in', 'is', 'on', 'by', 'it', 'or', 'be', 'at']
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 to either pantry.
Proof dogs are not as smart as they think they are
© 2020 Jim Cohoon | Resources from previous semesters are available. |