Python If Else Statements

This is a detailed tutorial on the If-Else Statement in Python. All of its four types, if, if-else, nested if-else, and if-elif ladders are illustrated.

Use of Python If-Else Statement

If-else statements are used to do the decision-making in any programming language. In python also, you can do any kind of decision-making using the if-else statement. If the statement can also be used without an else statement but the else statement always has to be followed after an if statement. You can write your code in if and else blocks and according to your provided conditions, any one of the blocks will be executed.

This way you can run different blocks of codes for different functionalities according to different parameters like different user inputs, different calculation results, etc. The If-else statements work according to boolean values. You have to write an expression that results in a boolean value in this statement i.e. either True or False. We’ll have a better understanding of this concept with the help of examples, later in this article.

The If-else statement is further categorized according to its usage pattern.

  • If Statement
  • If-else Statement
  • Nested If-else Statement
  • If-else-if Ladder Statements

Each of the above four listed categories of the If-Else Statement in Python is explained in detail below with the help of syntax explanation and examples.

If Statement

The simplest decision-making statement is the solo If Statement. This allows you to run a block of code only if the condition provided by you is True. If the condition is False, this block is simply skipped from execution rather than shifting control to the else block. So, you need not define the else block here.

The syntax for a simple if statement is given below.

if condition:
    #Write Your Code Here
    #This will execute only if the condition is True

The following example shows the use of a solo if statement. We’ve defined the value for a variable x and then we’re checking if the value of x is less than 10 or not in the condition, written in the if statement. As its value i.e. 5 is less than 10, so the condition here is True and the code in the if statement block will be executed.

x = 5
if x < 10:
    print("The value of x is less than 10")

If Statement Example

If the value of x might have been 10 or more, the print statement written inside the if block would not have been executed. But all the statements that are written after the ending of the if block will execute normally. You can also use multiple if conditions in your program. Have a look at the following example.

x = 5
if x < 10:
    print("The value of x is less than 10")
if x >= 10:
    print("The value of x is either 10 or greater than 10.")
print("The program has finished its execution.")

If Condition Control Flow Example

In the above code, I’ve written two If statements and one print statement after the ending of the block of the second If statement. As the value of x is 5 is less than 10, so the first block code will be executed and the code written inside the second block will not be executed as the condition for it is False. Also, the print statement that is written after the ending of the second block also executes normally.

If-Else Statement

Rather than just using the If statement, we often prefer to make use of the If and else statements together. As the condition can be True or false, we should be able to handle the program flow in both cases. Therefore, if block code will be executed when the condition is True but if it’s false, the code inside the else block will be executed.

The syntax for the if and else statements is given below.

if condition:
    #If the Condition is True
    #Code of this block will be executed
else:
    #If the Condition is False
    #Code of this block will be executed

The following example illustrates the basic usage of the if and else statements together.

x = 5;
y = 10;

if x > y:
    print("The value of x is greater than y.")
else:
    print("The value of x is either equal to or greater than y.")

If Else Statement Example

As the value of x is not greater than y, so the code of the else block is executed. This is exactly what you can see in the screenshot of the example output given above. The print statement inside the else block has been executed.

Nested If-else Statement

You can use If-else statements inside other if-else statements, as many times and as deeply nested as you want. In other words, If-else statements, inside another and another if-else statement, and so on. The syntax of the nested if-else arrangement is given below. This is just one possible arrangement, there could be many others.

if condition1:
    #Code written here executes if the condition1 is True
    if condition2:
        #Code written here executes if the condition2 is True and condition1 is True
    else:
        #Code written here executes of the condition 2 is False and condition1 is True
else:
    #Code written here executes if the condition1 is False

Have a look at the following code.

x = 5;
y = 10;

if x > y:
    print("The value of x is greater than y.")
else:
    if x == y:
        print("The value of x is equal to y.")
    else:
        print("The value of x is less than y.")

In the above code, you can see there is another set of if-else statements inside the else block of an if-else decision-making statement. As the value of x is not greater than y, so the condition is False and the control comes to the outer else statement. Now, there are two conditions, either the value of x will be equal to y or maybe less than y. Therefore, we used another set of if-else statements inside the outer else block.

The if statement of the inner block will run if the value of x and y are equal and otherwise the code of the inner else block will be executed, which is the case. As the value of x i.e., 5 is less than 10, therefore, the print statement inside the inner else block i.e. “The value of x is less than y.” is printed on the output screenshot as shown in the screenshot given below.

Nested If Else Example

If-elif Ladder Statement

You can also check multiple conditions separately inside one set of if-else-if and these statements are known as Ladder If-else statements as we make a ladder of conditions. The syntax of the same is illustrated below.

if Condition1:
    #Code To Run if Condition1 is True
elif Condition2:
    #Code To Run if Condition2 is True and Condition 1 is False
elif Condition3:
    #Code To Run if Condition3 is True and Condition1 & Condition2 are False
...
...
...
else:
    #Code To Run if all conditions, Condition1, Condition2, Condition3 .... are False

Note. The keyword for laddering conditions check here is elif rather than else if. In most of the other programming languages, the keyword is else if, so don’t get them confused with the Python elif keyword.

Let’s take the same example here as well but now we’ll do the decision-making quicker and with less code.

x = 5
y = 10
if x == y:
    print("x is equal to y.")
elif x < y:
    print("x is less than y.")
else:
    print("x is greater than y.")

Note. You can use any of the above-explained decision-making statements anywhere in your Python program, even inside the flow control and iteration statements like Python For Loops.

So, this way you can easily do decision-making in python using the if-else statements.  I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

Leave a Reply

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