Table of Contents
What are conditional statements in python?
Generally we are writing straight line programs in which statements are executed one after another till end of line character.
Conditional statements are also known as branching statements or decision making statements.
In conditional statements an expression or condition is tested and depending on the test condition it returns true or false than the respective block of code is executed.
The conditional statements has three main parts:
- Test : In this expression to be tested is mentioned.
- Block for True condition : It consists of block of code to be executed when condition is True .
- Block for False condition : It consists of block of code to be executed when condition is False. It is and optional block of code.
Python programming consists of various types of decision making statements.
Conditional statements in python:

if statement
This is the most simple and easy decision making statement in python programming. In this, if statement will check for certain condition and if the condition is true then it will execute block of code written in it. If the condition is false then it will not execute block of code.
Like while loop if block also accepts boolean value which means if we enter 0 as condition then it will be considered as false and non-zero value will be considered as true.

Syntax
Here is the simple syntax for if statement −
if (condition): code block if condition is True
Example 1
""" Author : ITVoyagers (itvoyagers.in) Date :9th August 2020 Description : Program to show use of if statement """ if 0: print ("if condition is true i will be printed") print("if condition is false i will be printed")
OUTPUT

Example 2
""" Author : ITVoyagers (itvoyagers.in) Date :9th August 2020 Description : Program to show use of if statement """ i=int(input ("Enter a Number")) if i<15: print ("Condition is True") print(i, "is less than 15")
OUTPUT

if-else statement
In this is we have two separate blocks of code and one of them get executed based on certain condition. We have if and else two separate blocks, if block checks the condition and if the condition is true then if block will get executed and if the condition is false then else block will get executed.
In simple word there are two separate blocks of code and which block will get executed depending on condition mentioned in if’s parameter.
Like while loop if block also accepts boolean value which means if we enter 0 as condition then it will be considered as false and non-zero value will be considered as true.

Syntax
Here is the simple syntax for if..else statement −
if (condition): code block if condition is True else: code block if condition is False
Example 1
""" Author : ITVoyagers (itvoyagers.in) Date :9th August 2020 Description : Program to show use of if...else statement """ if 0: print ("if condition is true i will be printed") else: print("if condition is false i will be printed")
OUTPUT

In below example we have take a value i from user, then in if parameter we are checking if value of i is less than 15 or not.
Example 2
""" Author : ITVoyagers (itvoyagers.in) Date :9th August 2020 Description : Program to show use of if...else statement """ i=int(input ("Enter a Number")) if i<15: print ("Condition is True") print(i, "is less than 15") else: print("Condition is False") print(i, "is greater than 15")
OUTPUT

if-elif-else statement
We use elif block for multipath decision making. In this is we have many separate blocks of code and one of them get executed based of certain condition. We have three block starting with if block, elif block and else block. First if block will check if mentioned condition is true or not, if the condition is true then if block will get executed and if the condition is false then pointer shifts to very next elif block. elif block will check for its condition, if the condition is true then elif block will get executed and if the condition is false then pointer shifts to very next else block.
In simple words if block and elif blocks will check for certain condition one by one and who ever’s condition get satisfied first then respective block is executed, and if no condition is satisfied then last block i.e. else block will get executed.
We cannot use elif block and else block without if block.
Like while loop if block and elif block also accepts boolean value which means if we enter 0 as condition then it will be considered as False and non-zero value will be considered as True.

Syntax
Here is the simple syntax for if…elif…else statement −
if (condition): code block if condition is True elif (condition 2): code block if condition 2 is True else: code block if both conditions are False
Example 1
""" Author : ITVoyagers (itvoyagers.in) Date :9th August 2020 Description : Program to show use of if...elif...else statement """ a=int(input("Enter a number")) if a<0: print("Number is negative") elif a>0: print("Number is positive") else: print("Number is zero")
OUTPUT

For other python basics related posts:
For other advanced python related posts: