if-else Elif nested if in Python with examples

Python python-loops

In this post, we are going to understand if-else Elif nested if in Python with examples

What are Python if-else statements


In programming, language code executes in sequential order one by one. What if we need to execute the code block after evaluating some expression and execute the code block on the basis of the evaluation result of the expression. All this could be performed using the control flow statement.

Types of Python if-else statement


  • if
  • if-else
  • if……Elif…….else
  • Nested-if

Note: In Python Non-null or Non-Zero values are considered as True, Zero or Null values consider as False.

1. What is Python if Statement


if statement is a basic decision control statement. It uses when we need to execute some code statement only when the condition is true, Else nothing is performed.

Syntax

if a statement evaluates an expression if the evaluation of the expression result is TRUE. “code statement ” will be executed otherwise nothing will be executed.

if expression:
 ..code statement 
  • Expression: This expression returns TRUE or False.
  • Code body: It is the code inside if block will execute if the condition is true.

Example Python if statement

num1 = 10

num2 = 20
 
if num1%2==0 :

  print('num is even number')

if num2>num1 :

 print('num2 is greater than num1')

if num1<num2:

 print('num2 is less than num1')

Output

num is even number
num2 is greater than num1
num2 is less than num1

2. Python if..else statement


“If” statement executes some code block, if the condition is true, else statement executes code that is in “else” block if the expression is evaluated to false.

Syntax

if expression:

  code body of if 

else:

  code body of else 

The expression for if is evaluated to TRUE then “code body of if ” will execute, on False then “code body of else” will execute.

Code Example : Python if-else Statement

num1 = 10
num2 = 20
 
if num1%2==0 :
  print('num is an even number')
else:
  print('num1 is not an even number')
if num2<num1 :
 print('num2 is less than num1')
else:
 print('num2 is greater than num1')

Output

num is an even number
num2 is greater than num1

3. Python if……Elif…….else Statement


In programming languages like C#, Java, “else if” is indicated by the else -if statement, In python if-else is indicated by the Elif keyword.

Elif stands for else-if in Python. It is used when needs to evaluate multiple expressions. In this example, we only have one if-else block but multiple Elif blocks.

Python if…elif…….else statement syntax

if expression:

 code for if block

elif expression:

 code for elif block

else:
   code block else block

The expression for if is evaluated to False then elif expression is evaluated then next elif expression is evaluated and so on if all condition is False then else code block will be executed.

Example Python if…elif…….else statement

x = 10
y = 20
 
if y<x :

  print('y is less than x')

elif x>y:

  print('x is greater than y')

elif x==11:

  print('x is 11')

else:

  print('none of condition is true')

Output

none of condition is true

4. Python Nested If statement


Whenever in code, if statement inside another if statement is called then this is called as nested if statement

Python Nested if statement syntax

if expression:

  if expression
    code for inside if-block go here

  else:
    code for inside if-block go here

else:
  code for else-block
 

Example Python Nested if statement

x = 10
y = 20
 
if y<x :

  print('y is less than x')

  if x==10:

   print('x value is 10')

  else :

   print('x is value not 10')

else:
  
 print('y is greater than x')

Output

y is greater than x

Conclusion:

We have understood if..else, if..elif..else decision control statement in Python Decision flow statements in Python with code example. We hope you find this useful.