BASIC FUNCTIONS
FUNCTIONS
HOW TO WRITE and
CALL/INVOKE A FUNCTION
def
is_even( i ):
"""
Input: i:int, a positive number
Returns True if i is even, otherwise False """
print("inside is_even")
return i%2 == 0
is_even(3)
IN THE FUNCTION BODY
def is_even( i ): """
Input: i:int, a positive int
Returns True if i is even, otherwise False """
print("inside | is_even") | ||
return | i%2 | == | 0 |
VARIABLE SCOPE
actual parameter when function is called
def f( x ):
x = x + 1
print('in f(x): x =', x) return x
x = | 3 | | |
z = | f( | X | ) |
Variables with the same apparent name can reference different object in different places
Locality and persistence or lack thereof
VARIABLE SCOPE
Global scope
f
x
z
Some
code
3
4
def f( x ):
x = x + 1
print('in f(x): x =', x) return x
x = 3
z = f( x )
ONE WARNING IF NO
return STATEMENT
def is_even( i ):
"""
Input: i, a positive int Does not return anything """
i%2 == 0
EXERCISE: FIBONACCI NUMBER
MODULES
import xyz
xyz.stuff = 23
from xyz import *
def foo():
global stuff;
stuff = 23
DICTIONARIES
A PYTHON DICTIONARY
my_dict = {}
grades = {'Ana':'B', 'John':'A+', 'Denise':'A', 'Katy':'A'}
Key 1
Key 2
Key 3
…
Val 1
Val 2
Val 3
…
key1 val1 key2 val2
key3 val3 key4 val4
'Ana' | 'B' |
'Denise' | 'A' |
'John' | 'A+' |
'Katy' | 'A' |
Also explain how to search by value
DICTIONARY LOOKUP
'Ana' | 'B' |
'Denise' | 'A' |
'John' | 'A+' |
'Katy' | 'A' |
grades = {'Ana':'B', 'John':'A+', 'Denise':'A', 'Katy':'A'}
grades['John'] grades['Sylvan']
⇾ evaluates to 'A+'
⇾ gives a KeyError
grades = {'Ana':'B', 'John':'A+', 'Denise':'A', 'Katy':'A'}
grades['Sylvan'] = 'A'
⇾ returns True
⇾ returns False
'John' in grades 'Daniel' in grades
del(grades['Ana'])
DICTIONARY OPERATIONS | 'Ana' | 'B' |
'Denise' | 'A' | |
'John' | 'A+' | |
| 'Katy' | 'A' |
'Sylvan' | 'A' |
grades = {'Ana':'B', 'John':'A+', 'Denise':'A', 'Katy':'A'}
grades.keys()
⇾ returns ['Denise','Katy','John','Ana']
grades.values() ⇾ returns ['A', 'A', 'A+', 'B']
DICTIONARY OPERATIONS | 'Ana' | 'B' |
'Denise' | 'A' | |
'John' | 'A+' | |
| 'Katy' | 'A' |
Fix to make l=grades.keys() so a list not a tuple!!!!
Add grades.items…
DICTIONARY KEYS and VALUES
d = {4:{1:0}, (1,3):"twelve", 'const':[3.14,2.7,8.44]}
list
vs
dict
immutable type
CREATING A DICTIONARY
def lyrics_to_frequencies(lyrics): myDict = {}
for word in lyrics:
if word in myDict: myDict[word] += 1
else:
myDict[word] = 1 return myDict
USING THE DICTIONARY
def most_common_words(freqs): values = freqs.values() best = max(values)
words = []
for k in freqs:
if freqs[k] == best: words.append(k)
return (words, best)
LEVERAGING DICTIONARY PROPERTIES
def words_often(freqs, minTimes): result = []
done = False while not done:
temp = most_common_words(freqs) if temp[1] >= minTimes:
result.append(temp) for w in temp[0]:
del(freqs[w])
else:
done = True return result
print(words_often(beatles, 5))
EXERCISE: CHARACTER COUNTING
Module Questionnaire
Support: NIH NIBIB-U24EB028887, NIGMS-R01GM122424, NSF-188553, NSF-186890, NSF-1720625, NIGMS-R01GM076692, NIGMS-R01GM077138
Please take a minute or two to let us know about your experience with this module by filling out the brief zoom survey
Feel free to provide additional comments and suggestions in the slack or by email to us as well (haydenfennell@gmail.com)