Harold Nelson
1/26/2021
Read the section in PY4E. Then evaluate each of the following expressions in your head before proceeding to the following slide to see the answer.
\[ 3<4\]
\[ 3 \neq 4\] Note the use of the mathematical symbol.
The remainder after dividing 12 by 2 is zero. Say this in python instead of English.
Read the section in PY4E. Then evaluate each of the following expressions in your head before proceeding to the following slide to see the answer.
In the exercises that follow, try to predict what the python interpreter would do. Commit yourself on paper to your prediction. Then copy the code to a Jupyter notebook and run it.
Each block begins with one or more logical variables of the form condn, where the n is replaced by an integer. You should try different combinations of these variables to expand the original exercise into multiple exercises. If there are two logical variables, there are four possible combinations. By treeing out the possibilities, you can see that if there are \(n\) possibilities, there are \(2^n\) possible combinations.
If the condition after the if is true, the statement(s) in the scope of the if statement are executed. Then the following statements are executed regardless of the value of the condition.
If the condition is false, the statement(s) in the scope of the if are not executed. Regardless, the statement following the if is executed.
Note that an if nested within this structure follows the same rule.
cond1 = True
cond2 = True
if cond1:
print("Point A")
if cond2:
print("Point B")
print("Point C")
print("Point D")
In your Jupyter notebook, try each of the four possible combinations.
If the condition after the if is true, the statement(s) in the scope of the if statement are executed; and the statements in the scope of the else are not executed
If the condition after the if is false, the stetements in the scope of the else are executed; and the statements in the scope of the if are not executed.
Then the following statements are executed regardless of the value of the condition.
When an if is followed by one or more elif statements, the conditions following each of these are evaluated in order. As soon as one of the conditions is found to be true, the statements in the corresponding scope are executed. Then the statement following all of these is executed.
Basically this is the same as if + elif, but if none of the conditions is true, the statements in the scope of the else are executed. Then the following statement is executed.