# Lecture 8 -- Example 1. Conditionals

#%%
#---------------------------------------------------
# A. Conditional Expressions (review)
#---------------------------------------------------
#%%
# Recall there were five important keywords: 
#   `True`
#   `False` 
#   `and`
#   `or`
#   `not`

# We can use these for Boolean logic:
print(True and True) # True
print(True and False) # False
print(True and not True) # False
print(True or False) # True

#%%
# There were also some important comparison operators:
#
#   == : equal (comparison, not assignment!)
#   != : not equal
#   <  : less than
#   <= : less than or equal
#   >  : greater than
#   >= : greater than or equal

print (3 >= 2) # True
print (5 == 2) # False
print (5 != 2) # True

# **Spyder Pro-tip**: [ctrl]+[1] = comment/uncomment lines

#%%
#---------------------------------------------------
# B. The `if` statement
#---------------------------------------------------
#%%

#
# The `if` statement is a keyword.
# Note the syntax: (i) a colon and (ii) indendation

if (5 < 3):
    print('This will never print.')

# no extra line is necessary here, but it makes it more obvious
print('This is not inside the if statement.')

#%%
# We can also skip lines. Indentation is the key.
if (2 < 3):
    print('This will print.')
    
    print('So will this.')
   
#%% ------
# Practice
#%% ------
# Write an if statment that prints 'Hello' if 5 is less than or equal to 7.

#%%
# If statements also work with variables.
x = 2
y = 3
if (y > x):
    print('x+y = ', x+y)
    
#%%
# or with expressions
x = 2
y = 3
if (y**2 > x**2):
    print('x**2 = ', x**2)
    print('y**2 = ', y**2)

#%%
# We can also do more complicated logic.
z = 1
print('y > x', y > x)
print('y < z', y < z)
print('z < x', z < x)
if ((y > x or y < z) and z < x ):
    print('Will this print?')

#%% ------
# Practice
#%% ------
# Let the variable 'd' be the day of the month of your birthday (e.g. d=22). 
# Find the square root, and print the value if it is less than 4.5.


#%%
#---------------------------------------------------
# C. else and elif
#---------------------------------------------------
#%%

#
# `else` and `elif` are two additional keywords
# The `else` statement lets us catch everything the if statment misses.

if (5 < 3):
    print('This will never print')
else: # Don't forget the colon here!
    print('But this will...')

#%%
# What if we have a couple of different cases to catch?
# This is called a nested if statement.
x = 1.5
if (x >= 0 and x < 1):
    print('x is between 0 and 1')
else: 
    if( x >= 1 and x < 2):
        print('x is between 1 and 2')
    else:
        print('x is greater than or equal to 2')
        
#%%
# The previous statement is easier to write using the 
# special keyword `elif` which stands for "else if".
if (x >= 0 and x < 1):
    print('x is between 0 and 1')
elif( x >= 1 and x < 2): # again, notice the colon
    print('x is between 1 and 2') # notice how the indentation is different
else:
    print('x is greater than or equal to 2')

#%%    
# `elif` is great for times when we have multiple cases
# However, sometimes we still need nested statements
if (x >= 0 and x < 1):
    print('x is between 0 and 1')
    if( x < 0.25):
        print('x is between 0 and 0.25')
    elif (x >= 0.25 and x < 0.5):
        print('x is between 0.25 and 0.5')
    elif (x >= 0.5 and x < 0.75):
        print('x is between 0.5 and 0.75')
    else:
        print('x is between 0.75 and 1')
else: 
    print('x is not between 0 and 1')
    
#%% ------
# Practice
#%% ------
# Let the variable 'm' be the number of the month of your birthday (e.g. Jan =
# 1, Feb = 2, etc.) Print 'Happy Birthday' if your birthday is in January but
# print the number of the month if not.

