<jupyter_start><jupyter_text>Logical Conditions and Operations in PythonConditional statements allow us to alter the flow of a program based on the outcome of a logical condition.<jupyter_code>from __future__ import print_function<jupyter_output><empty_output><jupyter_text>Comparison OperatorsThese operators compare the values of two variables and return `True` or `False` accordingly. These operators are in the heart of any logical condition.<jupyter_code>a = 5
b = 3

print('{} == {}: {}'.format(a, b, a == b))  # Returns True if the two operands are equal: False
print('{} != {}: {}'.format(a, b, a != b))  # Returns True if the two operands are different: True
print('{} > {}:  {}'.format(a, b, a > b))   # Returns True if the left operand is greater than the right one: True
print('{} < {}:  {}'.format(a, b, a < b))   # Returns True if the right operand is greater than the left one: False
print('{} >= {}: {}'.format(a, b, a >= b))  # Returns True if the left operand is greater or equal to the right one: True
print('{} <= {}: {}'.format(a, b, a <= b))  # Returns True if the right operand is greater or equal to the left one: False<jupyter_output>5 == 3: False
5 != 3: True
5 > 3:  True
5 < 3:  False
5 >= 3: True
5 <= 3: False<jupyter_text>Logical OperatorsThese operators allow is to combine two or more logical conditions into a larger condition that too returns True of False.<jupyter_code>t = True
f = False

print('{} and {}: {}'.format(t, f, t and f)) # Returns True if both operands are True: False
print('{} or {}:  {}'.format(t, f, t or f))  # Returns True if one of the two operands is True: True
print('not {}:       {}'.format(t, not t))   # Returns the reverse logical state of it's operand: False<jupyter_output>True and False: False
True or False:  True
not True:       False<jupyter_text>Decision MakingWe can create conditional statements in python with the `if` statement:Syntax is:```python if (logical statement is True):  (does something)```or```pythonif (logical statement is True):         (does something)         (does something else)         (... and so on)```        Note the **indentation** when expressing multi-line `if` statements. Every line in python must be in the same **indentation level** or else we would raise an *IndentationError*.<jupyter_code>if (a > b): print('yes')  # because the condition is true it prints 'yes'
if (a < b): print('yes')  # this time around it doesn't print anything because the condition isn't true<jupyter_output>yes<jupyter_text>`if ... else` statements:```pythonif (logical statement is True):         (does something)         (does something else)         (... and so on)else:         (does something if the condition is false)         (... and so on)```<jupyter_code>if (a < b):  # if the condition is true then execute the indented code below
    print('a is smaller than b')  # note the indent
else:  # if the condition is not true then execute the indented code below
    print('a is not smaller than b')<jupyter_output>a is not smaller than b<jupyter_text>`elif` statements:```python if (statement1 is True):         (does something)         (does something else)         (... and so on)elif (statement1 is False but statement2 is True):                 (does something)         (...)elif (statement1 and statement2 are False but statement3 is True):                 (...)else:         (does something if the condition is false)         (... and so on)```<jupyter_code>if (a < b):  # if the condition is true then execute the indented code below
    print('a is smaller than b')  # indented line
elif (a == b):  # if the first condition is not true but this one is then execute the indented code below
    print('a is equal to b')
else:  # if none of the above conditions aren't true then execute the indented code below
    print('a is larger than b')<jupyter_output>a is larger than b<jupyter_text>Nested if statementsThis means having an if statement inside another if statement.```pythonif (statement1):         (does something if statement1 is True)        if (statement2):             (does something if statement1 and statement2 are True)            if(statement3):                 (does something if all three statements are True)            else:                 (does something if statements 1 and 2 are True)                 (but statement3 is False)        else:             (does something if statement1 is True but statement2 is False)else:     (does something if statement1 is False)```    In the above syntax there is never a state that checks if more than one of the statements are `False`. If we wanted to do something if **all three** logical statements were False, we would need to modify the code accordingly. One way to do so would be:```pythonif (statement1):         (same nesting as before)else:    if (not statement2 and not statement3):         (does something if all three conditions are False)```<jupyter_code>if a < 0:
    print('a is negative')
    if a < -100:  # this if condition is checked only if the first condition (a<0) is met
        print('a is smaller than minus 100')
    else:
        print('a is not smaller than minus 100')
