Methods and Functions Flashcards

1
Q

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.

A

methods.

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

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.

A

Functions.

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

name_of_function is in what type of casing?

A

Snake casing. All lowecase with underscores between words.

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

Is there a default option for functions in case the user does not input something for the function to do it’s work?

A

Yes.

def name_of_function(variable = default option),

default option is whatever you want it to be.

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

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.

A

return

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

When debugging it is useful to use both the ______ and _____ to see what is going on internally.

A

return, print.

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

Notice due to the dynamic functionality of python, we are needed to do what in the functions?

A

To clarify the data types of the arguments in the functions.

>> sum_numbers(10,20)

<< 30

>> sum_numbers(‘10’, ‘20’)

<< ‘1020’

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

What must the function return inside the filter function to be able to run correctly?

A

The function must return a boolean.

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

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.

A

map()

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

The ________ function returns an iterator were the items are filtered through a function to test if the item is accepted or not.

A

filter()

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

Both map() and filter() will return what if left as

>> filter(function,iterable)

and/or

>> map(function,iterable)?

A

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.

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

_______________________ are utilized to construct anonymous functions (one time used functions).

A

lambda expressions (or lambda forms)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
def square(num):
 result = num\*\*2
 return result

Convert the above code into a lambda expression.

A

Converting the square function into a lambda expression:

  1. Simplicity

>> def square(num):

return num**2

  1. One line

>> def square(num): return num**2

  1. One time use so we do not need the function build.

>> lambda num: num**2

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

Typically we won’t be assigning a variable to lambda expressions. We instead will be using this in conjunction with what two functions?.

A

map and filter.

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

>> x = 25

>> def printer():
x=50
return x

>> print(x)

What will be outputted?

A

25

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

>> x = 25

>> def printer():
x=50
return x

>> print(printer())

What will be outputted?

A

50

17
Q

This is used inside of functions with n inputs. In this case n is arbitrary to fit whatever the user needs it to be.

A

my_function(*args)

Note: print(args) gives back a tuple.

18
Q

This is used inside of functions with n keyworded arguments . In this case n is arbitrary to fit whatever the user needs it to be.

A

my_function(**kwargs)

print(kwargs) gives us a dictionary.

19
Q

Input function can cause disrupt the functionality of following codes because it awaits for user interaction.

Due to two situations starting from not inputting anything initially,

  1. moving on to the next cell of codes.
  2. Shift-Enter the input function twice.

Give the solution to both situations.

A
  1. Solution: Answer the input.
  2. Solution: Kernel –> Restart
20
Q

A function attached to an object is called a _______. (Non-function things attached to an object, such as imag, are called _______).

“attached” simply means attached by python’s dot syntax.

A

method; attributes.

method examples: len(), max(), min(), …

attributes examples: image, ..

>>> variable.name_of_

A variable stored in an instance or class is called an attribute. A function stored in an instance or class is called a method.