Class 42 — Monday, December 6

One more chance to be a class

Programming basics — Your semester achievement — Bask in the success

Dream big and work hard — Celebrate diversity — Always pay forward

sunrise



Look both ways


Agenda


Thanks again to the TAs for making the course successful

Amshala BharathanKevin LukSophia Rogers Meghan Tracy
Katherine BrickleyRachel McNamaraMaryJeanine Seaman
Bailey GreggsJimmy NjugunaShivania Surtio


Want to be a TA for CS 1112


Test 3


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.

Sequence expression — adapted in part from docs.python.org

Operation Evaluation
len( s ) Length of sequence s
sum( s ) Sums the items in sequence s
min( s ) Smallest item in sequence s
max( s ) Largest item in sequence s
x in s True if an item of sequence s equals x; otherwise, False
x not in s False if an item of sequence s equals x; otherwise, True
s + t New concatenation of sequences s and t
s[ i ] Value of item at index ith of sequence s
s[ : ] New copy of sequence s
s[ i : ] New copy of sequence s for the elements from index i up to and including last sequence element
s[ : j ] New copy of sequence s for the elements from index 0 up to but not including index j
s[ i : j ] New copy of sequence s for the elements from index i up to but not including index j
s[ i : j : k ] New copy of sequence s for the elements from index i up to but not including index j with step k
s.index( x ) Index of the first occurrence of item x in sequence s
s.index( x, i ) Index of the first occurrence of item x in sequence s at or after index i
s.count( x ) Total number of occurrences of item x in sequence s
s * n New sequence equivalent to adding sequence s to itself n times


List patterns

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


Dictionaries

d = { 18 : "voting", 67 : "retirement", "CS 1112" : "course" }

d = {}

d[ 18 ] = "voting"

d[ 67 ] = "retirement"

d[ "eighteen" ] = 18

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




birthday gift ducks for CS