elif a == 0:
    print('a is zero')
else:
    print('a is positive')
    if a > 100:
        print('a is greater than 100')
    else:
        print('a is not greater than 100')<jupyter_output>a is positive
a is not greater than 100<jupyter_text>ExampleGiven a roulette number x find out if it is black/red, odd/even, high/low and which set of 12s or column it belongs to:![](http://www.casinonewsdaily.com/wp-content/uploads/2015/02/european-roulette-layout.jpeg)Imagine the roulette table pictured above. Roulette numbers go from 0 to 36. A given number can be either black or red, odd or even (in this case it is the same as being black or red), it can be small (1-18) or large (19-36), it can belong to one distinct set of 12s (1-12, 13-24, 25-36), it can belong of 3 the columns ( 1,3,...,34  /  2,5,...,35  /  3,6,...,36 ) or it can be 0 (which doesn't belong to any of the above categories).<jupyter_code>from random import randint
x = randint(0,100)
# we don't know what x is, but we know it belongs in [0,100].
print('The number is:', x)
# Only numbers in [0,36] are valid roulette numbers, though.
# First lets check to see if our number is a valid roulette number
if x > 36 or x < 0:
    print('This is not a valid roulette number!')
else:
    print('This is a valid roulette number!')
    # Now lets check if it is 0 or not
    if x == 0:
        # actually we don't need to do anything here (because zero doesn't belong in any category
        pass  # with this statement we tell the program not do anything.
        # if we hadn't used pass then we would get an IndentationError 
        # (because python expects an indented statement after colons)
    else:  # or if it is an integer between 1 and 36
        # now lets check the color
        if x % 2 == 0:
            print('This is an even number!')
            print('The color is red!')
        else: 
            print('This is an odd number!')
            print('The color is black!')
        if x > 18:  # we don't have to check if x <= 36 because we are nested inside the first else statement
            print('This is a large number!')
            if x > 24:
                print('This number belongs in the third set of 12s!')
            else:
                print('This number belongs in the second set of 12s!')
        else:  # or if x <= 18
            print('This is a small number!')
            if x > 12:
                print('This number belongs in the second set of 12s!')
            else:  # if x <= 12
                print('This number belongs in the first set of 12s!')
        # Now that we checked if it is large or small and figured out in which
        # set of thirds it belongs to, lets check what column it belongs to:
        if (x % 3) == 0:  # if the number divides perfectly with 3 it belongs in the 3rd column
            print('This number belongs in the third column!')
        elif (x % 3) == 1:  # no need for elif (we could just have used if instead)
            print('This number belongs in the first column!')
        elif (x % 3) == 2:  # same as before
            print('This number belongs in the second column!')<jupyter_output>This is a valid roulette number!
This is an even number!
The color is red!
This is a small number!
This number belongs in the first set of 12s!
This number belongs in the third column!<jupyter_text>ExerciseAs an exercise you can try to write a program that does the exact same thing as the previous example. Try to nest everything in a large if condition that checks for the color.Pseudocode follows:```pythonif (color_check):    if (small/large):        if (...):            ...        else:            ...    else:        ...else:    ...```  Truthy and Falsy valuesIn python most objects have a boolean representation of either<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09522218509856331, 'fraction_numerical': 0.017262501392137208, 'mean_word_length': 2.926541320507215, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '312248', 'license': 'MIT', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2466, 'n_words': 1244, 'path': 'notebooks/06_logical_operations.ipynb', 'repo_name': 'LamprosArgy/python_ml_tutorial', 'size': '8979'}