Writing Functions Flashcards
Python Data Science Toolbox (Part 1)
Print function
print()
Convert to string
str()
Creating a function
def func_name(par1, par2, default=val): """doc strings""" function body
Check the type of an object
type()
Parts of a function
- header
- parameter
- docstrings
- body
function(x) - what is x called when calling a function? when creating a function?
calling = argument creating = parameter
function that gives back a value
use “return”
what is a tuple?
immutable object type: (a, b, c)
creating a larger scope in a function
line before the variable: say “global var” where var is the variable
how to explore the python module that comes with python
import builtins
dir(builtins)
benefits of an inner function and name of this
nested function: 1. avoid writing repetitions within a function
2. closure - nested functions remember state of enclosing scope
explain a nested function
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
alter variable in enclosing scope
“nonlocal var”
function with variable-length arguments
use *args
functions with variable-length keyword arguments
use **kwargs (a variable length dictionary)