6 - Methods and Functions Flashcards
What are the methods for a List? (There are eight of them.)
Fortunately, with iPython and the Jupyter Notebook we can quickly see all the possible methods using the tab key. The methods for a list are:
append
count
extend
insert
pop
remove
reverse
sort
How do you add elements to the end of a list?
Create a simple list
lst = [1,2,3,4,5]
lst.append(6)
lst
[1, 2, 3, 4, 5, 6]
How do you determine the number of times a specific item occurs in a List?
Check how many times 2 shows up in the list
Great! Now how about count()? The count() method will count the number of occurrences of an element in a list.
lst = [1, 2, 3, 4, 5, 6]
lst.count(2)
2 occurs once
With Python, how do you get more information on a specific method?
You can always use Shift+Tab in the Jupyter Notebook to get more help about the method. In general Python you can use the help() function:
help(lst.count)
Help on built-in function count:
count(…) method of builtins.list instance
L.count(value) -> integer – return number of occurrences of value
What is a function?
Formally, a function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions.
On a more fundamental level, functions allow us to not have to repeatedly write the same code again and again.
Why even use functions?
Put simply, you should use functions when you plan on using a block of code multiple times. The function will allow you to call the same block of code without having to write it multiple times. This in turn will allow you to create more complex Python scripts. To really understand this though, we should actually write our own functions!
What is the first keyword needed to create a function?
def
def name_of_function(arg1,arg2):
‘’’
This is where the function’s Document String (docstring) goes.
When you call help() on your function it will be printed out.
‘’’
# Do stuff here
# Return desired result
How do you call a function?
Use the function name + ()
say_hello()
What happens if you try to call a function without the () ?
If you forget the parenthesis (), it will simply display the fact that say_hello is a function. Later on we will learn we can actually pass in functions into other functions! But for now, simply remember to call functions with ().
say_hello
How do have a function return a value?
Use the ‘return’ keyword.
def add_num(num1,num2):
return num1+num2
Why does this not work?
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a “hit” on an even number, we return True
if number % 2 == 0:
return True
else:
return False
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a “hit” on an even number, we return True
if number % 2 == 0:
return True
# This is WRONG! This returns False at the very first odd number!
# It doesn’t end up checking the other numbers in the list!
else:
return False
** Correct Approach: We need to initiate a return False AFTER running through the entire loop**
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a “hit” on an even number, we return True
if number % 2 == 0:
return True
# Don’t do anything if its not even
else:
pass
# Notice the indentation! This ensures we run through the entire for loop
return False
How do you unpack Tuples?
stock_prices = [(‘AAPL’, 200),(‘GOOG’, 300),(‘MSFT’, 400)]
for stock,price in stock_prices:
print(price)
200
300
400
What does the map() function do?
To get the results, either iterate through map()
def square(num):
return num**2
my_nums = [1,2,3,4,5]
# or just cast to a list
list(map(square, my_nums))
[1, 4, 9, 16, 25]
What is the filter() function?
The filter function returns an iterator yielding those items of iterable for which function(item) is true. Meaning you need to filter by a function that returns either True or False. Then passing that into filter (along with your iterable) and you will get back only the results that would return True when passed to the function.
What does the filter() look like?
def check_even(num):
return num % 2 == 0
nums = [0,1,2,3,4,5,6,7,8,9,10]
list(filter(check_even,nums))
[0, 2, 4, 6, 8, 10]