Class 34 — November 11

Functioning

On Veteran's day — Remember sacrifices — Made on our behalf


Look both ways


Agenda


Past homework


Test taking


What to expect for the test


Built-in functions


Functions

  • A return statement can optionally have a return expression following the keyword return. The expression is evaluated. A copy of that value is the return value of the function. The return value is said to be the value of the evaluation of the function invocation.
  • All Python functions return values – if no return statement with a return expression is executed. The function returns None.

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

alist = alist.append( v ) # alist is no longer a list, it is None



Warnings


FWIW


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

 


  © 2020 Jim Cohoon   Resources from previous semesters are available.