In this tutorial, we will learn about the Python if…else Statement , or its types, and how to use them with the help of examples.

Decision making in Python structures have one or more conditions to be evaluate or test by the program, along with a statement or statements that are to be execute if the condition is determine to be true, and optionally, other statements to be execute if the condition is determine to be false.

Types of Python if…else Statement

  • If Statement
  • If-else Statement
  • Nested If Statements
  • elif statement

If Statement

This statement is use to test a particular condition and if the condition is true, it executes a block of code is know If Statement.

Flow Diagram in If Statement

Flow Diagram in If Statement
Flow Diagram in If Statement

Syntax of If Statement

if condition:
   # Statements to execute if
   # condition is true
Syntax of If Statement

The Example of If Statement

Input Value

num = int(input("enter the number?"))  
if num%3 == 0:  
    print("odd number") 
Example of If Statement

Let’s run the code

Example of If Statement in Python
Example of If Statement in Python

If-else Statement

This statement provides an else block combine with the if statement which is executes in the false case of the condition. If the condition is true, then the if-block is execute. Otherwise, the else-block is execute.

Flow Diagram in If-else Statement

Flow Diagram in If-else Statement
Flow Diagram in If-else Statement

Syntax of If-else Statement

if (condition):
    # Executes this block if
    # condition is true
else:
    # Executes this block if
    # condition is false
Syntax of If-else Statement

The Example of If-else Statement in Python

Input Value

age = int (input("Enter your age? "))  
if age>=18:  
    print("You are eligible to vote !!");  
else:  
    print("You are not eligible to vote !!");  
Example of If-else Statement in Python

Let’s run the code

Example of If-else Statement in Python
Example of If-else Statement in Python

Nested if Statement

This statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.

Flow Diagram of Nested if Statement

Flow Diagram of Nested if Statement in Python
Flow Diagram of Nested if Statement in Python

Syntax of Nested if Statement

if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here

Syntax of Nested if Statement

The Example of Nested if Statement in Python

Input Value

i = 25
if (i == 25):
    
    if (i < 35):
        print("i is smaller than 35")
          
    if (i < 37):
        print("i is smaller than 37 too")
    else:
        print("i is greater than 35")
Example of Nested if Statement in Python
Example of Nested if Statement in Python
Example of Nested if Statement in Python

elif statement

This statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them.

Flow Diagram of elif Statement in Python

Flow Diagram of elif Statement in Python
Flow Diagram of elif Statement in Python

Syntax of elif Statement in Python

if expression 1:   
    # block of statements   
  
elif expression 2:   
    # block of statements   
  
elif expression 3:   
    # block of statements   
  
else:   
    # block of statements 
Syntax of elif Statement in Python

The Example of elif Statement in Python

number = int(input("Enter the number?"))  
if number==80:  
    print("number is equals to 85")  
elif number==70:  
    print("number is equal to 79");  
elif number==86:  
    print("number is equal to 84");  
else:  
    print("number is not equal to 85, 79 or 84");  
Example of elif Statement in Python
Example of elif Statement in Python

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about Python Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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