Class 11 – Friday, September 18

Listing

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


Look both ways


Agenda


For the fun of it


Alert


Downloads


To do list


Lists


List creation basics — program list_creation.py

Task

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

Task

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

Task

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]



Numeric list paradigm — program summing.py

Task

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



Chrestomathics — program secret_decoder.py

Scenario

'abcdefghijklmnopqrstuvwxyz-'

and the indices are

[ 4, 8, 26, 4, 8, 26, 14 ]

'ei-ei-o'

Because s[ 4 ] equals 'e';   s[ 8 ] equals 'i';   s[ 14 ]; equals 'o';   and s[ 26 ] equals '-'.

Algorithm

Some program runs

Enter code phrase: computer

Code phrase: computer

Enter indices: 3 6 5

Indices: [3, 6, 5]

Hidden message: pet


Enter code phrase: gosh look what the cat is up to now

Code phrasee: gosh look what the cat is up to now

Enter indices: 24 5 17 17 27 23 32 0

Indices: [24, 5, 17, 17, 27, 23, 32, 0]

Hidden message: sleeping


Enter code phrase: ‐abcdefghijklmnopqrstuvwxyz‐

Code phrase: ‐abcdefghijklmnopqrstuvwxyz-

Enter indices: 12 15 22 5 27 9 19 0 1 12 12 27 14 5 5 4

Indices: [12, 15, 22, 5, 27, 9, 19, 0, 1, 12, 12, 27, 14, 5, 5, 4]

Hidden message: love‐is‐all‐need



Proof dogs are not as smart as they think they are

csi dog

 


  © 2020 Jim Cohoon   Resources from previous semesters are available.