Python Functions

Previously, we learned about variables which allow us to store information for later use, strings which represent text, and floats which represent numbers. Now that we know the basic data types in python, we'll start using them with functions.

What is a Function?

A Function is an Action.

It turns one or more input into an output. Consider the following code snip.

import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv('letter_frequency.csv')

plt.plot(df.letter_index, df.frequency, label = 'Ransom')

plt.show()

This code will read some data and produce a graph. In this snippet there are three functions:

  • pd.read_csv() turns a csv file into a table
  • plt.plot() turns data into a line plot
  • plt.show() displays the plot in a new window

Let's learn about functions by examining one from our code snippets.

plt.plot(df.letter_index, df.frequency, label = 'Ransom')

This function takes the data from the table and plot letter_index on the x-axis and frequency on the y-axis.

Anatomy of a Function

Function Name

The function name plt.plot() has two parts:

  • Starts with the module that the function comes from plt
  • Followed by the name of the function after the period plot
  • Function name is always followed by parentheses ()
  • The input of the function will all come inside of the parentheses

Positional Arguments

df.letter_index, df.frequency

Positional Arguments are one type of input that a function can have. Positional Argument must come in a specific order. In this case, the first argument is the x-value of each point, and the second argument is the y-value of each point. Each argument is separated by a comma. If you forget the comma, you will get a syntax error. It's a good practice to put a space after the comma, but your code will run even if you forget that space.

  • Positional Arguments are inputs to a function; they tell the function how to do its job
  • Order matters

Keyword Arguments

label = 'Ransom'

Keyword Arguments come after Positional Arguments, but if there are multiple keyword arguments, they can come in any order. In this case the keyword argument is called "label". After the equals sign, we've put the actual input to the function, which is "Ransom". Eventually, this argument will let us create a legend for our graph.

  • Must come after Positional Arguments
  • Start with the name of the argument label, then an equal sign =
  • Followed by the argument Ransom

Common Function Errors

There are two common errors for functions. If you get a syntax error, you might be missing commas between each argument. Both positional and keyword arguments need to be separated by commas. Alternatively, you might be missing a parentheses at the end of your function.

  • Missing commas between arguments
  • Missing closed parentheses

Code Examples

Load a DataFrame

A ransom note was left at the scene of a Dog kidnapping. Eventually, we'll want to analyze the frequency with which each letter occurs in the note, to help us identify the kidnapper. For now, we just need to load the data from ransom.csv into Python.

We'll load the data into a DataFrame, a special data type from the pandas module. It represents spreadsheet-like data (something with rows and columns).

We can create a DataFrame from a CSV (comma-separated value) file by using the function pd.read_csv.

# Import pandas
import pandas as pd

# Load the 'ransom.csv' into a DataFrame
r = pd.read_csv('ransom.csv')

# Display DataFrame
print(r)

Snooping for suspects

We need to narrow down the list of suspects for the kidnapping of Bayes. Once we have a list of suspects, we'll ask them for writing samples and compare them to the ransom note.

A witness to the crime noticed a green truck leaving the scene of the crime whose license plate began with 'FRQ'. We'll use this information to search for some suspects.

As a detective, you have access to a special function called lookup_plate.

lookup_plate accepts one positional argument: A string representing a license plate.

# Define plate to represent a plate beginning with FRQ
# Use * to represent the missing four letters
plate = 'FRQ****'

# Call the function lookup_plate()
lookup_plate(plate)

# Call lookup_plate() with the keyword argument for color
lookup_plate(plate, color = "Green")