Class 37 — Wednesday, April 20

I know that you know that

Programming basics — Your semester achievement — Bask in the success


Look both ways


Agenda

soup stock from wikimedia

Downloads


but_its_on_sale.py - functions and optional parameters

A $ 50 item on sale for 40 % off will cost $ 30.0

A $ 10 item on sale for 15 % off will cost $ 8.5

A $ 50 item on sale for 15 % off will cost $ 42.5

A $ 10 item on sale for 40 % off will cost $ 6.0

A $ 10 item on sale for 0 % off will cost $ 6.0


naming_names.py - while looping

Enter desired name: Janine

Enter list of names: Laura Michael Elizabeth Isabelle Janine

Enter random seed: 52

Isabelle

Janine


Enter desired name: Michael

Enter list of names: Laura Michael Elizabeth Isabelle Janine

Enter random seed: 52

Isabelle

Janine

Laura

Elizabeth

Isabelle

Elizabeth

Isabelle

Janine

Laura

Laura

Janine

Elizabeth

Laura

Elizabeth

Janine

Laura

Laura

Laura

Michael



step_case.py - functions and strings

FLORIDA georgia = step_case("Florida Georgia")

MICHIGan ohio = step_case("MICHIGAN Ohio")

VA MAryland = step_case("va maryland")


the_average_thing.py - while loops and user-inputted dictionary

The user inputs pairs of grocery store items and costs, and the program creates a dictionary representation of this user-specified inventory. When the user enters the empty string, the dictionary is fully built. Next, the program will calculate and print out the average cost of all the items. Finally, the program will print out the item that costs closest to the average price, along with that item's price.


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

 


  🦆 © 2022 Jim Cohoon   Resources from previous semesters are available.