(Python) Nested conditional statement. Difficult conditions


Nested conditional statement

In "if" blocks and "otherwise" may include any other statements, including other nested conditional statements; the word  else refers to the nearest previous if.
 
Example 
if A > 10:
    if A > 100:
        print("You have a lot of money.")
    else:
        print("You have enough money.")
else:
    print("You don't have enough money.")
Bold indicates a conditional statement that is inside another if statement, which is why it is called a nested conditional statement. With nested conditional statements, you can implement multiple choices, not just two.
You can also use a nested operator after the word else.
 
Example 
if A < 10:
    print("You don't have enough money.")
else:
    if A > 100:
  print("You have a lot of money.")
  else:
  print("You have enough money.")
In this case, if after else one more condition needs to be checked, then instead of the if operator, you can use "cascading" branching with the keyword elif (short for else - if).
 
Example
if A < 10:
    print("You don't have enough money.")
elif A > 100:
  print("You have a lot of money.")
else:
  print("You have enough money.")
Pay attention to the indentation in all examples. When using a cascade condition, all if-elif-else keywords are at the same level.
With a large number of checks written using a cascading condition, for example, in the if-elif-elif-... chain, the first true condition is triggered.

Complex Conditions

The previous problem can be solved in a shorter way using complex conditions. 
Let's understand what complex conditions are.

The simplest conditions consist of one relation (greater than, less than, etc.). But sometimes it is necessary to combine simple conditions into more complex ones. For example, it is cold outside and it is raining. Two simple conditions (it's cold outside), (it's raining outside) are connected here by the copula I.
 
COMPLEX CONDITION - consists of two or more simple relations (conditions) that are combined using logical operations :
  AND - logical multiplication - written in Python as and, < br />   OR - logical addition - written in Python as or,
  NOT - logical negation - in Python written as not.

Operation AND - requires simultaneous fulfillment of two conditions:
  condition 1 and condition 2   - will evaluate to true only if both simple conditions are true at the same time,
moreover, if condition 1 is false, then condition 2 will not be checked

The operation OR - requires at least one of the conditions
  condition 1 or  condition 2   - will evaluate to false only if both simple conditions are false at the same time,
moreover, if condition 1 is true, then condition 2 will not be checked

Operation NOT 
 not condition 1 - will evaluate to false if condition 1 is true and vice versa
For example, the following two conditions are equivalent:   A>B     and    not (A<=B)

 

Priority of execution of logical operations and relations
  1. Operations in brackets
  2. NOT operation
  3. Logical relationships >, <, >=, <=, ==, !=
  4. And operation
  5. OR operation
Parentheses are used to change the order of actions.

 
Boolean variables
In many programming languages, it is possible to use variables that store boolean values ​​("true"/"false"). In Python, such variables can take the values ​​True (true) or False (false). For example, the program  
a=True
b=False
print(a or b)
Displays True.
Boolean variables are of type bool, named after the English mathematician George Boole, the creator of the algebra of logic.