03 Conditionals Part 1

Posted on Jun 15, 2026

In this module we will revisit the bool data type by discussing conditionals. For now we’ll look at the kinds of comparisons using numbers that we know from math. We’ll look at how to write comparative statements or conditions and how these conditions allow us to change how our scripts are executed.

Numerical Comparisons

In the variables module we briefly mentioned the boolean or bool type that could only have one of two values True or False. Now, occasionally we will have data that can be effectively represented with such a variable but more often we will only implicitly use bools that we make on the fly using comparisons.

Let’s look at a simple comparison and how python interprets it.

# conditionals.py
my_comparison = 5 > 7
my_other_comparison = 5 < 7

print(f"Python sees the first comparison as: {my_comparison}")
print(f"Python sees the second comparison as: {my_other_comparison}")
uv run conditionals.py
Python sees the first comparison as: False
Python sees the second comparison as: True

In the variables module, we said that bool variables could be either True or False and we see those values appear here without us knowingly defining a bool. Python knows to do this because in both of the examples above we used comparison operators in the variable definitions. The ones above—> (greater than) and < less than—should be familiar from math. Our first comparison could be read as “5 greater than 7” and the second “5 less than 7”. Of course looked at this way it should be obvious that the former is false while the later is true. Usually we will be doing this with variables and we might not know the answer ahead of time and thats where this automatic conversion of comparisons to bool values can be useful.

Before we move on let’s look at more of the comparisons that should be familiar from math as these come up quite often any time we’re working with numerical data.

Python operatorMath OperatorDescription
==$=$Is equal (note difference from =)
!=$\neq$Is not equal
>, <$\gt$, $\lt$Greater than, less than
>=, <=$\ge$, $\le$Greater than or equal to, less than or equal to

We will surely make this mistake regardless but its worth hammering on a bit. When we define a variable we use = in a statement like x = 7 to state something like “set $x$ equal to 7”. When we do comparisons we use == in a statement like x == 7 to ask something like “is $x$ equal to 7”?.

Conditionals

The real power of comparisons comes from the ability for us to control the flow of our scripts based on the values we’re working with. To achieve this we’ll use conditional statements. Conditional statements consist of one of a number of special keywords if, elif, and else followed by a comparison and an indented block of code. There must always be an if statement if there are to be others so we will start with that.

an if statement looks like this:

if some_comparison:

    execute
    indented
    code

If the comparison evaluates to True than the indented code gets executed line by line. If the comparison evaluates to False than the code inside is skipped. Python knows which code is “inside” of the if based on the indentation. Indented code after a line starting with if is part of the conditional and we can return to the normal flow of our code by removing indentation from lines we want always to run.

Let’s replace our script with some new code. We’ll write a little program that sends a message only if a number, electoral_votes, is above the threshold needed to win the US presidential election, 270. We’ll use functions to seperate some of the logic of our program from our focus on the conditional statements.

# conditionals.py
def election_won(votes: int):

    # 538 is total electoral votes as made famous by stats douche nate silver's website
    # note we can use parenthesis just like we would in math PEMDAS, etc.
    vote_percentage = (votes / 538) * 100

    print(f"We got {vote_percentage} % of the vote. We did it, Joe!")

electoral_votes = 306
election_threshold = 270

if electoral_votes > election_threshold:
    election_won(electoral_votes)

print("Wow, what an election, huh?")
uv run conditionals.py
We got 56.877323420074354 % of the vote. We did it, Joe!
Wow, what an election, huh?

First, we’ve defined a function to build and print the message that we’ve called election_won(). This function takes a single integer argument called votes. Inside of the function we calculate a percentage based on the number passed in and the total number of possible electoral votes and print a nice little message.

Next we defined some variables that we’ll use in the conditional statement. These should be fairly self evident.

Then comes the interesting part, an if statement with a comparison. electoral_votes >= election_threshold. This says that if electoral_votes is greater than or equal to our threshold of 270 than Python should execute the indented code. Here the indented code is just a single line calling our election_won() function.

We end with another print that follows the if block but is not indented which tells Python that it doesn’t belong to the if block. Its just regular old line-by-line code and will be run after Python checks the conditional regadless of the conditional itself.

When we run the script with our initial electoral_votes value 0f 306, than electoral_votes >= election_threshold and we see our nice little message.

Let’s change the value from 306 to something below the threshold, say, 226, and see what happens.

# conditionals.py
def election_won(votes: int):

    # 538 is total electoral votes as made famous by stats douche nate silver's website
    # note we can use parenthesis just like we would in math PEMDAS, etc.
    vote_percentage = (votes / 538) * 100

    print(f"We got {vote_percentage} % of the vote. We did it, Joe!")

electoral_votes = 226
election_threshold = 270

