Recap

So far we have covered two-thirds of the most basic tools needed to program in Python: data types and functions. Check out our last post here if you missed out. In this post, we take a look at conditionals to round out our trio. This will set us up with all of the basics needed to dive into some real bonafide fantasy football analysis on our next post.

Conditionals

Conditionals are a fundamental concept in programming that allow you to control the flow of your code. They allow you to make decisions based on certain conditions and to execute different lines of code depending on whether those conditions are met. In this post, we are going to look at the following components of conditional statements:

  • If/Else Statements
  • Comparison Operators
  • Logical Operators
  • Elif
  • If/Else Statements

    Basic conditonals make use of logical "If/Else" statements and are a powerful part of any programmers tool belt. An if/else block follows the basic format of "if this statement here on this first line is true, then run the block of code in the indented lines below, if it's not true, then run the block of code in the indented block below else".

    Comparison Operators

    Comparison operators compare two values and return a Boolean value (either True or False) depending on the outcome of the comparison. For example, the > operator checks if the value on the left is greater than the value on the right, while the <= operator checks if the value on the left is less than or equal to the value on the right. Some common comparison operators include:

  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • == (equal to)
  • <= (less than or equal to)
  • != (not equal to)
  • Note that the "equal to" operator consists of TWO equal signs. Rember the "=" symbol is used to assign a value to a variable.

    Here is a simple example:

    a = 1 
    b = 1
    
    if a == b: 
        print("a is equal to b")
    else: 
        print("a is not equal to b")
    a is equal to b
    

    As you can see, a and b have the same value. Therefore, our conditional statement evaluates to True and prints the first line of code.

    a = 1 
    b = 2
    
    if a == b: 
        print("a is equal to b")
    else: 
        print("a is not equal to b")
    a is not equal to b
    

    As you probably guessed, since a and b are not equal, the conditional statement evaluates to False and the line under our else statement runs. Next, let's move on to the next concept: Logical Operators.

    Logical Operators

    Logical operators are used to combine multiple conditions in a single statement. The most commonly used logical operators are and , or , and not . The and operator requires every part of the conditional statement to be true for the entire conditional statement to evaluate to True . The or operator requires only one part of the condiitonal statement to be true for the entire statement to evaluate to True . The not operator is a little weird and requires the conditional statement (you guessed it) to NOT be true for the conditional statement to evaluate to True. Here is an example using the and operator:

    a = 1 
    b = 2
    c = 3
    if b > a and c > a : 
        print("a is the smallest value")
    else: 
        print("a is not the smallest value")
    a is the smallest value
    

    Notice here that we combined two conditional statements: a > b and c > a . The and operator allows us to do this. Here is an example of how you would use the not operator, if you are curious:

    a = 1 
    b = 2
    c = 3
    if not c > a : 
        print("a is larger than c")
    else: 
        print("a is not larger than c")
    a is not larger than c
    

    Here, a is less than c , therefore the statement If not c > a evaluates to False because c is greater than a . So, the else statement is run and the program prints "a is not larger than c".

    Elif

    The elif statement is a way for us to add multiple If/else statements into our code in a concise way. Basically, if the first condition is not met the statement following elif will be checked and if that is met, the code below is run. If the second condition is not met, you can either add another elif statement and check another condition or use the else statement to account for all other cases.

    a = 1 
    b = 2
    c = 1
    if c == a and b == a and b == c : 
        print("all values are equal")
    elif b == a or c == a or b == c : 
        print ("some of the values are equal")
    else: 
        print("none of the balues are equal")
    some of the values are equal
    

    Here, we used the elif statement to add another if/else to our code. "Elif" is short for "Else If" and you can think of it as a way for us to chain multiple conditional statements together in one conditional statements. The first if statement evaluates to False because all three values are not equal. Next, the elif block runs. This time, at least one pair of the variables are equivalent (which satisfies the requirements of the or operator), so this elif statement evaluates to True and the code below runs.

    Challenge: See if you can modify this code by adding more elif statements so this conditional tells us specifically what values are equivalent.

    Check out the python documentation for more information on how you can control the flow of your code. Also, be sure to consult the full documentation on python operators as well because we did not cover all of them.

    If you are like me, everything makes more sense when put into fantasy football terms. So let's check out an example that helps us compare the NFL's top receivers.

    name = "Tyreek Hill"
    receiving_yards = 1710
    
    if receiving_yards >= 1000:
        print(f"{name} is a top receiver.")
    Tyreek Hill is a top receiver.
    

    In this example,if tells the computer that whatever we write next will be a condition that needs to be met for the code underneath to run. Next, we establish that the receving yards variable is part of that condition by using the >= comparison operator. This one stands for "greater than or equal to" and gives the computer the condition that receving_yards must be greater than or equal to 1000 . If that condition is met, the : colon symbol is used to represent the word "then" and the code beneath the if statement is run. So, in a sense we are "teaching" the computer with this if-statement that any player that has 1,000 receivng yards or more in an NFL season can be considered a top receiver. In this example, Tyreek Hill had more than 1,000 yards receiving, and our conditional statement evaluates to True and the program prints "Tyreek Hill is a top receiver".

    Example Two

    What happens if the condition is not met, such as receiving_yards not being greater than 1000? To explore this scenario, let's introduce a receiver that didn't quite measure up.

    name = "Kenny Golladay"
    receiving_yards = 81
    
    if receiving_yards >= 1000:
        print(f"{name} is a top receiver.")
    else: 
        print(f"{name} is not a top receiver.")
    Kenny Golladay is not a top receiver.
    

    Kenny Gollday's cap hit this year was 21.4 million dollars. With 81 receiving yards, that means the Giants paid roughly $264,197 per yard. Woof.

    Anyway, We added an else statement to our conditional that states that if the first condition is not met, then a different line of code needs to run. In this case, Kenny G did not have greater than or equal to 1,000 yards in 2022, so the second print function executes instead of the first.

    Let's pull everything together and apply multiple conditionals to multiple players at the same time. To do this we will need to use our fancy new conditional statements, a function, a for loop, and a dict . If you get confused here, refer back to our older posts!

    #establish a dictionary for 2022 player stats
    players = [
        {
        "name": "Tyreek Hill",
        "receiving_yards": 1710,
        "touchdowns": 7
        },
        {
        "name": "Kenny Golladay",
        "receiving_yards": 81,
        "touchdowns": 1
        },
        {
        "name": "George Pickens",
        "receiving_yards": 801,
        "touchdowns": 4
        }
    ]
    #define a function to rate receivers
    def receiver_rater(name, receiving_yards, touchdowns):
    #print "is a top receiver for players with greater than or equal to 1000 yards AND greater than or equal to 7 touchdowns"
        if receiving_yards >= 1000 and touchdowns >= 7:
            print(f"{name} is a top receiver.")
    #print "is an above average receiver for players with greater than or equal to 800 yards OR more than 4 touchdowns"
        elif receiving_yards >= 800 or touchdowns > 4:
            print(f"{name} is an above average receiver.")
    #print "is not a top receiver for all other players that do not meet the first two conditions"
        else:
            print(f"{name} is not a top receiver.")
    
    #loop through the dictionary and apply the receiver rater function to each player in the dictionary
    for player in players: 
        receiver_rater(name=player['name'],receiving_yards=player['receiving_yards'],touchdowns=player['touchdowns'])
    Tyreek Hill is a top receiver.
    Kenny Golladay is not a top receiver.
    George Pickens is an above average receiver.
    

    To recap, we established a dictionary of three different receivers. The key : value pairs here represent the player names, their receivng yards, and touchdowns for the 2022 season. Next, we created a function called receiver_rater that takes the name , receiving_yards , and touchdowns as arguments and prints the appropriate statement based on our conditional statements. Finally, We use a for loop to iterate through our dict (for player in players) and apply the function to each player in the dictionary. Let's take a closer look at the conditional statement within our function.

        if receiving_yards >= 1000 and touchdowns >= 7:
            print(f"{name} is a top receiver.")

    In the first line of our conditional we use the and operator to combine the receiving yard and touchdown conditions. So, if a player has more than 1,000 receivng yards AND greater than or equal to 7 touchdowns, then that player is a top receiver and the print function below runs using that players name in the print statement.

        elif receiving_yards >= 800 or touchdowns > 4:
            print(f"{name} is an above average receiver.")

    Our second condition is "if receiving yards are greater than or equal to 800 OR touchdowns are greater than 4 then run the code below". Here, the or logical operator is used and is less "strict" than the and operator. In this case, only one of the two conditions in our elif statement need to be met for the second print function to run. If we wanted to be more strict in our definition of an above average wide receiver we could certainly use an and operator here instead of the or operator. Remember, you can use as many elif statements as you want to establish as many different conditions as you want.

        else:
            print(f"{name} is not a top receiver.")

    Finally, we again use the else statement to catch all other scenarios when the if and elif conditions are not met. For this simplified example, if you are not a "top receiver" or an "above average receiver", then you are simply "not a top receiver". I'd feel bad for Kenny here but he made 21 million dollars to chill on the sidelines this year. So good for him, I guess.

    Challenge: add Josh Palmer to the dictionary using his 2022 stats from pro-football-reference and add another elif statement to the function that would define him as an "average receiver".

    Concluding Thoughts

    In today's post we:

  • Learned the basic concepts of conditional statements in python
  • Defined the meaning and purpose of logical and comparison operators
  • used if/elif/else statements to define top receivers in the league
  • Now that you understand data types, functions, and conditionals we can start analyzing the 2022 season for actionable takeaways. We will do our first "real" analysis in the next post!

    Thanks for reading - You guys are all awesome and have fun coding!