Functions & Built in Functions Flashcards
Higher Order Functions
A function that either:
- takes one or more functions as arguments
- returns a function as a result
def higher_ord ( f, *args ) :
result = f ( *args )
return result
methods
(def, how to call, function to find information)
- a function that is avaiable to a given object because of the object type; a function that belongs to an object
- .method_name( )
- help( object.method)
lambda function example and standard function equivilent
lambda used with higher oder functions
(3 answers)
( 1 )
add_one = lambda x: x +1
( 2 )
def add_one( x ):
return x + 1
add_one( 2 ) returns 3 in both
( 3 )
high_ord = lambda x, func: x + func( x )
high_ord(2, lambda x: x * x)
lambda as an anonymous function
lambda x, y: x + y
_(1, 2) returns 3
the single underscore (_) is bound to the last expression evaluated by the interpretor
function general syntax
(how to set a default value)
(setting variable types & dynamic type)
( 1 )
def name_of_function( num1, num2 ):
”””
Docstring explaining the function
”””
do something
return ( num1 + num2 )
( 2 )
def name_of_function( var = ‘some text’) :
= ‘some text’ in the function inputs is the default value
( 3 )
python is a dynamically typed prog. lang., so you cannot define input types in function call
work around: define var types in function body
def add(x, y):
x = int(x)
y = int(y)
return x + y
even when text inputed in function call, will convert to interger values
Working with Files in Python
- write a text file in python line of code
- open the text file
- print working directory
- read the file
- .readlines( )
- with open syntax
- %%writefile myfile.txt
- myfile = open(myfile.txt)
- pwd
- .read( )
- returns a list with each line as an element
- with open(filename, mode) as var_name:
- do something
- ex: contents = var_name.read( )
while loop generic example
while some_boolean_condition:
do something
else:
do something different
useful functions:
range( ), enumerate( ), zip( ), min( )/max( ), input( )
( 1 )
- range( ) returns a sequence of numbers
for num in range (0, 11, 2):
print(num)
returns even numbers
( 2 )
- enumerate( )
word = ‘abcde’
for item in enumerate(word):
print(item)
return tuple of index and value ex. (0, ‘a’)
can also unpack using tuple unpacking
( 3 )
mylist1 = [1,2,3]
mylist2 = [‘a’,’b’,’c’]
from item in zip(mylist1, mylist2):
print(item)
returns both list items as a tuple
( 1 , ‘ a ‘ ) ( 2 , ‘ b ‘ ) ( 3 , ‘ c ‘ )
( 4 )
min( ) / max( ) # returns the min and max values in a function
( 5 )
ask for user input
accepts anything as a string, must transform/cast it to different data type
useful libraries
random
2 functions: shuffle( ) and randint( )
( 1 )
shuffle( ) # randomly shuffles items in a list
mylist = [1, 2, 3, 4]
shuffle(mylist) # returns random order of values
( 2 )
randint( ) # retruns a random integer, similar to a seed
return multiple variables in python
use they comma in between return variables in
ex. return even_list, odd_list @ end of func.
function annotation
(ex. and def.)
example
def split_comma(line: str) → str :
pass
definition
- this is a variable type hint
- indicating that only string variables can be passed to the function
- this can be used with outher variables types as well
another example
def h_index(citations: List[int]) → int:
pass
- indicates that a list object on integers should be passed to the function