Python Function
Python Function
Python Function
Familiar functions
Out of the box, Python offers a bunch of built-in functions to make your life as a
data scientist easier. You already know two such functions: print() and type().
You've also used the functions str(), int(), bool() and float() to switch between
data types. These are built-in functions as well.
Calling a function is easy. To get the type of 3.0 and store the output as a new
variable, result, you can use the following:
result = type(3.0)
The general recipe for calling functions and saving the result to a variable is
thus:
output = function_name(input)
Instructions
Use print() in combination with type() to print out the type of var1.
Use len() to get the length of the list var1. Wrap it in a print() call to directly
print it out.
Use int() to convert var2 to an integer. Store the output as out2.
# Create variables var1 and var2
var1 = [1, 2, 3, 4]
var2 = True
Help!
Maybe you already know the name of a Python function, but you still have to figure
out how to use it. Ironically, you have to ask for information about a function
with another function: help(). In IPython specifically, you can also use ? before
the function name.
To get help on the max() function, for example, you can use one of these calls:
help(max)
?max
Multiple arguments
In the previous exercise, the square brackets around imag in the documentation
showed us that the imag argument is optional. But Python also uses a different way
to tell users about arguments being optional.
You'll see that sorted() takes three arguments: iterable, key and reverse.
key=None means that if you don't specify the key argument, it will be None.
reverse=False means that if you don't specify the reverse argument, it will be
False.
In this exercise, you'll only have to specify iterable and reverse, not key. The
first input you pass to sorted() will be matched to the iterable argument, but what
about the second input? To tell Python you want to specify reverse without changing
anything about key, you can use =:
Note: For now, we can understand an iterable as being any collection of objects,
e.g. a List.
Instructions
Use + to merge the contents of first and second into a new list: full.
Call sorted() on full and specify the reverse argument to be True. Save the sorted
list as full_sorted.
Finish off by printing out full_sorted.
# Create lists first and second
first = [11.25, 18.0, 20.0]
second = [10.75, 9.50]
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: