Python Conditional Statements

Decision making is an important aspect of programming. We often want to execute a certain piece of code based on certain expressions. This is done by if..elif..else block in python. The general syntax for conditional statement is as follows:

If condition method 1

if Conditional_expression:
    body_of_if_block

If condition method 2

if Conditional_expression:
    body_of_if_block
else:
    body_of_else_block

If condition method 3

if Conditional_expression:
    body_of_if_block
elif Conditional_expression2:
    body_of_elif
... 
else:
    body_of_else

In method 1, the body has only one if statement. if the Conditional_expression  value evaluates to True. The if block code is executed, otherwise the block is skipped.

Conditional_statement can be an expression which evaluates to True or False, or it can be a value. If the expression has values then all the digit are evaluated to True except 0 which evaluates to False. Like wise empty string and None is also evaluated to False.

In method 2 if the if-block is evaluated to True it will executed code inside if-block otherwise the else-block code is executed.

In method 3: if the if block is True the code inside the if is executed and all other blocks are skipped (elif and else both). If the if block evaluates to False the code moves to elif block and checks the condition if the condition is True elif-block code is executed, otherwise it moves to other blocks and so on.

Single if statement

single if block has only one block that test the expression. Then normal syntax is :

if Conditional_expression:
    body_of_if_block  

Note Conditional_expression can also be wrapped inside parenthesis like this

if (Conditional_expression):
    body_of_if_block  

These both syntax are correct in python.

Example

fruit_count = 100

if (fruit_count==100):
    print('There are 100 fruits')
There are 100 fruits

Since the fruit_count is equal to 100, the expression in the if-block is evaluated to True and thus , code inside if block is executed.

Also, as we have said None, 0 , and ‘ ‘ (empty string) evaluates to False and thus it skips the execution of code inside that block. All other decimal and string values evaluates to True and code is executed. Let’s see an Example:

# value with some value
x = 10
if (x):
    print("if block with some value, x = ",x)

if (1):
    print('if with 1 in expression')

string_Value = 'Hello'
if (string_Value):
    print('we have string =',string_Value)

# This if block will be skipped
z = None
if (z):
    print('if block with None')

# This will also be equivalent to if (False), so skipped
if (0):
    print('if with 0')

# This will not be executed
val = ''
if (val):
    print('empty value block')

Output

if block with some value, x =  10
if with 1 in expression
we have string = Hello

As we can see the None, 0 and empty string if-block are all skipped while, the 10, 1 and ‘hello’ string blocks are executed.

if – else statement

The if else statement consist of two block one with if and one with else. First condition in the if block is tested if the if-block expression evaluates to True , the else part is skipped. Otherwise,if the if-block is expression evaluates to False, the else part is executed.

Syntax:

if Conditional_expression:
    body_of_if_block
else:
    body_of_else_block

Example

x = 10 
if (x==20):
    print('x is 20')
else:
    print('x is not 20 x = ',x)

Output

x is not 20 x =  10

Here, since the x =10 comparing it with 20 results in False, therefore the else block code is executed.

Nested if..elif..else statement

The nested if..elif..else block has multiple block which is checked one after another. If the block in if evaluates to True the if -block code is executed otherwise, the checking moves to elif part and check the same , the block which evaluates to True is executed and all other blocks are skipped.

Syntax:

if Conditional_expression:
    body_of_if_block
elif Conditional_expression2:
    body_of_elif
... 
else:
    body_of_else

Example:

x = 10
if (x <= 5):
    print('x less than 5')
elif(x <= 15):
    print('x is  greater than 5 and less (or equal to) 15')
elif(x <= 50):
    print('X is greater than 15 and less than or equal to 50')
else:
    print('x = ',x)

Output:

x is  greater than 5 and less (or equal to) 15

Ternary operator in python

A lot of other programming languages like java, c, c++, javascript, etc. have ternary operator, which has expression like this

expression ? statement1 : statement2

if the expression evaluates to True, the statement1 part is executed otherwise,statement2 is executed.

Python does not have ternary operator. However, similar result can be achieved by if..else block.

Syntax:

statement1 if expression else statement2

Example:

x=10
print('x = 10') if (x == 10) else print('x not 10')

print('x is 20 ') if (x == 20 ) else print('x is not 20')

Output:

x = 10
x is not 20

Leave a Reply

Your email address will not be published. Required fields are marked *