Class 38 — Wednesday April 22
Functioning
The end is coming — Soon time to show mastery — Expect to do well
Look both aways
Zoom
Agenda
- Take stock
Art exhibition
Download
- Module duck.py
Expectation
- You will take test 2 as close to 2 PM EDT as possible. No matter what you will complete the test before 10 AM EDT on Saturday April 25.
- Please submit
duck.py
today.
What to expect for the test
- Knowledge of
if
andfor
statements; along with the ability to use them when problem solving.
- Ability to write functions using numbers, strings, lists, dicts, datasets, random, and image manipulation akin to the image flipping and bluing examples.
- No short answer questions
- If you have an accomodation, then you get your accommodation multiplier * 90 minutes for test taking time.
List patterns — suppose alist
, blist
, and clist
are lists and that value v
is not a list
- To analyze a list use
result = ...
for element in alist :
...
return result
- To modify a list use
n = len( alist )
for i in range( 0, n ) :
element = alist[ i ]
...
alist[ i ] = element
- Important list operations
alist.append( v )
- Appends
v
to end ofalist
.
alist.remove( v )
- Removes first occurence of
v
fromalist
.
alist.index( v )
- Index of first occurrence of
v
inalist
.
clist = alist + blist
- Concatenates
alist
andblist
to makeclist
.
v = alist.pop( )
- Removes the last element from
alist
. The value of that former element is the return value assigned tov
.
v = alist.pop( i )
- Removes the element at index
i
fromalist
. The value of that former element is the return value assigned tov
.
- Major gotcha
blist = alist + v # cannot add a list and a non-list
blist = alist + [ v ] # can add a list with another list
Warnings
- A
print()
statement in a function is not supplying a return value. It is sending a message to the program user.
- An
input()
statement is wrong to include in a function body, information comes into a function through its parameters.
- No function you will be asked to implement should have a
print()
orinput()
statement.
Gotchas
- Nested for-loops usually wrong unless using a list-of-lists (e.g., a dataset) or looking at one list in relation to another list or image manipulation
Function sort()
versus sorted():
x.sort()
modifies the contents ofx
to be in non-descending order and returnsNone
sorted( x )
creates a new list that has the elements ofx
in non-descending order, returns that new list, and does not modifyx
Dictionaries
- Unordered collection of key-value pairs
- Initialization examples
d = {} # empty dict
d = { 18 : 'voting', 67 : 'retirement', 'eighteen' : 18}
- Access values by key:
d[ 18 ]
- Means the same thing as 'voting'
- Access values by key:
d[ 21 ]
- Will be an error because 21 is not a key
- Access values by key:
d[ 'voting' ]
- Will be an error because
'voting'
is not a key
- Access values by key:
d.get( 18 )
- Means the same thing as
'voting'
- Access values by key:
d.get( 21 )
- Means the same thing as None (not an error)
- Access key by value: not easy, probably need a loop
- Collection of all keys:
d.keys()
- Collection of all values:
d.values()
- Indices: there are none (just keys)
- List of all keys:
list( d.keys() )
- Set the value of a key:
d[ 100 ] = 'centarian' # makes association for key
100
to be'centarian'
- How do we look at the keys a
dict
for key in d.keys():
# do something with key
- How do we look at the values of a
dict
for value in d.values():
# do something with value
- Trying to find and change a particular entry
- By key; e.g., change key
18
to'adult'
d[ 18 ] = 'adult'
- By value (find key for value 18)
found = None
for key in d.keys():
value = d[ key ]
if value == 18:
found = key
First question from Test 2
© 2020 Jim Cohoon Resources from previous semesters are available.