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-elsestatements. - 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:
x = 5
y = 4
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.
ifstatements 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 
ifstatements, code executes when:- The program reaches the 
ifstatement, and - The condition after 
ifis 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:
a < 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)We can also check for equality with == and inequality ==.
a == 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
 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.
if (1.5 + 1.5) == 3:
  print("1.5 plus 1.5 equals 3")
if (0.1 + 0.2) == 0.3:
  print("0.1 plus 0.2 equals 0.3")What behavior do you expect from this program?
0.4.1 if and else
Think of else as if’s occasional partner.
Consider this example:
x = 5
y = 5
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 
xandyare equal,xis not greater thany, so the result of this conditional is the same as ifxwere less thany. - The conditional we wrote is only checking if 
xis 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:
x = 5
y = 5
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 
ifstatement on Line 3 is false (xis not equal to 1), so its body on Line 4 is skipped - The 
elifstatement on Line 5 is false (xis not equal to 2), so its body on Line 6 is skipped - The 
elifstatement on Line 7 is true (xis equal to 3), so its body on Line 8 is executed - The remaining 
elifandelsestatements are skipped because oneiforelifwasTrue, 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.