if electoral_votes >= election_threshold:
    election_won(electoral_votes)

print("Wow, what an election, huh?")
uv run conditionals.py
Wow, what an election, huh?

When we reduce the value of electoral_votes to something below the threshold set in election_threshold than our comparison will evaluate to False and the code inside of the if block will be skipped entirely. Indeed, all we see as output is the message at the end that we expect to run regardless of the conditional stuff because it is not indented.

else

Now the thing is, our program would be more complete if we have a message for when the electoral_votes are below the threshold. We could accomplish this by adding some code inside of another if block—something like if electoral_votes < election_threshold:—but really we want winning and losing to be explicitly mutually exclusive. Here is where else comes in.

else allows us to write code that gets executed only if a corresponding if statement is not executed. If the comparison in the if line evaluates to False than the code in the else block is executed instead. Let’s look at a schematic example before we go to the real thing.

if some_condition:
    execute_indented_code   # executes if some_condition == True
else:
    execute_different_code  # e `xecutes if some_condition == False

An if paired with an else allows us to ensure that something is always executes based on the conditional. We can use an else block to send a nice conciliatory message when the election is lost.

We’ll update our script with another function that we’ll call inside a new else block that we’ll add as well.

# conditionals.py
def election_won(votes: int):

    # 538 is total electoral votes as made famous by stats douche nate silver's website
    # note we can use parenthesis just like we would in math PEMDAS, etc.
    vote_percentage = (votes / 538) * 100

    print(f"We got {vote_percentage} % of the vote. We did it, Joe!")


electoral_votes = 226
election_threshold = 270

if electoral_votes > election_threshold:
    election_won(electoral_votes)

else:
    print("rip bozo lol acab includes prosecutors shit were so cooked")

print("Wow, what an election, huh?")
uv run conditionals.py
rip bozo lol acab includes prosecutors shit were so cooked
Wow, what an election, huh?

Now we have an else block that runs some code in the case that the comparison in the if is False. If we leave our electoral_votes value below the threshold we see our nice little message. To prove the point we should run the script again changing our electoral_votes value back above the threshold. If we change the line where we set the value back to electoral_votes = 306 than we should see this once more:

uv run conditionals.py
We got 56.877323420074354 % of the vote. We did it, Joe!
Wow, what an election, huh?

elif

There is still one situation that we should feel uncomfortable about and thats the case where the election is a tie. If a candidate recieves 269 electoral votes, than the other candidate does as well. Thinking about our script as it stands, the program would display the “lose” message with 269 votes and that’s not really right.

While if runs code if the condition is true, and else runs code if the if doesn’t run, elif is a sort of combination of the two and it goes between ifs and elses.

Like else, elif only gets looked at if the preceeding if doesn’t run. Unlike else, elif also has a comparison to check before it runs. Let’s look at some more pseudocode so we can talk through the logic concretely.

if some_comparison:
    execute_indented_code  # if some_comparison == True
elif some_other_comparison:
    execute_another_branch_of_code  # if some_comparison == False but some_other_comparison == True
else:
    execute_catchall_branch  # if both some_comparison and some_other_comparison are False

So here’s how it works. When Python first encounters these lines it checks whether the comparison in the if line is True or False. If its True than the code inside the if is executed and both the elif and else sections are skipped.

However, if the first condition is False, before running the stuff inside else, Python checks the comparison for elif. If that condition is met, Python will execute the code indented under the elif line and skip the else.

Finally, if the conditions described in both if and elif are False than we run the stuff under else.

Its worth noting that a conditional block will always have an if, may have zero or one else, but can have any number of elifs each checked in the order they are written. This way we can have an arbitrarily large number of mutually exclusive branches our code can follow.

Let’s use an elif statement to make a branch for ties in our little electoral votes script.

# conditionals.py
def election_won(votes: int):
    vote_percentage = (votes / 538) * 100
    print(f"We got {vote_percentage} % of the vote. We did it, Joe!")


electoral_votes = 269  # set this to the tie value to see elif branch
election_threshold = 270

if electoral_votes > election_threshold:
    election_won(electoral_votes)

elif electoral_votes == election_threshold - 1:
    print("Well somehow its a tie. Guess we don't have a president")

else:
    print("rip bozo lol acab includes prosecutors shit were so cooked")

print("Wow, what an election, huh?")
uv run conditionals.py
Well somehow its a tie. Guess we don't have a president
Wow, what an election, huh?

Summary

In this module we learned how to write programs that can follow different branching paths depending on the value of some variable. We saw how comparisons get converted to boolean True or False values automatically behind the scenes and how we can use standard mathematical operators to compare numbers.

In the next module, we will look deeper at conditionals to look at how we can work with strings and combine conditions into complex conditional statements.