In part two of the beginner series, we continue to build on the Python basics and introduce conditional statements. All at the same time analyze rushing stats for Mark Ingram, Aaron Jones, and Derrick Henry.
If you have any questions about the code here, feel free to reach out to me on Twitter or on Reddit.
In this part of the free beginner series we are going to be expanding upon our code in the last section and going to briefly be covering a concept called built-in type methods. This will be a brief introduction to the concept and if you want to learn more about it, definitely check out our course on Learning Python with Fantasy Football.
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.
Anyway, let's move on with the code. If you're haven't read part one of the beginner series, here's a link for that.
Here's the source code for this project. You'll see that now - we are calculating yards per carry instead of catch rates. But the basic format of the code is the same. The only thing that is changing here is the way in which we extract out information from our dictionary.
So again, the code is more or less the same as last time, but with some minor changes. This is how we'll be doing things in this series. Taking the same piece of code and making it better and better with each iteration.
We have a list of dictionaries. This is possible in Python because all a list is is a collection of Python "objects" and a dictionary is an object. Remember also that we can iterate through lists, and we do this using a for loop.
Remember also that last time we used the syntax value = my_dict['key'] to extract out values from our dictionary. There's a problem with this way of extracting data from our dictionary, though. If the key is not in the dictionary, we get back what's called a KeyError, stopping our "script" or code dead in it's tracks.
There's multiple ways we can handle this. We can check if the key is in the dictionary using what's called an if block, and then if it is, we extract out our value. That's one way but that's not the way we do things here.
Here, instead we use what's called a method. A method is essentially an operation that we can "call" off a Python object using a dot notation. The notation goes like this - our_object.our_method(arg1, arg2, arg3)
Inside our method, you'll see that I put in there arg1, arg2, arg3, that's shorthand for arguments. We pass in arguments to our method to give it additional information about what we watn our method to do.
You can see here that we are using a method called get to extract out a value from our dictionary. get takes in two arguments. The first argument is the key we want to extract, the second argument is the default value we want to return if the key we passed in as our first argument was not found in our dictionary. This is the great thing about this get method. We can safely extract values from our dictionary without worrying about getting back an error if the key is not in the dictionary. We pass in a value of None with a capital N to tell Python we'd like a value of None, which is a special word in Python that means return nothing.
We repeat this process three times to find our name, rushing_att, and rushing_yds values.
In line 19, we use an if statement to check if our values were all caught by the get method. If any of our values are None, the code underneath the if statement will not run. I'm being short here because I'm going to let you guys figure out how this if block works on your own for now! We'll be covering if statements and conditionals in the next part of the beginner series in depth.
In the last line of our code, we use the print function like we did last time to print out our yards per carry number in a human readable fashion much like we did last time with our catch rate numbers.
Except this time, the code only runs if the if block evaluates to True. That's all you need to know for now, as the topic of the next installation of this series will be all about if blocks. I urge you to do some looking through the Python documentation or on Google to figure out how this if block works before I release the next installment. Doing independent research is the best way to learn this stuff.
Anyway, run the code in your Google Colab cell and take a look at the output! Remember, you run the code using Shift+Enter.
Thanks for reading, you guys are awesome!