Writing Functions Flashcards

Python Data Science Toolbox (Part 1)

1
Q

Print function

A

print()

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

Convert to string

A

str()

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

Creating a function

A
def func_name(par1, par2, default=val):
"""doc strings"""
function body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Check the type of an object

A

type()

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

Parts of a function

A
  1. header
  2. parameter
  3. docstrings
  4. body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

function(x) - what is x called when calling a function? when creating a function?

A
calling = argument
creating = parameter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

function that gives back a value

A

use “return”

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

what is a tuple?

A

immutable object type: (a, b, c)

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

creating a larger scope in a function

A

line before the variable: say “global var” where var is the variable

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

how to explore the python module that comes with python

A

import builtins

dir(builtins)

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

benefits of an inner function and name of this

A

nested function: 1. avoid writing repetitions within a function
2. closure - nested functions remember state of enclosing scope

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

explain a nested function

A

outer function asks for an input which is used in the inner function and as a result returns a specific variant of the inner function

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

alter variable in enclosing scope

A

“nonlocal var”

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

function with variable-length arguments

A

use *args

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

functions with variable-length keyword arguments

A

use **kwargs (a variable length dictionary)

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

lambda function format

A

= (lambda vars: function)

17
Q

best use case for lambda functions

A

functions anonymously embedded in larger expressions

18
Q

functions to use lambda

A

map(), filter(), reduce()

19
Q

map function

A

map(function, list) - applies the function over the list

20
Q

filter function

A

filter(function, list) - filters out elements that don’t satisfy a criteria

21
Q

reduce function

A

from functools import reduce

reduce(function, list) - produces a single object from a list

22
Q

basic error handling framework

A

try:
except:

23
Q

another way to handle errors

A
raise
if xyz:
raise ValueError('message')