<jupyter_start><jupyter_text>Controlling Flow with Conditional StatementsNow that you've learned how to create conditional statements, let's learn how to use them to control the flow of our programs.  This is done with `if`, `elif`, and `else` statements.   The `if` Statement What if we wanted to check if a number was divisible by 2 and if so then print that number out.  Let's diagram that out. ![image.png](attachment:image.png)- Check to see if A is even- If yes, then print our message: "A is even" This use case can be translated into a "if" statement.  I'm going to write this out in pseudocode which looks very similar to Python.```textif A is even:    print "A is even"```<jupyter_code># Let's translate this into Python code
def check_evenness(A):
    if A % 2 == 0:
        print(f"A ({A:02}) is even!")

for i in range(1, 11):
    check_evenness(i)
# You can do multiple if statements and they're executed sequentially

A = 10

if A > 0:
    print('A is positive')
if A % 2 == 0:
    print('A is even!')<jupyter_output>A is positive
A is even!<jupyter_text>The `else` StatementBut what if we wanted to know if the number was even OR odd?  Let's diagram that out:![image.png](attachment:image.png) Again, translating this to pseudocode, we're going to use the 'else' statement:```textif A is even:    print "A is even"else:    print "A is odd"```<jupyter_code># Let's translate this into Python code
def check_evenness(A):
    if A % 2 == 0:
        print(f"A ({A:02}) is even!") 
    else:
        print(f'A ({A:02}) is odd!')

for i in range(1, 11):
    check_evenness(i)<jupyter_output>A (01) is odd!
A (02) is even!
A (03) is odd!
A (04) is even!
A (05) is odd!
A (06) is even!
A (07) is odd!
A (08) is even!
A (09) is odd!
A (10) is even!<jupyter_text>The 'else if' or `elif` StatementWhat if we wanted to check if A is divisible by 2 or 3?  Let's diagram that out: ![image.png](attachment:image.png) Again, translating this into psuedocode, we're going to use the 'else if' statement.```textif A is divisible by 2:    print "2 divides A"else if A is divisible by 3:    print "3 divides A"else    print "2 and 3 don't divide A"```<jupyter_code># Let's translate this into Python code
def check_divisible_by_2_and_3(A):
    if A % 2 == 0:
        print(f"2 divides A ({A:02})!") 
    
    # else if in Python is elif
    elif A % 3 == 0:
        print(f'3 divides A ({A:02})!')
    else:
        print(f'A ({A:02}) is not divisible by 2 or 3)')
        
for i in range(1, 11):
    check_divisible_by_2_and_3(i)<jupyter_output>A (01) is not divisible by 2 or 3)
2 divides A (02)!
3 divides A (03)!
2 divides A (04)!
A (05) is not divisible by 2 or 3)
2 divides A (06)!
A (07) is not divisible by 2 or 3)
2 divides A (08)!
3 divides A (09)!
2 divides A (10)!<jupyter_text>Order MattersWhen chaining conditionals, you need to be careful how you order them.  For example, what if we wanted te check if a number is divisible by 2, 3, or both: ![image.png](attachment:image.png)<jupyter_code># Let's translate this into Python code
def check_divisible_by_2_and_3(A):
    if A % 2 == 0:
        print(f"2 divides A ({A:02})!") 
    elif A % 3 == 0:
        print(f'3 divides A ({A:02})!')
    elif A % 2 == 0 and A % 3 == 0:
        print(f'2 and 3 divides A ({A:02})!')
    else:
        print(f"2 or 3 doesn't divide A ({A:02})")
        
for i in range(1, 11):
    check_divisible_by_2_and_3(i)<jupyter_output>2 or 3 doesn't divide A (01)
2 divides A (02)!
3 divides A (03)!
2 divides A (04)!
2 or 3 doesn't divide A (05)
2 divides A (06)!
2 or 3 doesn't divide A (07)
2 divides A (08)!
3 divides A (09)!
2 divides A (10)!<jupyter_text>Wait! we would expect that 6, which is divisible by both 2 and 3 to show that!  Looking back at the graphic, we can see that the flow is checking for 2 first, and since that's true we follow that path first.  Let's make a correction to our diagram to fix this: ![image.png](attachment:image.png)<jupyter_code># Let's translate this into Python code
def check_divisible_by_2_and_3(A):
    if A % 2 == 0 and A % 3 == 0:
        print(f'2 and 3 divides A ({A:02})!')
    elif A % 3 == 0:
        print(f'3 divides A ({A:02})!')
    elif A % 2 == 0:
        print(f"2 divides A ({A:02})!") 
    else:
        print(f"2 or 3 doesn't divide A ({A:02})")
        
for i in range(1, 11):
    check_divisible_by_2_and_3(i)<jupyter_output>2 or 3 doesn't divide A (01)
2 divides A (02)!
3 divides A (03)!
2 divides A (04)!
2 or 3 doesn't divide A (05)
2 and 3 divides A (06)!
2 or 3 doesn't divide A (07)
2 divides A (08)!
3 divides A (09)!
2 divides A (10)!<jupyter_text>**NOTE:** Always put your most restrictive conditional at the top of your if statements and then work your way down to the least restrictive.![image.png](attachment:image.png)  In-Class Assignments- Create a funcition that takes two inputs variables `A` and `divisor`.  Check if `divisor` divides into `A`.  If it does, print `" is divided by "`.  Don't forget about the `in` operator that checks if a substring is in another string.- Create a function that takes an input variable `A` which is a string.  Check if `A` has the substring `apple`, `peach`, or `blueberry` in it.  Print out which of these are found within the string.  Note: you could do this using just if/elif/else statements, but is there a better way using lists, for loops, and if/elif/else statements?  Solutions<jupyter_code>def is_divisible(A, divisor):
    if A % divisor == 0:
        print(f'{A} is divided by {divisor}')
        
A = 37

# this is actually a crude way to find if the number is prime
for i in range(2, int(A / 2)):
    is_divisible(A, i)
    
# notice that nothing was printed?  That's because 37 is prime
    
B = 27
for i in range(2, int(B / 2)):
    is_divisible(B, i)
# this is ONE solution.  There are more out there and probably better
# one too
def check_for_fruit(A):
    found_fruit = []
    if 'apple' in A:
        found_fruit.append('apple')
    if 'peach' in A:
        found_fruit.append('peach')
    if 'blueberry' in A:
        found_fruit.append('blueberry')
        
    found_fruit_str = ''
    for fruit in found_fruit:
        found_fruit_str += fruit
        found_fruit_str += ', '
        
    if len(found_fruit) > 0:
        print(found_fruit_str + ' is found within the string')
    else:
        print('No fruit found in the string')
check_for_fruit('there are apples and peaches in this pie')<jupyter_output>apple, peach,  is found within the string
{'fraction_non_alphanumeric': 0.11328908782219478, 'fraction_numerical': 0.03812316715542522, 'mean_word_length': 3.360699865410498, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '1070', 'license': 'MIT', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2173, 'n_words': 964, 'path': 'Lecture Material/07_Conditional_Logic_and_Control_Flow/07.3_ControllingFlowWithConditionalStatements.ipynb', 'repo_name': 'knherrera/pcc-cis-012-intro-to-programming-python', 'size': '6479'}