The starting point for Python beginners interested in fantasy football. Get started by analyzing catch rates for Michael Thomas, Julio Jones, and Davante Adams. Learn how to set up Python for Fantasy Football analysis and the very basics of Python programming.
If you have any questions about the code here, feel free to reach out to me on Twitter or on Reddit.
This post is going to be a little different – this is going to be an introduction to Python with Fantasy Football/NFL for absolute beginners (People who have never coded before). It’s pretty much going to be an excerpt (shameless plug incoming) to a course on learning Python for Fantasy Football Analysis.
If you like Fantasy Football and have an interest in learning how to code, check out our Ultimate Guide on Learning Python with Fantasy Football Online Course. Here is a link to purchase for 15% off. The course includes 15 chapters of material, 14 hours of video, hundreds of data sets, lifetime updates, and a Slack channel invite to join the Fantasy Football with Python community.
For those of you that don’t know, Python is a programming language that is really popular for data analysis. If
you get good at Python, you can do a lot of the same fantasy football stuff you can do in excel, but way more in
depth and way faster.
Also, if you have any questions about the post, just message me through our discord or email me at
[email protected].
And finally, if you stumbled upon this post but are already relatively proficient in Python, then check out my
first post on How to Set Up Python for Fantasy Football Analysis with Pandas, Seaborn, and MatplotLib. This post
here is meant for absolute beginners.
If this is your first time coding, I advise you type in every line of code from the source code and make it run
on your own computer. After you’ve done that, come back here to find out how it works. Don’t just read the
source code and this post and think you’ve learned anything. You learn programming by typing in code, making it
break, and expanding on it.
Anyway, let’s get in to actually learning how to code with Python
The header image is literally the entire source code for this project. This post is going to cover some basic
data types, for loops, and functions. If you don’t know what any of that is, that’s cool. My hope is that you
have a general understanding of what all these things mean and why they’re useful by the end of all this, as
we’ll be going over all of them in detail.
Usually, to get Python running on your machine, you’d need to download Python, download something called a text
editor, and learn how to use the terminal.
Instead, I’m going to show you a way to get Python running on your computer in like 5 seconds.
Go over to Google Colab, and hit “New Notebook”
And you’re good to go!
What we’re going to be doing is writing Python code in these little cells, and then we can “run” the code by
hitting Shift+Enter.
In this first post, we’ll be calculating the catch rate for Michael Thomas, Julio Jones, and Davante Adams and
using Python to output that for us in a human-readable format.
Let’s run through the code line by line.
Input this into the first cell in Google Colab and hit Shift+Enter. Make sure all the brackets, curly braces,
and commas are there. These are all important as in Python you need to write things a certain way or else you
get something called a SyntaxError. More on that later.
Nothing should output because all we are doing is assigning a variable to some data.
Side note, these are actual stats for 2019 that I got from profootballreference. Looking back on it now, Michael
Thomas’s catch rate is actually insane. For comparison, during Marvin Harrison’s record year in 2002 where he
broke the single-season reception record (which MT broke this year), he had 202 targets. If Michael Thomas had
202 targets in 2019 and maintained his catch rate of about 80%, he would had about 163 catches on the season –
20 more than Marvin Harrison’s 143 receptions in 2002.
Back to learning Python!
So programming is all about moving around and manipulating data, mostly. Now, it’s really easy to move around
data when you can save it somewhere and then reference it later.
This is what variables are for. We can set variables to some type of data and then reference that variable later
when we want to move around or manipulate that data.
That’s what we’re doing here with the players variable. We could have named our variable anything (or almost
anything. There’s certain rules on how you can name variables), and we set it equal to a list data type.
A list is pretty much what it sounds like – a list of Python “objects”. Lists are enclosed by square brackets on
both sides. Python objects that go inside lists can either be integers, floats (don’t worry about this for now),
dictionaries, strings (a sentence or bunch of ASCII characters enclosed by double or single quotes), lists
themselves (Yes, you can have a list of lists), and like, way, way more. Moreover, each of these items is
separated by a comma. Again, if you don’t put square brackets or commas in their right place, you get a
SyntaxError.
If this all sounds confusing – it’s okay, over time you learn to think in terms of these “objects”
Here are some examples of Python lists.
That’s a lot of case scenarios, but I just wanted to give you an idea of how flexible lists can be. You don’t
need to understand all of this, but try to wrap your head around some of it. You can see that for
my_list_four we were able to reference my_list_one, my_list_two,
my_list_three. This is one of the powers of using variables, you can reference them later in your
code (which we’ll also be doing today).
In my list_five, the last object in our list is what’s know as a dictionary. A dictionary consists of key, value
pairs. Dictionaries are enclosed by curly braces. Each key: value pair is separated by a comma just like in
lists. So for example, {‘catches’: 100, ‘targets’: 120}. A dictionary is a useful way of organizing
a whole column of a players data.
I can literally write for hours on lists and different ways to organize them but this is all you really need to
know for right now. If you can absorb all this information, you’re ready to move on to the next part.
Let’s take a look at our list, then. You can probably tell by now that we have a list of dictionaries. Each
dictionary has information on an NFL wide receiver – in this case – Julio Jones, Michael Thomas, and Davante
Adams (Just recently learned it’s davante, not devante. Oops).
The information for each player is stored in a dictionary, with information on their name, catches, and number
of targets.
One more thing about lists – they are what’s called iterable (In a way that dictionaries are not).
Iterable means we can run a sequence of tasks ACROSS the list.
We do this with something called a for loop.
A for loop basically tells Python – for every object in the list, run this task on that object.
Now that we’ve gone over the list of dictionaries, let’s go over the for loop that will run a task
for each item in the list (each player dictionary)
This is the next block of code that comes after our list, so let’s run through this step by step.
Note that the block of code underneath the line for player in players: has to be indented by using tab. Python
is pretty strict on this, so if you don’t do this right you’ll get a SyntaxError again.
We are essentially saying in the first line that for each “player” (our dictionary object) in our players list,
run the following code on that player object in the indented block below. Each player object will be a Python
dictionary, so we have to run code that manipulates and pulls data from a dictionary.
Now, remember when I told you guys about dictionaries and how they are a collection of key, value pairs?
Well dictionaries are really useful when we want to pull out that value by referencing the key. You can think of
our dictionary sort of as a database or excel table in this case scenario that has a bunch of data organized in
two columns (Ever used VLOOKUP in excel?).
Here, for each player, we pull out the name value by using player[‘name’] to get our player name.
This is the syntax we use to pull values from a dictionary. I’ve alluded to syntax throughout this post by
mentioning SyntaxErrors. Syntax is essentially a language’s requirements and conventions for how to
do a certain task or write a certain line of code. The syntax to pull a value from a Python dictionary is
dictionary_name[‘key_name’].
Remember, the placeholder player is a Python dictionary. We then save our player’s name to a variable we call
name so we can reference it later in the code.
We then do the same thing with catches and targets, except this time, after we save our
player[‘catches’] and player[‘targets’] to variables we call catches and targets,
respectively, we then reference both variables afterwards as well.
We declare a new variable, which we call catch_rate, to catches divided by targets. Both the values we extract
from the keys catches and targets, are both data types known as integers, so we are allowed to do this. We can
multiply integers together, divide them, and do all sort of basic math stuff in Python.
Then, in the final line of our for loop, we use something called a function (twice). Python has a number of
“built-in” functions, meaning functions we can use that Python just provides for us. You sometimes have to
create functions yourself. This is something we’ll be doing in another post and in a lot more detail in the
book.
A function takes an input and returns an output. We are using two functions here. Sort of like functions in
Excel or high school algebra. There’s one built-in function called print, which takes in a Python
object and outputs it back to us below. That’s literally all it does. We’ll be using the print function to tell
Python to output each player’s catch rate in a human-readable way.
The other built-in function we’re using is called str, which takes a Python object and converts to
a string.
Remember, a string is a collection of characters enclosed by double quotes or single quotes. Below are some
examples of strings.
Again, just showing you how flexible strings can be. You don’t need to understand all of this right now.
You can actually combine strings together by adding them, like in string_five, and it’ll come out
as a single string all put together. The only thing is, that all the objects you are concatenating (that’s the
official word for it) must all be strings. This is where the str function becomes handy. Our catch
rate is a number (A float, to be specific, but don’t worry about that), but if we want to output a string and
concatenate them together, all the objects we are concatenating need to be strings. So we use the
str function to be able add all the items together.
And that’s it! Run the code using Shift+Enter and see what outputs. You should see an output
identical to the one in the header image.
This is basic stuff, but hopefully you can already start to see the power of programming. It’s actually possible
to have a collection of thousands of rows of data like this and be able to “iterate” through them to find the
information we just found in seconds. This is exactly what we do in my more advanced Python for Fantasy Football
Series.
If you made it this far, you’re awesome. Hopefully you were able to gain something valuable from this post. If
you have any questions, leave a comment below or PM me on reddit. I’m always glad to help.
My suggestion on what to do next is to play around with the code, make it break, do some independent research,
and maybe expand upon it. Below is a link to the official Python tutorial you may want to check out if this is
your first time using Python (Don’t be discouraged if you don’t understand everything in the links below. I
still don’t understand everything in the Python documentation 7 years later).
Python documentation
Part Two of the Beginner Series
If you liked this post and want to receive updates on my upcoming posts and book, make sure to subscribe to our
newsletter!