What is a function?

Introduction to Data Science in Python

Hillary Green-Lerman

Lead Data Scientist, Looker

A function is an action

function-analogy

Introduction to Data Science in Python

Functions in code

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()

Functions perform actions:

  • pd.read_csv() turns a csv file into a table in Python
  • plt.plot() turns data into a line plot
  • plt.show() displays plot in a new window
Introduction to Data Science in Python

Introduction to Data Science in Python

Anatomy of a function: function name

function-anatomy

Function Name:

  • Starts with the module that the function "lives" in (plt)
  • Followed by the name of the function (plot)
  • Function name is always followed by parentheses ()
Introduction to Data Science in Python

Anatomy of a function: positional arguments

function-anatomy

Positional Arguments:

  • These are inputs to a function; they tell the function how to do its job
  • Order matters!
Introduction to Data Science in Python

Anatomy of a function: keyword arguments

function-anatomy

Keyword Arguments:

  • Must come after positional arguments
  • Start with the name of the argument (label), then an equals sign (=)
  • Followed by the argument (Ransom)
Introduction to Data Science in Python

Common function errors

  • Missing commas between arguments

  • Missing closed parenthesis

Introduction to Data Science in Python

Let's practice!

Introduction to Data Science in Python

Preparing Video For Download...