Python if else statement

In this tutorial, you will learn about flow controlling flow and decision making using Python if else statement.

If else: Introduction
If statement
If else statement
If .. elif .. else statement
Nested if else statement

python if else statement

Python if else: Introduction


Python if elseĀ are decision-making statements that facilitate us to make a decision between true/false or multiple options by imposing a particular condition.

Why do we need if else statements?

Basically, in a simple program, statements are executed from top to bottom in a sequence. Sometimes we need to change the flow of the program or maybe conditionally execute the particular statements in a program.

For these, Python has if statements, for loop, while loop and goto statements.

Here we will discuss Python if else statements.

Python if statement


Python if statement is used to conditionally execute a block of code. If statement contains a logical expression or condition, depending in which decisions are made.

Python if statement syntax

if expression:
  statement(1)
  statement(2)
  ...........

As seen in syntax, first the expression is checked and if it returns True statement(s) will be executed, else they are not executed and are skipped.

Unlike other programming languages, instead of curly braces '{ }', indentation is used to indicate the body of if statement.

Flowchart of Python If statement

python if statement flowchart
[adsense1]

Python if statement example

Let’s take the simplest example to understand if statement in Python.

a = 2
if a<3:
  print (a);
if a>3:
  print 'Hi'

Output

2

Here, first if statement returns TRUE, since a is less than 3 and the body of if statement is executed. In second if statement, it returns FALSE as a is not greater than 3, so Hi is not printed in output console.

Python If else Statement


if else statement has two blocks of codes, each for if and else.

Python if else statement syntax

if test_expression:
  statements
else:
  statements

The block of code inside if statement is executed if the test_expression in if statement is TRUE, else the block of code inside else is executed.

Flowchart of Python if else statement

python if else statement flowchart

Example of Python if else

x = 5
if x<10:
  print 'Condition is TRUE'
else:
  print 'Condition is FALSE'

Output

Since the test expression x<10, it will execute the block of code inside if statement.

Condition is True

Python if .. elif .. else statement


So far in if statement and if else statements, we can only check one condition or expression.

What if we need to check multiple conditions to make a decision?

For this, Python has if .. elif .. else statement. elif is the short form of else if.

Syntax of Python if .. elif .. else statement

if test_expression1:
   statements(1)
elif test_expression2:
   statements(2)
elif test_expression3:
   statements(3)
else:
   statements(4)

As seen in above syntax, we can check multiple test_expressions to make decisions in if .. elif .. else statements.

Flowchart of Python if .. elif .. else statement

Here is the flowchart of if .. elif .. else statement for its better understanding.

python if else if flowchart

Example of Python if .. elif .. else statements

x = 5
if x == 2:
   print 'x is equal to 2'
elif x < 2:
   print 'x is less than 2'
else:
   print 'x is greater than 2'

Output

x is greater than 2

Explanation: Here, the program tests first expression x ==2 which returns FALSE and then it tests expression in elif (x < 2), which also returns FALSE. So, the program executes the block of codes inside else statement.

Python nested if else statement


In nested if else statements, if statement is nested inside an if statements. So, the nested if statements will be executed only if the expression of main if statement returns TRUE.

Syntax of nested if else in Python.

if test_expression:
  if test_expression:
     block of code
  else:
     block of code
else:
  block of code

This syntax shows an if else statement nested inside an if else statement.