Class 32 — November 12
Everything is functioning aok
Part two is ending — Soon time to show mastery — Expect to do well
Look both ways
- HW 23: aid.py
- HW 21: quad.py
- HW 20: magnificent.py
- HW 19: take_two.py
- HW 18: olio.py
- HW 17: same_difference.py
- HW 16: do_you_relate.py
What to expect for the test
- Knowledge of function and function invocation vocabulary, syntax, and semantics
- Knowledge of lists, strings, datasets, random, loops, strings in the context of functions
- No graphics or dicts
- Short answer / objective questions, analysis, and implementation
- To analyze a list use
result = ...
for element in the_list :
- To modify a list use
n = len( the_list )
for i in range( 0, n ) :
element = the_list[ i ]
the_list[ i ] = element
- List gotcha
v = 1112
the_list = the_list + v # cannot add a list and a non-list
the_list = the_list + [ v ] # can add a list and a non-list
- Important list operations
the_list.append( v )
vto end of
the_list.count( v )
- Returns the number of occurrences of
the_list.remove( v )
- Removes first occurence of
the_list.index( v )
- Index of first occurrence of
list3 = list1 + list2
- Functions must be defined.
- Functions can be invoked. The invocation identifies the function to be started up. The invocation includes the arguments. The arguments are evaluated. Copies of the values are used to initial the parameters specific in the function definition,
- Header: first line
def name ( parameters ) :
- Body: remaining lines in function. How can you tell? They are indented. Body is also known as the statement list
- When a
returnstatement is reached in a function, the function stops executing its instructions; that is, the function immediately stop its execution.
returnstatement 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
returnstatement with a return expression is executed. The function returns
- A local variable is a variable initialized/defined inside a function definition.
- Local variables only exist inside the function definition (limited "scope")
- The parameters of a function are the variables listed with the parentheses in the function header.
- Parameters are local variables
- The arguments to a function are the values given within the parentheses of an invocation of the function.
- The Python way of handling the arguments is pass by value; that is, the arguments are evaluated to determine their values. Copies of those values are used to initialize the parameters.
- Because Python uses pass by value, the value of an argument is unchanged by a function invocation; that is, the value of an argument is the same after the invocation as it was before the invocation.
- Implication: not possible to write a swapping function in Python
- Because a parameter has the same value as its argument, if that argument is a list, the contents of that list can be modified. Such a change is called a side effect of the function. Functions with side effects generally have a return value of None.
print()statement in a function is not supplying a return value. It is sending a message to the program user. Do not include a
print()statement in a function body unlessly told to print/display information to the program user
input()statement is wrong to include in a function body, unless explicitly told to do so. No function you will be asked to write should have an input statement