Python: conditions if, elif, else, match

The essence of the branching algorithm is that there are one or more conditions in the program that will be checked by the program, and depending on whether the condition is met (True - true) or not (False - false), one or another code of the program will be executed.

Conditions in a program are constructed using comparison operators. For convenience, let's present all the operators in a table. Suppose we have 2 variables with given values:
a = 2, b = 5.

Comparison Operators table a = 2, b = 5

Operator Description Example
== Equal. Checks if the values of the operands are equal, if they are, returns True otherwise False. (a == b), results in False.
!= Not equal. Checks if the values of the operands are not equal, if they are not, returns True otherwise False. (a != b), results in True.
> Greater than. Checks if the left operand is greater than the right operand, if it is, returns True otherwise False. (a > b), results in False.
< Less than. Checks if the left operand is less than the right operand, if it is, returns True otherwise False. (a < b), results in True.
>= Greater than or equal to. Checks if the value of the left operand is greater than or equal to the right operand, if it is, returns True otherwise False. (a >= b), results in False.
<= Less than or equal to. Checks if the value of the left operand is less than or equal to the right operand, if it is, returns True otherwise False. (a <= b), results in True.
is Checks if the variables refer to the same object. a = b = 2
print(a is b), results in True.
If we do:
a = b = 2
a = 3
print(a is b), results in False.

Code: Example comparison operators


>>> a = 2
>>> b = 5
>>> c = 4
>>> print(a < c < b) 
True

>>> a = 5
>>> b = 6
>>> print(a < b) 
True

>>> a = 5
>>> b = 6
>>> c = a < b
>>> print(c)
True

            

Logical Operators

Logical operators are used to create more complex conditions. Unlike other programming languages, Python allows you to explicitly create complex conditions, such as a < b < c and so on.

Creating a Table of Logical Operations:

Let's consider two variables: a = 5 and b = -4. We can construct a table to demonstrate the outcomes of applying various logical operators to these values:

Operator Description Example
and Logical AND. Requires all conditions to be met simultaneously to return True. (a > 0) and (b > 0), results in False because the condition b > 0 is not met.
or Logical OR. Does not require all conditions to be met simultaneously. To return True, at least one condition must be True. (a > 0) or (b > 0), results in True because the condition a > 0 is True.
not Logical NOT. Reverses the result of a condition: if it was True, it becomes False, and vice versa. not(b > 0), results in True because the condition b > 0 is False, and we negate it.

Code: Example logical Operators


>>> # and
>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> False and False
False

>>> (5<6) and (7>6)
True
>>> (5>6) and (7>6)
False 

>>> # or
>>> True or True
True
>>> True or False
True
>>> False or True
True
>>> False or False
False 

>>> (5<6) or (7>6)
True
>>> (5>6) or (7>6)
True 

>>> # not
>>> not True
False
>>> not False
True
    
                

if statement

The if statement is a fundamental control flow construct in Python, enabling programmers to execute code blocks conditionally based on the evaluation of a logical expression. It allows for decision-making within the program's flow.

Structure of the if Statement:

Code: Example if statement


# if condition:
#    # Statements to execute if the condition is True

a = 10
if a > 0:
    print('Yes')
    print(a)
print('Buy')

a = -10
if a > 0:
    print('Yes')
    print(a)
print('Buy') 
    
                

else statement

The else statement is used in conjunction with the if statement. The else statement contains a block code that should be executed if the condition returns False.

Code: Example if else statements


# if condition:
#    # Statements to execute if the condition is True
# else:
#    # Statements to execute if the condition is False

a = 2
b = 4
if a > b:
    print('Yes')
else:
    print('No')
print('Buy')
                        
            

elif statement

The elif statement, short for "else if," is an extension of the if statement in Python. It allows programmers to check multiple conditional expressions sequentially within a single if block. This is particularly useful when dealing with multiple mutually exclusive conditions.

Structure of the elif Statement:

Code: Example if elif else statements


# if condition1:
#     # Statements to execute if condition1 is True
# elif condition2:
#     # Statements to execute if condition1 is False and condition2 is True
# elif condition3:
#     # Statements to execute if condition1 and condition2 are both False and condition3 is True
# # ... (more elif statements can be added)
# else:
#     # Statements to execute if all conditions are False

a = int(input("a: "))
b = int(input("b: "))
if a > b:
    print('num а is greater than b')
elif b > a:
    print('num b is greater than a')
else:
    print('numbers are equal') 
                
    

match case statements

A match statement takes an expression and compares its value to successive patterns given as one or more case blocks.

Only the first pattern that matches gets executed and it can also extract components (sequence elements or object attributes) from the value into variables.

Code: match case statements


status = int(input("status: "))            
match status:
case 400:
    return "Bad request"
case 404:
    return "Not found"
case 418:
    return "I'm a teapot"
case _:
    return "Something's wrong with the internet"

Links: Python documentation


[1] Python if-elif-else , the control flow, match case


[2] Python if-elif-else , the control flow, match case