Conditionals

Back to main page

Boolean expressions

Boolean expressions are expressions that evaluate to either true are false. These are integral to Python’s ability to change program behavior (i.e. control flow) depending on whatever condition you want tested. As examples of boolean expressions:

5 == 2 + 3         # True
5 == 1             # False
len('Hello') == 3  # False
10 < 15            # True

These are all examples of boolean expressions involving comparison operators. For a full list:

OperationDescription
x == yTrue if x is equal to y. False otherwise
x != yTrue if x is not equal to y. False otherwise
x > yTrue if x is greater than y. False otherwise
x >= yTrue if x is greater than or equal to y. False otherwise
x < yTrue if x is less than y. False otherwise
x <= yTrue if x is less than or equal to y. False otherwise

Logical operators

There are three logical operators in Python: and, or, and not. These are used to chain together boolean expressions to create more complex expressions. For instance, suppose we want to check if 6 is divisible by both 2 and 3. Then we need to check that the remainders when 6 is divided by both are both 0.

(6 % 2 == 0) and (6 % 3 == 0)  # True

If, elif, and else

If statements are written as follows:

if CONDITION:
    STATEMENTS

It is also possible to have an else clause:

if CONDITION:
    STATEMENTS_1
else:
    STATEMENTS_2

In case the reader wants to run through a list of conditions, the elif is perfect:

if CONDITION_1:
    STATEMENTS_1
elif CONDITION_2:
    STATEMENTS_2
else:
    STATEMENTS_3

There is no limit to the number of elif’s, we very well could have put three or four if we wanted to.

As an example, let us attempt to implement the absolute value function in mathematics. Recall that it is defined as the function \(|\cdot|:\mathbb R \to \mathbb R\) such that

\[\begin{align} |x| = \begin{cases} x & \text{if } x \ge 0, \\ -x & \text{otherwise}. \end{cases} \end{align}\]

We might code this as:

def absolute_value(x):
    if x >= 0:
        return x
    else:
        return -x

print(absolute_value(5))
print(absolute_value(0))
print(absolute_value(-5))

As another example, suppose we code the piecewise function \(\mathrm{sgn}:\mathbb R \to \mathbb R\) defined by

\[\begin{align} \mathrm{sgn}(x) = \begin{cases} 1 & \text{if } x > 0, \\ 0 & \text{if } x = 0, \\ -1 & \text{otherwise}. \end{cases} \end{align}\]

We might code this as:

def sgn(x):
    if x > 0:
        return 1
    elif x == 0:
        return 0
    else:
        return -1

print(sgn(5))
print(sgn(0))
print(sgn(-5))

Exercises

  1. Using if, elif, and else, write a function h(x) that implements the mathematical function \(h:\mathbb{R} \to \mathbb{R}\) given by

    \(\begin{align} h(x) = \begin{cases} 3 & \text{if } x > 3, \\ 2 & \text{if } 2 < x \le 3, \\ 1 & \text{if } 1 < x \le 2, \\ 0 & \text{if } x \le 1. \end{cases} \end{align}\)

  2. One way of collecting user input is the function input(). For example:

    temperature = input('Input the temperature: ')
    

    The variable temperature holds a value that is a string. Convert this string to an integer and then print a message based on the integral value of the temperature using if, elif, and else:

    • if temperature is greater than 100 degrees, then print "It is very hot.".
    • if temperature is between 90 (exclusive) and 100 degrees (inclusive), then print "It is hot.".
    • if temperature is between 70 (exclusive) and 90 degrees (inclusive), then print "It is nice.".
    • if temperature is between 50 (exclusive) and 70 degrees, then print "It is a little cold.".
    • if temperature is less than 50 degrees, then print "It is cold.". (Hint: You don’t need an elif for this last part. Why?)

Back to main page