Python Tutorial: Fun with functions

Learn how functions transform inputs into outputs using matplotlib and pandas. Call functions with positional and keyword arguments, use the label keyword for legends, and plot letter frequency data from a CSV file.
Chapters
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. A function is an action. It turns one or more inputs into an output. In this example, the function is called turnorange. The input is a shape, such as a blue square, and the action is to color the input shape orange. Consider the following code snippet. In a previous lesson, we learned that this code would read some data and produce a graph. In this snippet, there are three functions. PD READCSV turns a CSV file into a table. In Python, Plot Plot turns data into a line. Plot and Plot show displays the plot in a new window. Let's learn about functions by examining one of our code snippets. This function takes the data from the table in the bottom left. Plot and plots letter index on the x axis and frequency on the y axis, resulting in the graph on the bottom right. First, let's look at the function name. The function name has two parts. The first part tells us what module the function comes from. In this case, it's from plot, which was the alias we used when we imported matplotlib. The second part, which comes after the period, is the name of the function plot. After the name of the function comes a set of parentheses. The inputs to the function will all come inside of these parentheses. Positional arguments are one type of input that a function can have. Positional arguments must come in a specific order. In this case, the first argument is the x value of each point and, 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 good practice to put a space after the comma, but your code will run even if you forget that space. 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. Before you start practicing, let's discuss 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 parenthesis at the end of your function while you write code. Syntax highlighting in the script editor will help remind you to close your parentheses. Let's practice what you've learned by making a few graph.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







