Newby Coder header banner

Python Conditions


Decision making statements in Python

Decision making statement consists of a header containing an expression which is evaluated, and a corresponding block (or body or suite) which gets executed when the header evaluates to true

If the body is a single line then it can appear in the same line as header, otherwise it has to be an indented block

if hungry == True:
    get_food()
    eat_it()

In the above example, if hungry == True: is the header of an if condition

The indented block following it represents its body, which gets executed if the header evaluates to True

Python programming language assumes any non-zero and non-null value as True, and if it is either zero or None, then it is assumed as False

Python provides following types of decision making statements

Statement Description
if statements

An if statement consists of a boolean expression followed by one or more statements

elif statements

elif statement comes after an if statement or another elif statment, and gets executed when the corresponding if condition (and prior elif conditions) evaluates to False

elif statement also consists of a boolean expression followed by one or more statements

else statements

An else statement appears as the last conditional statement of a chain of conditional statements, which executes when none of its prior conditions evaluates to True

It doesn't consist of a boolean expression (and acts as an encapsulation of other possible known and/or unknown condition(s))

Python if Statement

An if statement (also if clause) consists of a boolean expression followed by one or more statements

If the block of an if clause consists only of a single line, it may go on the same line as the header statement

Here is an example of a one-line if clause -

#!/usr/bin/python
var=100
if(var%10 == 0):
    print("Value is divisible by 10") 

When the above code is executed, it produces the following output :

 Value is divisible by 10 

Python elif Statement

elif is short for else if, which appears after an if condition and can be followed by more elif conditions

It allows to check for multiple expressions, as there can be multiple elif conditions

Expression of an elif statement is evaluated only if the if condition(and every other elif condition, before the current elif statement, of the same conditional flow or chain) evaluates to False

Syntax

if test_expression1:
    Body of if
elif test_expression2:
    Body of elif
elif test_expression3:
    Another elif
...

Body of the first condition which evaluates to True, is executed

Corresponding conditions following it are skipped

Example of Python elif statement

Following example checks divisibility of a number based on whether the remainder is 0

def check_if_divisible(n):
    if n % 2 == 0:
        print(n, "is divisible by 2")
    elif n % 3 == 0:
        print(n, "is divisible by 3")
    elif n % 5 == 0:
        print(n, "is divisible by 5")
    elif n % 1 == 0:
        print(n, "is divisible by 1")

check_if_divisible(15)
check_if_divisible(25)
check_if_divisible(49)

Output

15 is divisible by 3
25 is divisible by 5
49 is divisible by 1

Python else statement

An else statement appears as the last conditional statement of a chain of conditional statements, whose body gets executed when none of its prior conditions evaluates to True

Thus it doesn't consist of a boolean expression (and acts as an encapsulation of other possible known and/or unknown condition(s))

An if block can have only one corresponding else block, but multiple elif blocks

Syntax

if test_expression1:
    Body of if
else:
    Body of else 

With elif

if test_expression1:
    Body of if
elif test_expression2:
    Body of elif
...
else:
    Body of else 

Example of Python else statement

def check_if_divisible(n):
    if n % 2 == 0:
        print(n, "is divisible by 2")
    elif n % 3 == 0:
        print(n, "is divisible by 3")
    elif n % 5 == 0:
        print(n, "is divisible by 5")
    else:
        print(n, "is not divisible by 2,3 or 5")
        print("Go ask a for loop")

check_if_divisible(15)
check_if_divisible(25)
check_if_divisible(49)

Output

15 is divisible by 3
25 is divisible by 5
49 is not divisible by 2,3 or 5
Go ask a for loop

Python Nested if statements

if (along with corresponding elif and else) statements can be put inside body of another if, elif or else statements

This is called nesting in computer programming

The level of nesting can be figured by indentation

Python Nested if Example

Following example checks whether a number is positive, negative or 0

# define function to check number
def check_number(num):
    if num >= 0:
        if num == 0:
            print(num, "is the zero")
        else:
            print(num, "is a positive number")
    else:
        print(num, "is a negative number")

check_number(0.1)
check_number(-5)
check_number(0)

Output

0.1 is a positive number
-5 is a negative number
0 is the zero