Syntheses of past Q & A
Miscellaneous
- Nested for-loops usually wrong unless using a list-of-lists; e.g., a dataset
- Two lists? probably do not want one list inside another.
- Name of func with no return value? -> there is no general name in Python (they return
None
)
- Do not use
input()
in a function unless explicitly told to do so
Argument versus parameter
- Defining: e.g., in
def sqrt( x )
variablex
is the parameter
- Parameters are variables defined in the parentheses of a function definition
- Using/Invoking: e.g., in
math.sqrt( 3 )
literal3
is the argument
Parts of function
- First line: keyword
def
, then function name, then(
, then parameter list, then) :
- After first line:
- Indented
- The body of the function definition, which is a statement list
return
: a special statement in the body of the function that both ) gives a value and stops the function
Local variable
- Variable that is defined inside a function
- Parameters are local variables that are initialized at the start of a function by copying the values of arguments
- Local variables only exist inside the function definition (limited scope)
When do functions have error versus None
as output?
- To get
None
:
- Do not have a
return
statement
- To have a
return
with no value after it
- To get an error:
- Do something illegal inside the function
print() versus return
print:()
display, output -- shows up in the console, not in the invoking python code
return
: produce, hand-back, give -- shows up in the invoking python code, not in the console
- We usually want
return
, notprint()
(unless we ask for print/display/output)
- Consider the following:
def f( x ) :
print( x )
This function displays the value of x and returns
None
- Consider the following:
def g( x ) :
return x
This function does no display and returns the value of its parameter
How can a function change the contents of argument memory?
- Examples
def h ( my_list, my_dictionary ) :
my_list[ index ] = ...
my_list.remove( ... )
my_list.append( ... )
my_list.pop( ... )
my_list.sort()
my_dictionary[ key ] = value
- If we do not tell you to modify a parameter, don’t
- This means do not use the above statements on the parameter
- Does that mean we can
def f( x ) :
y = x
y.pop()
No! The assignment of
x
toy
means modifyingy
affectsx
- Does that me we can
def f( x ) :
y = x[ : ] # or y = list( x ) or y = x.copy()
y.pop()
Yes! The assignment to
y
makes it a copy ofx
and notx
itself
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
Consider
def g( x ) :
x = [ 1112 ]
x = [3,1,4]
g( x )
Neither
x
nor its contents have changed
- If we run
def g( x ) :
x = [ 1112 ]
return x
x = [3,1,4]
y = x
x = g( x )
Then because of the return,
x
has changed; i.e., it points to a different list
Also,
y
has not changed
- If we run
def g( x ) :
x = [ 1112 ]
x = [ 3, 1, 4 ]
y = x
x = g( x )
Then
x
is None
Also,
y
has not changed
Invocation (examples)
- if you see
identifier(
and it is not after adef
, it is an invocation
len( x )
math.sqrt( 4 )
sorted( x )
math.sqrt( len( x ) )
f( x )
f()
def f(): # not an invocation
return 3 # not an invocation
g = f # not an invocation (and probably a typo)
g = f() # an invocation
s = 'hi there' # not an invocation
s.lower # not an invocation
s.lower() # an invocation, but does not change anything...
s = s.lower() # an invocation, and probably what you meant to write
Invocation versus argument
- Invocation uses a function to do work
- Argument is the information passed into the invocation
math.sqrt( 4 )
is an invocation of functionmath.sqrt()
, with argument4
While loops examples
x = 3
while ( x > 0 ) :
x = x - 1
text = input( 'Type the third positive integer: ' )
while ( text != '3' ) :
text = input( 'Type the third positive integer: ' )
while ( 2 < 3 ) :
print( 'this will print again and again forever' )
while 2 > 3:
print( 'this will never be printed' )
- Can we use
while
in a function?
- Yes.
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 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 (change find key for value 18)
found = None
for key in d.keys():
value = d[ key ]
if value == 18:
found = key
© 2019 Jim Cohoon | Resources from previous semesters are available. |