Class 32 — April 6
Functioning
Part two is ending — Soon time to show mastery — Expect to do well
Look both ways
Agenda
- Review
Update (if you did not do so last class)
- Module url.py
- Now provides ability to get a web-based dictionary
Homework solutions
- HW 23: aid.py
- HW 22: quad.py
- HW 22: magnificent.py
- HW 21: take_two.py
- HW 19: olio.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
List patterns
- To analyze a list use
result = ...
for element in the_list :
...
return result
- To modify a list use
n = len( the_list )
for i in range( 0, n ) :
element = the_list[ i ]
...
the_list[ i ] = element
- Important list operations
the_list.append( v )
- Appends
v
to end ofthe_list
the_list.count( v )
- Returns the number of occurrences of
v
inthe_list
the_list.remove( v )
- Removes first occurence of
v
fromthe_list
the_list.index( v )
- Index of first occurrence of
v
inthe_list
list3 = list1 + list2
- Concatenates
list1
andlist2
to makelist3
Functions
- 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
return
statement is reached in a function, the function stops executing its instructions; that is, the function immediately stop its execution.
- A
return
statement can optionally have a return expression following the keywordreturn
. 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 returnsNone
.
- 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.
Warnings
- A
print()
statement in a function is not supplying a return value. It is sending a message to the program user. Do not include aprint()
statement in a function body unlessly told to print/display information to the program user
- An
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
Past Q & A
Warning: fopen(/l/cs1112/public_html/defs/htm/181-trailer): Failed to open stream: No such file or directory in /l/cs1112/public_html/defs/php/read.php on line 5
Unable to open file!