Conditional Statements¶
Note:
There are reading-comprehension exercises included throughout the text. These are meant to help you put your reading to practice. Solutions for the exercises are included at the bottom of this page.
In this section, we will be introduced to the if, else, and
elif statements. These allow you to specify that blocks of code are
to be executed only if specified conditions are found to be true, or
perhaps alternative code if the condition is found to be false. For
example, the following code will square x if it is a negative
number, and will cube x if it is a positive number:
# a simple if-else block
if x < 0:
x = x ** 2
else:
x = x ** 3
Please refer to the “Basic Python Object Types” subsection to recall the
basics of the “boolean” type, which represents True and False values. We
will extend that discussion by introducing comparison operations and
membership-checking, and then expanding on the utility of the built-in
bool type.
Comparison Operations¶
Comparison statements will evaluate explicitly to either of the
boolean-objects: True or False. There are eight comparison
operations in Python:
| Operation | Meaning |
|---|---|
< |
strictly less than |
<= |
less than or equal |
> |
strictly greater than |
>= |
greater than or equal |
== |
equal |
!= |
not equal |
is |
object identity |
is not |
negated object identity |
The first six of these operators are familiar from mathematics:
>>> 2 < 3
True
Note that = and == have very different meanings. The former is
the assignment operator, and the latter is the equality operator:
>>> x = 3 # assign the value 3 to the variable `x`
>>> x == 3 # check if `x` and 3 have the same value
True
Python allows you to chain comparison operators to create “compound” comparisons:
>>> 2 < 3 < 1 # performs (2 < 3) and (3 < 1)
False
Whereas == checks to see if two objects have the same value, the
is operator checks to see if two objects are actually the same
object. For example, creating two lists with the same contents produces
two distinct lists, that have the same “value”:
# demonstrating `==` vs `is`
>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> x == y
True
# `x` and `y` reference equivalent, but distinct lists
>>> x is y
False
Thus the is operator is most commonly used to check if a variable
references the None object, or either of the boolean objects:
>>> x = None
>>> x is None
True
# (2 < 0) returns the object `False`
# thus this becomes: `False is False`
>>> (2 < 0) is False
True
Use is not to check if two objects are distinct:
>>> 1 is not None
True
bool and Truth Values of Non-Boolean Objects¶
It can be useful to ascribe boolean values to non-boolean objects. For
example, in computer science, the number 0 is often associated with
False and non-zero numbers are associated with True. Python
ascribes boolean values to most of its built-in objects, which can be
evaluated with the built-in Python command bool. For example, in
Python the boolean value False is ascribed to the integer 0:
# Using `bool` to access the True/False
# value of non-boolean objects
>>> bool(0)
False
and non-zero Python integers are associated with True:
# nonzero values evaluate to `True`
>>> bool(2)
True
The following built-in Python objects evaluate to False via
bool:
FalseNone- Zero of any numeric type:
0,0.0,0j - Any empty sequence, such as an empty string or list:
'',tuple(),[],numpy.array([]) - Empty dictionaries and sets
Thus non-zero numbers and non-empty sequences/collections evaluate to
True via bool.
Takeaway:
The bool function allows you to evaluate the boolean values ascribed
to various non-boolean objects. For instance, bool([]) returns
False wherease bool([1, 2]) returns True.
if, else, and elif¶
We now introduce the simple, but powerful if, else, and elif
conditional statements. This will allow us to create simple branches in
our code. For instance, suppose you are writing code for a video game,
and you want to update a character’s status based on her/his number of
health-points (an integer). The following code is representative of
this:
if num_health > 80:
status = "good"
elif num_health > 50:
status = "okay"
elif num_health > 0:
status = "danger"
else:
status = "dead"
The following pseudo-code demonstrates the general template for conditional statements:
if <expression_1>:
the code within this indented block is executed if..
- bool(<expression_1>) is True
elif <expression_2>:
the code within this indented block is executed if..
- bool(<expression_1>) was False
- bool(<expression_2>) is True
...
...
elif <expression_n>:
the code within this indented block is executed if..
- bool(<expression_1>) was False
- bool(<expression_2>) was False
...
...
- bool(<expression_n-1>) was False
- bool(<expression_n>) is True
else:
the code within this indented block is executed only if
all preceding expressions were False
In practice this can look like:
x = [1, 2]
if 3 < len(x):
# bool(3 < 2) returns False, this code
# block is skipped
print("`x` has more than three items in it")
elif len(x) == 2
# bool(len(x) == 2) returns True
# this code block is executed
print("`x` has two items in it")
elif len(x) == 1
# this statement is never reached
print("`x` has one items in it")
else:
# this statement is never reached
print("`x` is an empty list")
"`x` has two items in it"
In its simplest form, a conditional statement requires only an if
clause. else and elif clauses can only follow an if clause.
# A conditional statement consisting of
# an "if"-clause, only.
x = -1
if x < 0:
x = x ** 2
# x is now 1
Similarly, conditional statements can have an if and an else
without an elif:
# A conditional statement consisting of
# an "if"-clause and an "else"
x = 4
if x > 2:
x = -2
else:
x = x + 1
# x is now -2
Conditional statements can also have an if and an elif without
an else:
# A conditional statement consisting of
# an "if"-clause and an "elif"
x = 'abc'
if len(x) < 9:
x = x * 3
elif len(x) > 40:
x = 'cba'
# x is now 'abcabcabc'
Note that only one code block within a single if-elif-else statement can
be executed: either the “if-block” is executed, or an “elif-block” is
executed, or the “else-block” is executed. Consecutive if-statements,
however, are completely independent of one another, and thus their code
blocks can be executed in sequence, if their respective conditional
statements resolve to True.
# consecutive if-statements are independent
x = 5
y = 0
if x < 10:
y += 1
if x < 20:
y += 1
# y is now 2
Reading Comprehension: Conditional statements
- Assume
my_listis a list. Given the following code:
first_item = None
if my_list:
first_item = my_list[0]
What will happen if my_list is []? Will IndexError be
raised? What will first_item be?
2. Assume variable my_file is a string storing a filename, where a
period denotes the end of the filename and the beginning of the
file-type. Write code that extracts only the filename.
my_file will have at most one period in it. Accommodate cases where
my_file does not include a file-type.
That is:
"code.py"\(\rightarrow\)"code""doc2.pdf"\(\rightarrow\)"doc2""hello_world"\(\rightarrow\)"hello_world"
Inline if-else statements¶
Python supports a syntax for writing a restricted version of if-else statements in a single line. The following code:
if num >= 0:
sign = "positive"
else:
sign = "negative"
can be written in a single line as:
sign = "positive" if num >=0 else "negative"
This is suggestive of the general underlying syntax for inline if-else statements:
The inline if-else statement:
The expression A if <condition> else B returns A if
bool(<condition>) evaluates to True, otherwise this expression
will return B.
This syntax is highly restricted compared to the full “if-elif-else” expressions - no “elif” statement is permitted by this inline syntax, nor are muli-line code blocks within the if/else clauses.
Inline if-else statements can be used anywhere, not just on the right side of an assignment statement, and can be quite convenient:
# using inline if-else statements in different scenarios
>>> x = 2
# will store 1 if `x` is non-negative
# will store 0 if `x` is negative
>>> my_list = [1 if x >= 0 else 0]
>>> my_list
[1]
>>> "a" if x == 1 else "b"
'b'
We will see this syntax shine when we learn about comprehension statements. That being said, this syntax should be used judiciously. For example, inline if-else statements ought not be used in arithmetic expressions, for therein lies madness:
# don't ever do this...ever!
2 - 3 if x < 1 else 1 + 6*2 if x >= 0 else 9
Reading Comprehension Exercise Solutions:¶
Conditional statements
1. If my_list is [], then bool(my_list) will return
False, and the code block will be skipped. Thus first_item will
be None.
2. First, check to see if . is even contained in my_file. If it
is, find its index-position, and slice the string up to that index.
Otherwise, my_file is already the file name.
my_file = "code.pdf"
if "." in my_file:
dot_index = my_file.index(".")
filename = my_file[:dot_index]
else:
filename = my_file