<jupyter_start><jupyter_text>If-Else Statements In this lesson we're going to move away from variable types and data structures and instead look at conditional programming. We'll see how to make decisions using the if, elif and else statements.This is one of the most powerful constructs we can use when programming and it's especially helpful when making different charts. For example, we might want to set a bar on a barchart to be a different colour if it shows a value above a certain threshhold. We would use an if statement to achieve this. How do If statements work?The if statement in Python is constructed as follows:````if :    ````The condition must be an expression which evaluates to a boolean (True or False) value. If the condition evaluates to True, then the code below is run. In Python, we must indent the code to run with four spaces (or a tab). Any code that is not indented after the statement is run anyway.In the example below, we're checking if a is greater than three, and if it is we return a string confirming this:<jupyter_code>a = 5

if a > 3:
    print("a is greater than 3")
print("This code runs anyway")<jupyter_output>a is greater than 3
This code runs anyway<jupyter_text>When a is not greater than three, nothing happens in the if statemnt because there is no code to run (notice that the code outside of the if statement still runs):<jupyter_code>a = 2

if a > 3:
    print("a is greater than 3")
print("This code runs anyway")<jupyter_output>This code runs anyway<jupyter_text>Adding an Else StatementIf we want to run some code when the condition evaluates to False, we need to add an else statement. The code that comes after this statement must be indented by four spaces (or a tab). The syntax is as follows:````if :    else:    ````The code after the else statement only runs when the if statement evaluates to False:<jupyter_code>a = 2

if a > 3:
    print("a is greater than 3")
else:
    print("a is less than 3")
    
print("This code runs anyway")<jupyter_output>a is less than 3
This code runs anyway<jupyter_text>Adding other conditionsWe can also introduce more conditions using the elif statement (else if). These conditions work in the same way as the ifstatement; the code after each is only run if the condition evaluates to True. The syntax is as follows:````if :    elif :    elif :    else:    ````You might have noticed an error with the code in the cell above; what happens when a is 3? The statement "a is less than 3" is clearly not true here! We can use an elif statement to add another condition:<jupyter_code>a = 3

if a > 3:
    print("a is greater than 3")
elif a == 3:
    print("a is equal to 3")
else:
    print("a is less than 3")
    
print("This code runs anyway")<jupyter_output>a is equal to 3
This code runs anyway<jupyter_text>We can add as many elif statements as we wish:<jupyter_code>a = 2

if a > 3:
    print("a is greater than 3")
elif a == 3:
    print("a is equal to 3")
elif a == 2:
    print("a is equal to 2")
else:
    print("a is less than 2")
    
print("This code runs anyway")<jupyter_output>a is equal to 2
This code runs anyway<jupyter_text>Nested If StatementsWe can place an If-Else statement as the code to run inside an if statement. The code to run in the nested if statement must be indented by 8 spaces (or two tabs). The syntax is as follows:````if :    if :            else:        else:    ````Creating nested if statements allows us to create complex decision trees and to tailor our code to deal with many different situations.In the cell below, we first check if a is greater than 3, if this condition is True, we then invoke the nested if statment and check if a is then subsequently greater than 4; if not, we know that it must be equal to 4, and this is captured in the Else part of the nested If statement.<jupyter_code>a = 6

if a > 3:
    if a > 4:
        print("a is greater than 4")
    else:
        print("a is equal to 4")
elif a == 3:
    print("a is equal to 3")
else:
    print("a is less than 3")
    
print("This code runs anyway")<jupyter_output>a is greater than 4
This code runs anyway
{'fraction_non_alphanumeric': 0.06491611962071481, 'fraction_numerical': 0.010454655968879163, 'mean_word_length': 3.789289871944121, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 26, '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': '245503', 'license': 'MIT', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1122, 'n_words': 669, 'path': 'PlotlyandPython/Lessons/(01) Intro to Python/Notebooks/(10) If-Else Statements.ipynb', 'repo_name': 'peternewman22/Python_Courses', 'size': '4113'}