Conditionals

Now that we know how to write Boolean expressions, we can use them inside conditional statements to tell our programs to conditionally execute one block of code versus another.

Conditional statements

In Python, conditional statements always start with if. Here's the basic template:

if <condition>:
    <statement>
    <statement>
    ...

If <condition> is true, then the program will execute the statements indented below it. The indentation of the inner statements is required in Python, and indenting incorrectly will result in either a syntax error or a buggy program.

Here's a simple if statement that re-assigns a variable based on a condition:

clothing = "shirt"

if temperature < 32:
    clothing = "jacket"

🧠 Check Your Understanding

In which situations will clothing be assigned to "jacket"?

Compound conditionals

A conditional statement can include any number of elif statements to check other conditions and execute different blocks of code. Other languages use else if, but Python decided that was just too dang long and shortened it to elif. 🤷🏼‍♀️

Here's a template with three conditional paths:

if >condition<:
    <statement>
    ...
elif <condition>:
    <statement>
    ...
elif <condition>:
    <statement>
    ...

Here's an extension of the earlier example with one more path:

clothing = "shirt"

if temperature < 0:
    clothing = "snowsuit"
elif temperature < 32:
    clothing = "jacket"

One way to make sure we understand a conditional is to make a table showing possible values of program state and the result.

For example:

temperature

clothing

-50

"snowsuit"

-1

"snowsuit"

0

"jacket"

1

"jacket"

31

"jacket"

32

"shirt"

50

"shirt"

It's a good idea to be doubly sure of what happens at the boundary conditions- sometimes you might realize, "uh oh! I wanted >=, not >, my bad!". 🙀When writing tests for a function that uses a conditional function, make sure to test those boundary conditions.

Finally, a conditional statement can include an else clause to specify what code should be executed if none of the preceding conditions were true.

if <condition>:
    <statement>
    ...
elif <condition>:
    <statement>
    ...
else <condition>:
    <statement>
    ...

Here's a way to rewrite the earlier example using if/elif/else:

if temperature < 0:
    clothing = "snowsuit"
elif temperature < 32:
    clothing = "jacket"
else:
    clothing = "shirt"

That code would result in the same values for clothing as shown in the table below, but reads more clearly.

In summary, a conditional statement:

Using conditionals in functions

A conditional statement could be just one part of a longer function, or it could be the entire function. It's common to write a function that uses a conditional to compare the input parameters and return values based on what was passed in.

For example:

def get_number_sign(num):
    if num < 0:
        sign = "negative"
    elif num > 0:
        sign = "positive"
    else:
        sign = "neutral"
    return sign

We'd call that function like this:

get_number_sign(50)  # Returns: "positive"
get_number_sign(-1)  # Returns: "negative"
get_number_sign(0)   # Returns: "neutral"

We could also end each branch of the conditional with a return, rewriting the above function like so:

def get_number_sign(num):
    if num < 0:
        return "negative"
    elif num > 0:
        return "positive"
    else:
        return "neutral"

That makes it very clear that the function won't be doing anything else besides returning that value, since a return statement exits a function immediately.

There's one more way we might write this function, leaving off the else of the conditional:

def get_number_sign(num):
    if num < 0:
        return "negative"
    elif num > 0:
        return "positive"
    return "neutral"

When that function receives a num of 0, it doesn't execute either of the return statements inside the conditional, since neither condition is true. It continues on, discovers the next statement is a return statement, and executes that statement instead.

➡️ Next up: Exercise: Conditionals