Data Science Toolbox Flashcards
How do you define a function?
def functionname ():
expression
expression
functionanme() will output expression
how you do define a function with a parameter?
def fxnname(parameter):
expression
expression
e.g.
def square(value):
new_value=value**2
print(new_value)
This will square any argument (value) you input in the fxn square()
Define a fxn with a parameter, but instead of printing the value, return it
def square(value):
new_value = value ** 2
return new_value
Now can use fxn to assign the result to a new variable, e.g. num = square(4)
What are docstrings? 4 facts
- Describe what your function does
- Serve as documentation for your function
- Placed in the immediate line after the function header
- In between triple double quotes “””
e.g.
def square(value):
”"”Return the square of a value”””
new_value = value**2
return new_value
When you assign a variable to a function that prints a value but does not return a value, what type of value will the variable be?
e.g. y = print(x)
NoneType
How do you define a function with more than one parameter?
def fxnname(parameter1, parameter2)
expressions
e.g.
def raise_to_power(value1, value2)
new_value = value1 ** value2
return new_value
What can you use to make functions return multiple values?
Facts about the answer to above
Tuples
e.g. even_nums = (2, 4, 6)
- Similar to list, except can’t modify values (immutable), and constructed using parenthesis
- Can unpack tuples into several variables:
a, b, c = even_nums
print(a) will output 2, print(b) will output 4, print(c) will output 6
- Can access tuple elements just like lists (zero-indexing)
second_num = even_nums[1]
print(second_num) will output 4
Use a tuple to return multiple values. Complete the code below
def raise_both(value1, value2)
’'’Raise value1 to the power of value2 and vice versa.”””
new_value1 = value1 ** value2
new_value2 = value2 ** value 1
def raise_both(value1, value2)
’'’Raise value1 to the power of value2 and vice versa.”””
new_value1 = value1 ** value2
new_value2 = value2 ** value 1
new_tuple = (new_value1, new_value2)
return new_tuple
Define Scope
Name and define 3 types of scope
Scope is the part of the program where an object or name may be accessible
3 Types:
- Global Scope: defined in the main body of a script
- Local scope: defined inside a function. Once execution of fxn is done any name inside the function ceases to exist, so can’t access those names outside the function definition
- Built-in scope: names in the pre-defined built-ins module, e.g. print()
new_val = 10
def square(value):
”"”Returns the square of a number”””
new_value2 = new_val ** 2
return new_value2
new_val = 20
square(3)
What will be the output?
400
The global value accessed is the one at the time the function is called–not the value when the function is defined.
How do you alter the value of a global name within a function call?
using global variable
new_val = 10
def square(value):
”"”Returns square of a number.”””
global new_val
new_val = new_val ** 2
return new_val
new_val will now output 100
*If you don’t use global, it would output 10
How do you print the names in the module builtins?
import builtins
dir(builtins)
def raise_val(n):
”"”Return the inner function.”””
def inner(x):
”"”Raise x to the power of n.”””
raised = x ** n
return raised
return inner
square = raise_val(2)
square(2)
What will be the output?
4
The program created a function “square” that squares any number.
Similarly, can do cube = raise_val(3) to create a function that cubes any number, so cube(4) will return 64
How do you change names in an enclosing scope in a nested function?
use nonlocal variable
def outer():
n = 1
def inner():
nonlocal n
n = 2
print (n)
inner()
print(n)
Now, outer() will output 2
In which order are scopes searched?
Local scope, Enclosing functions, Global, Built-in
“LEGB rule”
Add a default argument of 1 for pow in the following code
def power(number, pow)
”"”Raise number to the power of pow.”””
new_value - number ** pow
return new_value
After modifying the code, what would the following output?
power(9, 2)
power(9, 1)
power(9)
def power(number, pow=1)
”"”Raise number to the power of pow.”””
new_value - number ** pow
return new_value
power(9, 2): 81
power(9, 1): 9
power(9): 9
How do you add flexible arguments to a function?
Flexible arguments allows you to pass any number of arguments to the function
Use *args
e.g.
def add_all(*args)
”"”Sum all values in *args together.”””
sum_all = 0
for num in args:
sum_all += num
return sum_all
*args creates a tuple with all the arguments then the for loop iterates over them