Python if else statement

Rumman Ansari   Software Engineer   2023-03-28   6091 Share
☰ Table of Contents

Table of Content:


  • What you will learn in this section:
    1. What is decision making in python?
    2. Syntax of if statement in python
    3. Indentation in Python
    4. Example with coding in Python

    They’re some situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions, we will execute the next block of code. Decision-making statements in programming languages decide the direction of the flow of program execution.


    Syntax of if statement

    Below you can see that this is syntax of if statement.

    if condition:
       statement1 
    statement2
    

    Indentation: Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.

    An "if statement" is written by using the if keyword.


    Flow chart: Simple if

    if conditionals in Python have the following forms.

    Python if Statement

    Example 1: If statement:

    
    a = 33
    b = 200
    if b > a:
      print("b is greater than a")
    
    

    Output:

    The above code will produce the following result-

    
    b is greater than a
    
    

    In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 30, and b is 200, we know that 200 is greater than 30, and so we print to screen that "b is greater than a".


    Example 2: if statement

    Below you can see that this is syntax of if statement.

     PI = 3.14
    radius = 2.1
    
    if (radius > 0) :
      area = radius * radius * PI
      print("The area for the circle: ",area)
    
     The area for the circle:  13.8474
    

    Is indentation required in Python?

    Indentation is necessary for Python. It specifies a block of code. All code within loops, classes, functions, etc. is specified within an indented block. It is usually done using four space characters. If your code is not indented necessarily, it will not execute accurately and will throw errors as well.


    If else in Python Programming Language

  • What you will learn in this section:
    1. What is if else in python?
    2. Syntax of if else statement in python
    3. Flowchart of if else statement in python
    4. Example of if else with coding in python

    Syntax: if else statement

    Below you can see that this is syntax of if else statement.

     if (condition):
        # Executes this block if
        # condition is true
    else:
        # Executes this block if
        # condition is false
    

    Flow chart: Simple if

    if else conditionals in Python have the following forms.

    Python if else Statement

    Example 1: if else statement

    Below you can see that this is example of if else statement.

     # python program to illustrate If else statement
    
    PI = 3.14
    radius = -2
    
    if (radius > 0) :
      area = radius * radius * PI
      print("The area for the circle: ",area)
    else:
      print('Input is not valid')
    
     Input is not valid
    

    Example 2: if else statement

    Below you can see that one another example of if else statement.

     # python program to illustrate If else statement
      
    i = 20
    if (i < 15):
        print("i is smaller than 15")
        print("i'm in if Block")
    else:
        print("i is greater than 15")
        print("i'm in else Block")
    print("i'm not in if and not in else Block")
    
     i is greater than 15
    i'm in else Block
    i'm not in if and not in else Block
    

    Example 3: if else statement

    Below you can see that one another example of if else statement. Declare a variable i and check that it is positive number or negative number.

     i = int(input())
    
    if(i > 0):
        print("Positive no")
    else:
        print("Negative no")
    
     -12
    Negative no
    


    The if-elif conditional statement

    Syntax

    Syntax:
    if :
      Statement
       [statements]
    elif :
      statement
       [statements]
    else:
      statement
      [statements]
    
    Python if-elif conditional statement

    Example: Elif

    Code:The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".
    
    a = 33
    b = 33
    if b > a:
      print("b is greater than a")
    elif a == b:
      print("a and b are equal")
    
    

    Output:

    The above code will produce the following result-

    In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".

    
    a and b are equal
    
    

    Example: Else

    Code: The else keyword catches anything which isn't caught by the preceding conditions.

    
    a = 200
    b = 33
    if b > a:
      print("b is greater than a")
    elif a == b:
      print("a and b are equal")
    else:
      print("a is greater than b")
    
    

    Output:

    The above code will produce the following result-

    In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b".

    
    a is greater than b
    
    

    Example: Else

    Code: You can also have an else without the elif:

    
    a = 200
    b = 33
    if b > a:
      print("b is greater than a")
    else:
      print("b is not greater than a")
    
    

    Output:

    The above code will produce the following result-

    
    b is not greater than a