Python If Else Statement Tutorial

Python If Else Statements

This is a detailed tutorial of the If-Else Statement in Python. All of its four types, if, if-else, nested if-else, and if-elif ladder 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 is can also be used without else statement but 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 their 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 else block. So, you need not define the else block here.

The syntax for simple if statement is given below.

The following example shows the use of 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.

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.

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 that 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 of the cases. Therefore, if block code will be executed when the condition is True but if its false, the code inside the else block will be executed.

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

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

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.

Have a look at the following code.

In the above code, you can see there is another set of if-else statements inside the else block of a 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 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 the 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 statement as we make a ladder of conditions. The syntax of the same is illustrated below.

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 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.

The output for the above code is shown below in the screenshot.

Python If Elif Ladder Example

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.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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