Class 38 — Wednesday April 22

Functioning

The end is coming — Soon time to show mastery — Expect to do well


Look both aways


Zoom


Agenda


Art exhibition


Download


Expectation


What to expect for the test


List patterns — suppose alist, blist, and clist are lists and that value v is not a list

result = ...

for element in alist :

  ...

return result

n = len( alist )

for i in range( 0, n ) :

  element = alist[ i ]

  ...

  alist[ i ] = element

  • Appends v to end of alist.
  • Removes first occurence of v from alist.
  • Index of first occurrence of v in alist.
  • Concatenates alist and blist to make clist.
  • Removes the last element from alist. The value of that former element is the return value assigned to v.
  • Removes the element at index i from alist. The value of that former element is the return value assigned to v.

blist = alist + v # cannot add a list and a non-list

blist = alist + [ v ] # can add a list with another list


Warnings


Gotchas


Function sort() versus sorted():


Dictionaries

d = {} # empty dict

d = { 18 : 'voting', 67 : 'retirement', 'eighteen' : 18}

d[ 100 ] = 'centarian' # makes association for key 100 to be 'centarian'

for key in d.keys():

  # do something with key

for value in d.values():

  # do something with value

d[ 18 ] = 'adult'

found = None

for key in d.keys():

  value = d[ key ]

  if value == 18:

  found = key



First question from Test 2

duck.py module




q & a session with jb

 


 
  © 2020 Jim Cohoon   Resources from previous semesters are available.