Module 0.4 - Conditionals
Objectives
By the end of this module you will be able to:
- Mentally execute (trace) code with conditionals:
if
,if
-else
, andif
-elif
-else
statements. - Write and debug code with conditionals.
- Identify new syntactic elements related to the above.
0.4.0 A Simple Comparison
It is often useful to compare different variable values and execute different code based on those variables’ values.
- For instance, checking if one variable is greater than another.
- The mathematical expression for “x is greater than y” is \(x > y\)
- In Python, it looks similar:
x > y
Consider this program:
= 5
x = 4
y
if x > y:
print('X is larger than Y')
print("OK, we're done")
Exercise 0.4.1
How does this work? First, observe:
Now, at the moment the if
statement executes, the condition is evaluated:
If the condition is true then the code that’s indented below the if
statement executes.
if
statements use indented blocks to logically group code and execute it when conditions are met- For functions, the code executes when the function is called
- For
if
statements, code executes when:- The program reaches the
if
statement, and - The condition after
if
is satisfied
- The program reaches the
Consider what happens when y is 6:
Exercise 0.4.2
A numeric variable can be: strictly less, less than or equal to, strictly greater, greater than or equal to, or equal to another numeric variable. Accordingly, the different types of less/greater comparisons are:
< b # Strictly less than, as when a=3, b=4
a <= b # Could be less (a=3, b=5), could be equal (a=3, b=3)
a > b # Strictly greater (a=3, b=2)
a >= b # Could be greater (a=3, b=1), could be equal (a=2, b=2) a
We can also check for equality with ==
and inequality ==
.
== b # a and b are equal, such as a=3, b=3
a != b # a and are NOT equal: a=3, b is anything other than 3
a
Note that Python comparisons work between integers and floating point numbers, but equality requires that they are exactly equal. 10
is equal to 10.0
but not equal to 10.0000001
or 9.9999999
.
0.4.1 if
and else
Think of else
as if
’s occasional partner.
Consider this example:
= 5
x = 5
y
if x > y:
print('X is larger than Y')
else:
print('X is not larger than Y.')
print("OK, we're done")
Exercise 0.4.3
When x
is indeed larger than y
, the code in the if
body executes:
When the if
condition evaluates to false:
What happens when x
is 5 and y
is 5?
- When
x
andy
are equal,x
is not greater thany
, so the result of this conditional is the same as ifx
were less thany
. - The conditional we wrote is only checking if
x
is greater thany
, and the relationship between the two numbers does not otherwise matter.
- If we want to check other conditions, like equality, we must explicitly write those checks into our code.
0.4.2 if
-elif
-else
Consider this variation:
= 5
x = 5
y
if x > y:
print('X is larger than Y')
elif y > x:
print('X is not larger than Y?')
print('Y is larger')
else:
print('X and Y are equal')
print("Done")
Exercise 0.4.4
First, consider the case x=5
, y=5
:
Now consider x=5
, y=4
:
Next: x=5
, y=6
:
One can have as many elif
sections as one would like, for example:
x = 3
if x == 1:
print('one')
elif x == 2:
print('two')
elif x == 3:
print('three')
elif x == 4:
print('four')
else:
print('greater than four')
print('finished')
Think of the whole thing as a giant if
statement: In the above case, when x
is 3, the execution path through the giant if
statement is:
The execution pathway, illustrated:
The execution pathway, described:
- The
if
statement on Line 3 is false (x
is not equal to 1), so its body on Line 4 is skipped - The
elif
statement on Line 5 is false (x
is not equal to 2), so its body on Line 6 is skipped - The
elif
statement on Line 7 is true (x
is equal to 3), so its body on Line 8 is executed - The remaining
elif
andelse
statements are skipped because oneif
orelif
wasTrue
, and the program exits the conditional block and executes Line 14
Exercise 0.4.5
Consider this program:
Exercise 0.4.6
End-Of-Module Problems
Full credit is 100 pts (complete at least two problems). There is no extra credit.
Starting with this module, we will sometimes give you starter code, including functions and tests. This is code to help set up the problem - you don’t have to understand the starter code, but you also shouldn’t change it.