Methods and Functions Flashcards
A _______ in python is somewhat similar to a function, except it is associated with object/classes. _______ in python are very similar to functions except for two major differences. The _______ is implicitly used for an object for which it is called. The ______ is accessible to data that is contained within the class.
methods.
These allow us to create blocks of code that can be easily executed many times, without needing to constantly rewrite the entire block of code.
Functions.
name_of_function is in what type of casing?
Snake casing. All lowecase with underscores between words.
Is there a default option for functions in case the user does not input something for the function to do it’s work?
Yes.
def name_of_function(variable = default option),
default option is whatever you want it to be.
Typically we use the _____ keyword to send back the result of the function, instead of just printing it out. _____ allows us to assign the output of the function to a new variable.
return
When debugging it is useful to use both the ______ and _____ to see what is going on internally.
return, print.
Notice due to the dynamic functionality of python, we are needed to do what in the functions?
To clarify the data types of the arguments in the functions.
>> sum_numbers(10,20)
<< 30
>> sum_numbers(‘10’, ‘20’)
<< ‘1020’
What must the function return inside the filter function to be able to run correctly?
The function must return a boolean.
The _______ function applies a given function to each item of an iterable (list, tuple etc.). This can be used to vary the argument that a specific function is suppose to handle,
ex. function(‘string’)
x = [‘string1’, ‘string2’, ‘string3’]
This method is used to have x and function work together.
map()
The ________ function returns an iterator were the items are filtered through a function to test if the item is accepted or not.
filter()
Both map() and filter() will return what if left as
>> filter(function,iterable)
and/or
>> map(function,iterable)?
A code of stored memory, it must be inside of list() to have filter/map() return a list of what the function inside of filter/map() is to return.
_______________________ are utilized to construct anonymous functions (one time used functions).
lambda expressions (or lambda forms)
def square(num): result = num\*\*2 return result
Convert the above code into a lambda expression.
Converting the square function into a lambda expression:
- Simplicity
>> def square(num):
return num**2
- One line
>> def square(num): return num**2
- One time use so we do not need the function build.
>> lambda num: num**2
Typically we won’t be assigning a variable to lambda expressions. We instead will be using this in conjunction with what two functions?.
map and filter.
>> x = 25
>> def printer():
x=50
return x
>> print(x)
What will be outputted?
25