Python if else


Control flow statements like Python if-elif-else can be used to make decisions in Python on the basis of a Boolean Condition.


Decision Statement: When the Boolean Condition following if keyword is True, the program control will flow to the enclosed block of code, else skip over it.


Python If-Elif-Else Syntax

    if condition:
        statements
    elif condition:
        statements:
    ...
    else:
        statements
                                

Note: else is not mandatory with if or after elif.

Joining Python If Conditions

and, or can be used to join condition whereas not can be used to negate the condition.


Example:

    if condition1 and condition2:
        ...
        
    if condition1 or condition2: 
        ...
        
    if not i%2==0:
        ...
        
    if var: # !=None
        ...
        
    if "hello".isupper():
        ...