DEV Community

Cover image for If statements in Python
Python64
Python64

Posted on

If statements in Python

With Python conditional statement or if statements, the condition (True or False) determines if to execute a code block or not.

The Python programming if statement is in the form of:

    if the condition is determined:
        Execute the statement ......
    else:
        Execute the statement ......

If the condition is not True, the latter statement is executed. Else the other code is executed. Indention used to create code blocks. The else statement is optional.

If new to Python, you may like this Python book

If statement example

The following specific examples:

# 1: if the basic usage                                                                                                                                                                  

flag = False
name = 'python'
if name == 'python': # variable determines if condition True or False                                                                                        
    flag = True # flag is set to true conditions are satisfied                                                                                                                           
    print('welcome boss') # and the greeting message                                                                                                                                     
else:
    print("hello employee")

The output is:

>>> welcome boss

The condition of the if statement can have > (greater than), >= (greater than or equal), <= (less than or equal). For equality use ==.

For a set of comparisons:

    if Condition 1 is determined:
        Statement 1 is executed ......
    elif judgment condition 2:
        Execute the statement ...... 2
    elif judgment condition 3:
        Execute the statement 3 ......
    else:
        Execute the statement 4 ......

Examples are as follows:

# 2: elif usage                                                                                                                                                                          

num = 5
if num == 3:
    print('boss')
elif num == 2:
    print('user')
elif num == 1:
    print('worker')
elif num <0: # output value is less than zero                                                                                                                                            
    print('error')
else:
    print('conditions met') # conditions are met                                                                                                                                         

The output is:

conditions met

Python does not support the switch statement, so multiple conditions can only be achieved with elif.

# 3: plurality of condition statements

num = 9
if num >= 0 and num <= 10: # determines whether the value is between 0 and 10
    print('in range')

num = 10
if num < 0 or num > 10: # determines whether the value is less than 0 or greater than 10
    print('out of range')
else:
    print('in range')

num = 8
# Determines whether a value between 0 to 5 or 10 to 15
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
    print('in range')
else:
    print('out of range')

Related links:

Top comments (0)