Functions
Extracting min and max value from a dataset
min_year = min(data, key=lambda x: x[0])[0]
max_year = max(data, key=lambda x: x[0])[0]a_list = [1, 20, 10.5, 4, 0]
print(max(a_list))
print(min(a_list))Creating Our Own Functions
This is how we can create the square() function:
def square(a_number):
squared_number = a_number * a_number
return squared_numberStarted with the
defstatement, where we did the following:Specified the name of the function (
square)Specified the name of the variable (
a_number) that will serve as inputSurrounded the input variable
a_numberwith parenthesesEnded the line of code with a colon (
:)
Example
Our dataset is a collection of iOS app information organized in a list of lists. Each sublist represents a row, and each element within that sublist corresponds to a column. For instance, here is how a row might look:
To facilitate our data analysis, we need to perform two key operations:
Extract the values of the column in a separate list
Generate a frequency table for the elements of that list
We'll create separate functions for each of these tasks to maintain modularity and improve code reuse. In this screen, we'll focus on the data extraction function.
To extract the values from any column we want from our apps_data dataset, we must complete the following:
Create an empty list that will store the values extracted from the desired column.
Loop through the
apps_datadataset (excluding the header row), and for each iteration, do the following:Store the value from the column we want in a variable.
Append that value to the empty list we created outside the for loop.
Write a function named
extract()that can extract any column you want from theapps_datadataset.The function should take in the index number of a column as input (name the parameter whatever you want)
Inside the function's definition, complete the following:
Create an empty list
Loop through the
apps_datadataset (excluding the header).Extract only the value you want by using the parameter (which will be an index number)
Append that value to the empty list
Return the list containing the values of the column
Use the
extract()function to extract the values in theprime_genrecolumn and store them in a variable namedgenres. The index number of this column is11.
Remember that we want to create two functions:
A function that extracts the values for any column we want in a separate list (we already created this function — it's the
extract()function)A function that generates a frequency table for a list
To create a frequency table for the elements of a list, we need to do the following:
Create an empty dictionary.
Loop through that list and check for each iteration where the iteration variable exists as a key in the dictionary created.
If it exists, then increment by
1the dictionary value at that keyIf it doesn't exist, create a new key-value pair in the dictionary, where the dictionary key is the iteration variable, and the dictionary value is
1
Write a function named
freq_table()that generates a frequency table for any list.The function should take in a list as input.
Inside the function's body, write code that generates a frequency table for that list and stores the table in a dictionary.
Return the frequency table as a dictionary.
Use the
freq_table()function on thegenreslist (already defined from the previous screen) to generate the frequency table for theprime_genrecolumn and store the frequency table to a variable namedgenres_ft.
Alternate Way
We can also do it in a single function.
Multiple Parameters functions example
Keyword and Positional Arguments
There's more than one way to pass in these two arguments and get the result we want. These are all valid.
When we use the syntax subtract(a=10, b=7) or subtract(b=7, a=10), we pass in the arguments 10 and 7 using the variable names a and b. For this reason, we call them named arguments, or more commonly, keyword arguments.
When we use keyword arguments, the order we use to pass in the arguments doesn't make any difference. In our example, Python knows that the argument 10 corresponds to the parameter a, and the argument 7 to the parameter b, regardless of whether we use subtract(a=10, b=7) or subtract(b=7, a=10).
Default Arguments
When we create a function, we can initiate parameters with certain default values — we call these values default arguments. In the function below, we initiate the constant parameter with a default argument of 10:
Above, we can see we created the add_value() function with two parameters: x and constant. When we used the function, however, we only passed in one positional argument: 3. Judging by the definition and the output of the function, we can deduce that add_value() used the argument 10 for the parameter constant.
When we initiate parameters with default arguments, passing arguments to those parameters when we call the function becomes optional. If we don't pass in an argument, Python uses the default argument. However, if a parameter doesn't have a default argument, we must pass in an argument — otherwise we'll get an error.
Default arguments are easy to modify when we call a function:
Returning Multiple Variables
Python allows us to build functions that return more than one variable. This means that we can create a function that returns a sum and a difference.
Above, we passed 15 and 5 as arguments to the sum_and_difference() function. The function returned (20, 10), where 20 is the sum, and 10 is the difference. The order of the returned values matches the order of the variables in the return statement.
The structure of the output (20, 10). (20, 10) is a tuple.
No Return Statement.
Functions without a return statement don't return any value. Strictly speaking, they return a None value which represents the absence of a value. The None value is an instance of the NoneType data type.
Variable Scope
When a variable is accessed from within a function, Python first searches in the local scope (inside that function's definition) to see if the variable is defined there. If it doesn't find it there, it doesn't cause an error; rather, it continues to search in the global scope (the scope of the main program).
Last updated