Functions & Built in Functions Flashcards

1
Q

Higher Order Functions

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

methods

(def, how to call, function to find information)

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

lambda function example and standard function equivilent

lambda used with higher oder functions

(3 answers)

A

( 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

lambda as an anonymous function

A

lambda x, y: x + y

_(1, 2) returns 3

the single underscore (_) is bound to the last expression evaluated by the interpretor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

function general syntax

(how to set a default value)

(setting variable types & dynamic type)

A

( 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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
A
  • %%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( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

while loop generic example

A

while some_boolean_condition:

do something

else:

do something different

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

useful functions:

range( ), enumerate( ), zip( ), min( )/max( ), input( )

A

( 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

useful libraries

random

2 functions: shuffle( ) and randint( )

A

( 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

return multiple variables in python

A

use they comma in between return variables in

ex. return even_list, odd_list @ end of func.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

function annotation

(ex. and def.)

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly