Conditional Statements

In many situations, actions and results depends on a specific set of conditions being satisfied. For example, individuals in randomized controlled trials receive the treatment if they have been assigned to the treatment group. A gambler makes money if she wins her bet.

In this section we will learn how to describe such situations using code. A conditional statement is a multi-line statement that allows Python to choose among different alternatives based on the truth value of an expression. While conditional statements can appear anywhere, they appear most often within the body of a function in order to express alternative behavior depending on argument values.

A conditional statement always begins with an if header, which is a single line followed by an indented body. The body is only executed if the expression directly following if (called the if expression) evaluates to a true value. If the if expression evaluates to a false value, then the body of the if is skipped.

Let us start defining a function that returns the string 'Positive' for a positive number:

def classify(x):
    result = 'Not sure yet'
    if x > 0:
        result = 'Positive'
    return result

Let us work through what Python will do when we evaluate classify(3).

  1. Evaluate classify to the computer representation (CR) of the function;

  2. Evaluate 3 to give the CR of the integer 3

  3. Call the CR of the function, with the CR of int 3. So:

    1. Python enters function world

    2. The new variable x gets the value CR of int 3.

    3. Python executes the function body

All this is revision from functions.

What happens in the function body, with x = 3?

  1. Python evaluates the assignment statement result = 'Not sure yet'. The result variable has the value: CR of “Not sure yet”.

  2. Python evaluates the if expression x > 0. The result is the CR of True. This is a true value, so Python will execute the body of the if statement.

  3. The body of the if statement is the single line result = 'Positive'. Python reassigns the result variable to have the value: CR of “Positive”.

  4. return result first evaluates result to get the CR of “Positive”, then exits function world, returning this value.

classify(3)
'Positive'

The classify function returns 'Positive' if the input is a positive number, like 3. But if the input is not a positive number, such as -3, then this happens, in the function body:

  1. x has the value CR of int -3.

  2. result gets the value: CR of “Not sure yet”. So far this is the same as for the case of x equal to 3 above.

  3. x > 0 evaluates to False, so Python skips the body of the if statement.

  4. return result first evaluates result to get the CR of “Not sure yet”, (because this hasn’t changed) then exits function world, returning this value.

classify(-3)
'Not sure yet'

Let us refine our function to return Not positive if the input is a negative number. We can do this by adding an else clause. If the if expression evaluates to a true value, then Python executes the first clause, after the if expression, as before. If it evaluates to a false value, Python executes the clause following the else, instead.

def classify(x):
    result = 'Not sure yet'
    if x > 0:
        result = 'Positive'
    else:
        result = 'Not positive'
    return result

Imagine we evaluate classify(-3) again. Python enters function world, and sets x to have the value CR of int -3. It checks x > 0 and finds a false value, so it skips the first clause, and executes the clause following else:. This returns 'Not positive'.

Notice that the if statement now deals with all possible values of x. Whatever the value of x we with either execute result = 'Positive' or we will execute result = 'Not positive'. We can now get rid of the first line in the function result = 'Not sure yet' because we will always change result from that value, in the if statement:

# Classify without the unnecessary first assignment.
def classify(x):
    if x > 0:
        result = 'Positive'
    else:
        result = 'Not positive'
    return result

Now imagine we prefer our function to return 'Negative for negative values and Zero if the input value is 0. It seems like we need three clauses, one each for positive, negative and 0 values.

We can do this by adding an elif clause, where elif is Python’s shorthand for the phrase “else if”.

def classify(x):
    if x > 0:
        result = 'Positive'
    elif x == 0:
        result = 'Zero'
    else:
        result = 'Negative'
    return result

Now classify returns the correct answer when the input is -3, 0, or 3:

classify(3)
'Positive'
classify(0)
'Zero'
classify(-3)
'Negative'

If the input is 3, Python enters function world, sets x to have the value 3, and evaluates the if expression x < 0. The expression is a true value, so Python executes the first clause, sets result to equal “Positive” and skips to the end of the if statement. Finally it executes return result to return “Positive” from the function.

If the input is 0, Python enters function world, sets x to have the value 0, and evaluates the if expression x < 0. The expression is a false value, so Python moves on to the next clause, which is elif x == 0:. This has another if expression x == 0. It is a true value, so Python executes this clause, and sets result equal to “Zero”. Then it skips to the end of the if statement, and returns the value in result.

If the input is -3, Python enters function world, sets x to have the value -3, and evaluates the if expression x < 0. The expression is a false value, so Python moves on to the next clause, which is elif x == 0. This expression is also a false value so Python moves to the next clause, which is the else: clause, and executes that, setting result to be “Negative”. Now we are at the end of the if statement, and we return result.

We can have as many elif clauses as we want. For example, imagine we want to classify the number into one of the following categories:

  1. above 10 ('Large positive')

  2. from (not including) 0 through 10 ('Small positive')

  3. exactly 0 ('Zero')

  4. from (not including) 0 through -10 ('Small negative')

  5. below -10 ('Large negative')

def classify(x):
    if x > 10:
        result = 'Large positive'
    elif x > 0:
        result = 'Small positive'
    elif x == 0:
        result = 'Zero'
    elif x >= -10:  # Greater than or equal to
        result = 'Small negative'
    else:
        result = 'Large negative'
    return result
classify(-100)
'Large negative'
classify(0)
'Zero'

Interlude - returning early

Remember the simpler version of our function:

def classify(x):
    if x > 0:
        result = 'Positive'
    elif x == 0:
        result = 'Zero'
    else:
        result = 'Negative'
    return result

You can see that, if x is greater than 0, then we reach result = 'Positive'. In fact, when we get to that line, our work is done, and we might as well leave function world and send back the result. We can do that by using another return statement, like this:

def classify_returning_early(x):
    if x > 0:
        return 'Positive'
    elif x == 0:
        return 'Zero'
    else:
        return 'Negative'

This will work exactly the same way as the original version above. This way, as soon as we have the answer we need, we are breaking out of function world, and sending back the answer.

Notice that we do not need a final return statement any more, because our if statement has covered all possible values. Therefore, we will always return inside the if statement, and we will never reach any statements that follow the if statement.

Here is the new version in action:

print(classify_returning_early(3))
print(classify_returning_early(-3))
print(classify_returning_early(0))
Positive
Negative
Zero

This is a common pattern, where we break out of function world as soon as we have the answer we need.

The General Form

A conditional statement can also have multiple clauses with multiple bodies, and only one of those bodies can ever be executed. The general format of a multi-clause conditional statement appears below.

if <if expression>:
    <if body>
elif <elif expression 0>:
    <elif body 0>
elif <elif expression 1>:
    <elif body 1>
...
else:
    <else body>

There is always exactly one if clause, but there can be any number of elif clauses. Python will evaluate the if and elif expressions in the headers in order until one is found that is a true value, then execute the corresponding body. The else clause is optional. When an else header is provided, its else body is executed only if none of the header expressions of the previous clauses are true. The else clause must always come at the end (or not at all).

Now try the exercises.

Note

This page has content from the Conditional_Statements notebook of an older version of the UC Berkeley data science course. See the Berkeley course section of the license file.