Abstract¶
Conditional execution allows a program to selectively execute different parts of the code based on specific conditions. This capability enables the program to adapt and respond appropriately to various situations it encounters, making it more flexible and functional. By using boolean expressions and conditional statements, students will learn to direct the flow of execution to handle a wide range of inputs and scenarios effectively.
from __init__ import install_dependencies
await install_dependencies()
import math
from flowcharts import *
from ipywidgets import interact
from dis import dis
%load_ext divewidgets
%load_ext jupyter_ai
%ai update chatgpt dive:chat
Motivation¶
Conditional execution means running different pieces of code based on different conditions. Why do programmers need conditional executation?
For instance, when trying to compute a/b
, b
may be 0
and division by 0
is invalid.
%%optlite -h 450
def multiply_or_divide(a, b):
print("a:{}, b:{}, a*b:{}, a/b:{}".format(a, b, a * b, a / b))
multiply_or_divide(1, 2)
multiply_or_divide(1, 0)
Although division by 0 is invalid, multiplication remains valid but it is not printed due to the division error.
Can we skip only the division but not the multiplication when b
is equal to 0
?
Solution 1: Use a conditional expression that specifies which code block should be executed under what condition:
... if ... else ...
def multiply_or_divide(a, b):
q = a / b if b else "undefined"
print("a:{}, b:{}, a*b:{}, a/b:{}".format(a, b, a * b, q))
multiply_or_divide(1, 2)
multiply_or_divide(1, 0) # multiplication is valid but not shown
Solution 2: Use a boolean expression:
... and ... or ...
def multiply_or_divide(a, b):
q = b and a / b or "undefined"
print("a:{}, b:{}, a*b:{}, a/b:{}".format(a, b, a * b, q))
multiply_or_divide(1, 2)
multiply_or_divide(1, 0) # multiplication is valid but not shown
Solution 3: Monitor and catch the error using a try statement:
def multiply_or_divide(a, b):
try:
q = a / b
except ZeroDivisionError:
q = "undefined"
print("a:{}, b:{}, a*b:{}, a/b:{}".format(a, b, a * b, q))
multiply_or_divide(1, 2)
multiply_or_divide(1, 0) # multiplication is valid but not shown
In this notebook, we will introduce the first two solutions. The last one is a better way to handle exceptions when the operations get complicated.
%%ai chatgpt -f text
Explain in one paragraph, using a simple example, the benefits of using a try statement over conditional checks for handling exceptions and errors.
Comparison Operators¶
A comparison/relational operators along with its operands form an expression, which evaluates to a boolean value:
True # if the operands satisfy certain conditions, and
False # otherwise.
Unlike many other languages, Python capitalized the keywords for the boolean values to signify that they are constants, just like the keyword None
. Hence,
true = False # is invalid in many languages but not Python
False = true # is valid in many languages but not Python
What are boolean values “boolean”?
Boolean expressions are named after George Boole, whose work laid the foundation of digital circuitry. Below are the links to the ebooks, thanks to Project Gutenberg:
Caution
Later in the course, you’ll learn that operators can be customized to return boolean or non-boolean values. For example, ==
can be redefined to perform addition, and +
can be redefined to check equality.
Equality and Inequalties¶
The equality and inequality relationships in mathematics are implemented using the following comparison operators:
You can explore these operators using the widgets below:
comparison_operators = ["==", "<", "<=", ">", ">=", "!="]
@interact(operand1="10", operator=comparison_operators, operand2="3")
def comparison(operand1, operator, operand2):
expression = f"{operand1} {operator} {operand2}"
value = eval(expression)
print(
f"""{'Expression:':>11} {expression}\n{'Value:':>11} {value}\n{'Type:':>11} {type(value)}"""
)
%%ai chatgpt -f text
Explain in a paragraph how the comparison operators == may be represented differently in other programming languages such as maxima, scheme, and bash.
What is the precedence of comparison operators?
All the comparison operators have the same precedence lower than that of +
and -
.
1 + 2 >= 3 # (1 + 2) >= 3
Similar to the assignment operations, comparison operators can be chained together but they are non-associative.
2.0 == 2 > 1, (2.0 == 2) > 1, 2.0 == (2 > 1)
1 <= 2 < 3 != 4, (1 <= 2) < (3 != 4)
Solution to Exercise 1 #
The second expression indeed does not involve chained comparison. The expressions inside the parentheses are first evaluated to boolean values, both of which are True
. Therefore, the entire expression True < True
evaluates to False
. You can also try evaluating the following expressions for further understanding:
False < True
- True ** 2 + 1
1 / False
To learn more about the relationship between bool
and int
, check out PEP 285.
The second expression is not a chained comparison:
- The expressions in the parentheses are evaluated to boolean values first to
True
, and so - the overall expression
True < True
is evaluated toFalse
. You may also want to try:False < True
- True ** 2 + 1
1 / False
To understand how bool
is related to int
, see the PEP 285.
# Comparisons beyond numbers
@interact(
expression=[
"10 == 10.",
'"A" == "A"',
'"A" == "A "',
'"A" != "a"',
'"A" > "a"',
'"aBcd" < "abd"',
'"A" != 64',
'"A" < 64',
]
)
def relational_expression(expression):
print(eval(expression))
Solution to Exercise 2 #
- Checks whether an integer is equal to a floating point number.
- Checks whether two characters are the same.
- Checks whether two strings are the same. Note the space character.
- Checks whether a character is larger than the order character according to their unicodes.
- Checks whether a string is lexicographically smaller than the other string.
- Checks whether a character is not equal to an integer.
- TypeError because there is no implementation that evaluates whether a string is smaller than an integer.
Comparing float
¶
How to compare floating point numbers?
x = 10
y = (x ** (1 / 3)) ** 3
x == y
Why False? Shouldn’t ?
- Floating point numbers have finite precisions and so
- we should instead check whether the numbers are close enough.
One method of comparing floating point numbers is:
abs_tol = 1e-9
y - abs_tol <= x <= y + abs_tol
where abs_tol
, often denoted as , is a positive number called the absolute tolerance.
Why call it absolute tolerance?
Note that the test remains unchanged if we swap x
and y
:
abs_tol = 1e-9
x - abs_tol <= y <= x + abs_tol
Using the absolute function abs
, we can also rewrite the comparison as follows:
abs_tol = 1e-9
abs(x - y) <= abs_tol
Is an absolute tolerance of 1e-9
good enough?
The same absolute tolerance fails if we set x = 1e10
instead of 10
?
x = 1e10
y = (x ** (1 / 3)) ** 3
abs_tol = 1e-9
abs(x - y) <= abs_tol
Why does the same absolute tolerance fail to tolerate the difference between x
and y
?
This is because floating point numbers “float” at different scales.
A better way is to use the isclose
function from math
module.
math.isclose(x, y)
How does isclose
work?
For example, we can check whether is within a certain percentage of :
rel_tol = 1e-9
y * (1 - rel_tol) <= x <= y * (1 + rel_tol)
Note that the above test is not symmetric between and , i.e., is relatively close to does not necessarily mean is relatively close to .
x = 1
y = 2
rel_tol = 0.5
y * (1 - rel_tol) <= x <= y * (1 + rel_tol), x * (1 - rel_tol) <= y <= x * (1 + rel_tol)
To make the comparison symmetric:
Why use but not ?
rel_tol = 1e-9
x = 1e10
y = (x ** (1 / 3)) ** 3
### BEGIN SOLUTION
abs(x - y) <= rel_tol * max(abs(x), abs(y))
### END SOLUTION
What if or is very close to 0? For instance, can you change rel_tol
so that math.isclose
returns True
?
x = 1e-15
y = 0
math.isclose(x, y, rel_tol = 1e-9)
For the most flexible comparison, one can impose both the absolute and relative tolerances:
rel_tol, abs_tol = 1e-9, 0.0
x = 1e-15
y = 0
### BEGIN SOLUTION
abs(x - y) <= max(rel_tol * max(abs(x), abs(y)), abs_tol)
### END SOLUTION
Conditional Constructs¶
To illustrate how python can carry out conditional execution, we will consider writing a program that sorts values in ascending order.
If-Then Construct¶
How to sort two values?
Given two values are stored as x
and y
, we want to
print(x,y)
ifx <= y
, andprint(y,x)
ify < x
.
Such a program flow is often represented by a flowchart like the following:
sort_two_values_fc1
How to read the flowchart?
A flowchart uses arrows to connects a set of annotated blocks. The rules were first specified by ANSI and later adopted in ISO 5807.
Why use a program flowchart?
A program flowchart is a powerful way of describing an algorithm quickly. Unlike a text-based programming language:
- The rules governing the program flow can be shown explicitly by arrows.
- The annotated graphical blocks can convey the meaning faster using visual clues.
How to implements the flowchart in python?
It is often useful to delay detailed implementations until we have written an overall skeleton. To leave a block empty, Python uses the keyword pass
.
# write a code skeleton
def sort_two_values(x, y):
pass
# print the smaller value first followed by the larger one
sort_two_values(1, 0)
sort_two_values(1, 2)
Without pass
, the code will fail to run, preventing you from checking other parts of the code.
Python provides the if
statement to implement the control flow specified by the diamond boxes in the flowchart.
def sort_two_values(x, y):
if x <= y:
pass
# print x before y
if y < x: pass # print y before x
sort_two_values(1, 0)
sort_two_values(1, 2)
To complete the implementations specified by the parallelogram boxes in the flow chart, we fill in the bodies/suites of the if
statements as follows:
def sort_two_values(x, y):
if x <= y:
print(x, y)
if y < x: print(y, x)
@interact(x="1", y="0")
def sort_two_values_app(x, y):
print("Values in ascending order:")
sort_two_values(eval(x), eval(y))
Test the program by filling in different values of x
and y
above.
How to indent?
As python uses indentation to indicate code blocks or suites:
print(x, y)
(Line 3) is indented to the right ofif x <= y:
(Line 2) to indicate it is the body of the if statement.- For convenience,
if y < x: print(y, x)
(Line 4) is a one-liner for anif
statement that only has one line in its block. - Both
if
statements (Line 2-4) are indented to the right ofdef sort_two_values(x,y):
(Line 1) to indicate that they are part of the body of the functionsort_two_values
.
The style guide recommends using 4 spaces for each indentation. In JupyterLab, you can simply type the tab
key.
We can visualize the execution as follows. Step through the execution to
- see which lines are skipped, and
- understand why they are skipped.
%%optlite -h 450
def sort_two_values(x, y):
if x <= y:
print(x, y)
if y < x: print(y, x)
sort_two_values(1, 0)
sort_two_values(1, 2)
If-Then-Else Construct¶
Can the sorting algorithm be improved further?
Hint
not (x <= y) and (y < x)
is a tautology, i.e., always true.
Consider the following modified flowchart:
sort_two_values_fc2
This can implemented by the else
clause of the if
statement as follows:
%%optlite -h 450
def sort_two_values(x, y):
if x <= y:
print(x, y)
else:
print(y, x)
sort_two_values(1, 0)
sort_two_values(1, 2)
Can we shorten the code to one line? This is possible with the conditional expression.
def sort_two_values(x, y):
print(("{0} {1}" if x <= y else "{1} {0}").format(x, y))
@interact(x="1", y="0")
def sort_two_values_app(x, y):
print("Values in ascending order:")
sort_two_values(eval(x), eval(y))
Solution to Exercise 5
A conditional expression must be an expression:
- It must give a value under all cases. To enforce that,
else
keyword must be provided. - An assignment statement does not return any value and therefore cannot be used for the conditional expression.
x = 1 if True else 0
is valid becausex =
is not part of the conditional expression.
Nested Conditionals¶
Now, consider a slight more challenging problem of sorting three values instead of two. A feasible algorithm is as follows:
sort_three_values_fc
To implement flowchart, we can use nested conditional constructs, where one conditional statement is placed within another, allowing for multiple layers of condition checks:
def sort_three_values(x, y, z):
if x <= y <= z:
print(x, y, z)
else:
if x <= z <= y:
print(x, z, y)
else:
if y <= x <= z:
print(y, x, z)
else:
if y <= z <= x:
print(y, z, x)
else:
if z <= x <= y:
print(z, x, y)
else:
print(z, y, x)
def test_sort_three_values():
sort_three_values(0, 1, 2)
sort_three_values(0, 2, 1)
sort_three_values(1, 0, 2)
sort_three_values(1, 2, 0)
sort_three_values(2, 0, 1)
sort_three_values(2, 1, 0)
test_sort_three_values()
Imagine what would happen if we have to sort many values. The program will not only be long, but also fat due to the indentation. To avoid an excessively long line due to the indentation, Python provides the elif
keyword that combines else
and if
.
def sort_three_values(x, y, z):
if x <= y <= z:
print(x, y, z)
elif x <= z <= y:
print(x, z, y)
elif y <= x <= z:
print(y, x, z)
elif y <= z <= x:
print(y, z, x)
elif z <= x <= y:
print(z, x, y)
else:
print(z, y, x)
test_sort_three_values()
def sort_three_values(x, y, z):
### BEGIN SOLUTION
# How many comparisons are needed in a program to sort n numbers?
if x > y:
x, y = y, x
if y > z:
y, z = z, y
if x > y:
x, y = y, x
print(x, y, z)
### END SOLUTION
sort_three_values(10, 17, 14)
Sorting algorithms
A sorting algorithm refers to the procedure of sorting any number of values in order. The performance of a sorting algorithm is measured primarily by its time complexity (the number of comparisons and swaps it makes) and its space complexity (the amount of additional memory it requires). It would be even better if the program is short and easy to read, but that depends partly on the design of the programming language. Python implemented its default sorted
function using the Timsort algorithm. See if you can understand how it works and how well it works.
%%ai chatgpt -f text
Explain in one paragraph what the minimum possible number of comparisons needed to sort n numbers is and why it is not the same as the maximum number of inversions.
Boolean Operations¶
Since chained comparisons are non-associative, it follows a different evaluation rule than arithmetic operators.
E.g., 1 <= 2 < 3 != 4
is equivalent to:
1 <= 2 and 2 < 3 and 3 != 4
The above is called a compound boolean expression, which is formed using the boolean/logical operator and
.
Why use boolean operators?
What if we want to check whether a number is either or ?
The following program checks whether a number is either or :
# Check if a number is outside a range.
@interact(x="15")
def check_out_of_range(x):
x_ = float(x)
is_out_of_range = x_ < 0 or x_ >= 100
print("Out of range [0,100):", is_out_of_range)
Can you rewrite the logical expression to use only the and
instead of or
?
and
instead of or
?Turns out this is mission impossible because and
alone is not functionally complete, i.e., it is not enough to give all possible boolean functions. We can add or
and not
operators to make it functionally complete.
%%ai chatgpt -f text
Explain in a paragraph why the set of operators including only "and" is not functionally complete.
%%ai chatgpt -f text
Explain in a paragraph why the set of operators "and", "or", and "not" is functionally complete.
The following table is called a truth table. It enumerates all possible input and output combinations for each boolean operator available in Python:
Table 2:Truth table for different logical operators
x | y | x and y | x or y | not x |
---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
Solution to Exercise 7
- Expression A evaluates to
True
becauseand
has higher precedence and so the expression has the same value asTrue or (False and True)
. - Expression B evaluates to
False
becauseand
is left associative and so the expression has the same value as(True and False) and True
. - Expression C evaluates to
True
becauseand
has a higher precedence and so the expression has the same value asTrue or (True and False)
. Note that(True or True) and False
evaluates to somethingFalse
instead, so precedence matters.
A compound boolean expression actually uses a short-circuit evaluation.
To understand this, we will use the following function to evaluate a boolean expression verbosely.
def verbose(id, expr):
"""Identify evaluated boolean expressions."""
print(id, "evaluated:", expr)
return expr
For instance:
verbose("1st expression", True)
verbose("2nd expression", False)
Visualization by Thonny IDE
You may also use the debugger of Thonny to visualize the step-by-step evaluations of the operands:
- In JupyterLab, click
File
New Launcher
Desktop
to launch a remote desktop for your server.[1] - Start Thonny in the remote desktop by clicking
Applications
Development
Thonny
.[2] - In the Thonny editor, write some Python code such as
True or False and True
and then save it to a file, saytest.py
.[3] - Debug the code by clicking the Thonny menu item
Run
Debug current script (nicer)
.[4] - Click the
step into
button to step through the executions.
Short-circuit or
¶
Consider evaluating True or False and True
in a verbose manner:
verbose("A", verbose(1, True) or verbose(2, False) and verbose(3, True))
Why the second and third expressions are not evaluated?
Because True or ... must be True (why?) so Python does not look further. From the documentation:
Short-circuit evaluation of or
The expression x or y
- first evaluates
x
; - if
x
is true, its value is returned; - otherwise,
y
is evaluated and the resulting value is returned.
Put it another way, (x or y)
translate to the following
_ if (_ := x) else y
Program 1:Translation of (x or y)
except for the extra variable _
. For example, True or False and True
translates to _ if (_ := True) else False and True
:
verbose("A", (_ if (_ := verbose(1, True)) else verbose(2, False) and verbose(3, True)))
Short-circuit and
¶
Now, consider evaluating False or False and True
in a verbose manner:
False or False and True
verbose("B", verbose(4, False) or verbose(5, False) and verbose(6, True))
Why expression 6 is not evaluated?
False or False and ...
must be False
so Python does not look further.
Short-circuit evaluation of and
The expression x and y
first evaluates x
;
if x
is false, its value is returned;
otherwise, y
is evaluated and the resulting value is returned.
Put it another way, (x and y)
translate to the following
_ if not (_ := x) else y
Program 2:Translation of (x and y)
except for the extra variable _
. For example, False or False and True
translates to False or (_ if not (_ := False) else True)
:
verbose(
"B", verbose(4, False) or (_ if not (_ := verbose(5, False)) else verbose(6, True))
)
Non-Boolean Values for Boolean Operations and Control Flow Statements¶
Interestingly, Python also allows logical operations to have non-boolean operands and return values. From the documentation:
Boolean interpretation
In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false:
False
None
- Numeric zero of all types
- Empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets)
All other values are interpreted as true.
The following is an application of this rule.
print("You have entered", input() or "nothing")
Solution to Exercise 8 #
- The code replaces an empty user input by the default string
nothing
because an empty string is regarded as False in a boolean operation. - If user input is non-empty, it is regarded as True in the boolean expression and returned immediately as the value of the boolean operation.
input(1) or input(2) and input(3)
Solution to Exercise 9
Enter ’ ', ‘’ and then ‘’.
### BEGIN SOLUTION
_ if (_ := input(1)) else (_ := input(3)) if (_ := input(2)) else _
### END SOLUTION
Comparison vs boolean operations
Is an empty string ""
equal to False
?
Try "" == False
.
- An empty string is regarded as false in a boolean operation but
- a comparison operation is not a boolean operation, even though it forms a boolean expression.
Should the following return False
?
0 == False and 1 == True
Try and explain what you see.
%%ai chatgpt -f text
In Python, explain in one paragraph what is the value of the following expression:
0 == False and 1 == True
Actual equivalence vs interpreted equivalence
Note that LLMs tend to hallucinate on subtle concepts like the one above. Indeed, 0
(1
) is actually equal to False
(True
).
The equality operator
==
consists of two equal signs, different from the assignment operator=
.Different from C or javascript:
- We can write
1 != 2
asnot 1 == 2
but not!(1 == 2)
because !
is not a logical operator. It is used to call a system shell command in IPython.
- We can write
Thonny is an IDE based on TKinter, which requires a proper desktop environment to run.
You can also launch a terminal in the remote desktop and run the command
thonny
.You can copy and paste code into the remote desktop by clicking
Remote Clipboard
button at the top right of the browser page and pasting your code into the textbox that appears below.Short-cut keys for Thonny may not work as the keys may be captured by the browser or your OS before they can reach Thonny.