Class 10 – Wednesday, February 5

Listing

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


Look both ways


Agenda


To do list


Downloads





Warning


Lists


String answer accumulation — program the_great_reverso.py

Enter word: desserts

stressed

Enter word: reverse

esrever

Enter word: abcdefghijklmnopqrstuvwxyz

zyxwvutsrqponmlkjihgfedcba



List creation basics — program list_creation.py

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 )

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

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 )

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

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

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

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 )

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

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 :

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[ : ]

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


Proof dogs are not as smart as they think they are

csi dog




Coney Island Cyclone

 


 
  © 2020 Jim Cohoon   Resources from previous semesters are available.