Python if…else Statement

Video Tutorial
FREE
This video belongs to
Python and SQL for Data Science
8 modules
Certificate
Topics Covered

Overview

The decision-making statements are a crucial part of programming as it does give the flow of execution a direction to flow into. Hence, condition checking is the backbone of decision-making, and the python if-else statement becomes handy to help us execute the same. We move forward to decide whether a block of statements will get executed or not. If the if condition around the block of the statements is TRUE, then it will be executed; otherwise, the else block of the statements will be executed.

What Is the Need for if-else Statements in Python

Decision-making statements or conditions in programming languages determine the flow of code execution and its direction. For example, to check whether a number is even or odd and segregate them into two categories, we can use if-else statements in Python. By applying the condition to check for even and odd numbers, we can segregate them accordingly. Python supports various mathematical logics, which are often used while implementing if-else statements to check for conditions in different scenarios.

Various mathematical logics are supported in python, which we often use when we are implementing if else in python to check conditions around scenarios as shown below:

• Equals: Scenario where we want to check, when variable a is equal to variable b represented as a == b.
• Not Equals: Scenario where we want to check when variable a is not equal to variable b represented as a != b.
• Less than: Scenario where we want to check, when variable a is less than variable b is represented as a < b.
• Less than or equal to: Scenario where we want to check, when variable a is less than or equal to variable b represented as a <= b.
• Greater than: Scenario where we want to check, when variable a is greater than variable b represented as a > b.
• Greater than or equal to: Scenario where we want to check, when variable a is greater than and equal to variable b represented as a >= b.

Python if Statement Syntax:

Quick Note: Indentation, the whitespace at the beginning of a line, plays an important role while working with if else in python programming. It is often used to define the scope of the program, while in other programming languages, we often make use of curly brackets to serve the purpose. It is important to take proper care of the indentation while coding with if - elif - else; otherwise, you will encounter IndentationError while executing the code.

Below is an example where proper indentation is not followed in the last print statement. Hence we observe that the code has thrown an error as the compiler considers the last print statement to be out of sync with the above followed indented print statements. This leads to the IndentationError, which could have been avoided if proper indentation had been followed.

Various Decision-Making Statements in Python

Below are various combinations of if else in python where we shall study seven methods to implement them in our code when we encounter any decision-making scenario.

If Statement

The simplest decision-making statement used in Python is the if statement. The if statement is significant in Python as it executes the statement in the if block when the condition is true. In Python, all non-zero values are considered true while values like None and 0 are considered false. It is essential to note that it is possible to have a single if statement without an else block, but having an else block without an if statement is not possible in Python.

Syntax:

As seen above, the code will evaluate if the expression is TRUE or FALSE. If it is satisfied, then the if statement will execute the block of statements below it; otherwise not. When the test expression is FALSE, then the block of statements is not executed. We can also use condition by using the bracket ( ).

FlowChart: Below is the flowchart for understanding the concept learned above for the if statement as follows:

The flow of Python program is determined by a test expression, which is validated to decide the program's proper flow. If the test expression is true, the program moves to the body of the if statement and executes the statement block indented below it. It's essential to note that if the statement block isn't properly indented, it will execute regardless of the test expression being true or false. When the test expression in the if statement is false, the program jumps out of the if statement, as shown in the chart.

Code:

Output:

If Else

The if statement executes a block of statements only if the condition is true, but if it's false, nothing is executed. To address this, we use the if-else statement in Python, which executes the else statement when the condition is false. Proper indentation is necessary for the if-else statement.

Syntax: The syntax for the if-else statement is as below:

As seen above, the code will evaluate if the expression is TRUE or FALSE. If it does get satisfied, then the if statement will execute the block of statements below it. When the test expression is FALSE, the else statement block of statements is executed. We use indentation to separate the blocks.

FlowChart: Below is the flowchart for understanding the concept learned above as follows: The code enters the test expression, and if it's true, it executes the statements in the if block based on indentation. Improper indentation leads to statement execution regardless of the validation. When the test expression is false, the code executes the else block based on indentation.

Code:

Output:

Nested If

In Python, we can use nested if statements to account for multiple possibilities when coding. Nested if statements involve placing an if statement within another if statement. Proper attention to indentation is crucial when implementing nested if statements, especially when dealing with multiple blocks of nested if statements.

Syntax: The syntax for the if statement is as below:

The code uses if statements to execute a block of code if a condition is true. It can also contain nested if statements, which evaluate conditions within the initial if statement. The code uses indentation to separate the different blocks of code.

FlowChart: Below is the flowchart for understanding the concept learned above as follows:

The chart shows how the program's flow is determined by a test expression. If the test expression is true, the program checks a nested test expression. If that is also true, it executes the nested if statements; otherwise, it executes the nested else statements. If the first test expression is false, it executes the else statement below it.

Code:

Output:

The if-elif-else statement checks all if statements in order, and if none of them are true, it moves to check the elif statements. When a condition is true, the block of statements under that if statement is executed, and the rest of the ladder is bypassed. If none of the conditions are true, the else statement is executed.

FlowChart:

Below is the flowchart for understanding the concept learned above as follows:

The chart shows that the program's flow is determined by various test expressions in the if-elif-else statements. If any of the test expressions is true, the block of statements under that if or elif statement is executed. If none of the test expressions are true, the else statement's block of statements is executed.

Syntax: The syntax for the If-elif statement is as below:

The code evaluates whether the expression is true or false. If it is true, the if statement's block of statements is executed. If the first expression is false, and expression2 is true, the elif statements are executed until one of them is true. If none of them are true, the else statement is executed. Indentation separates the blocks.

Code:

Output:

Short Hand If

The shorthand if statement is used when we only want a single statement to be executed inside the if block. It is written on the same line as the if statement, and indentation is not important. The shorthand if statement is easier to write and allows the developer to focus on the code rather than proper alignment.

Syntax: The syntax for the Short hand if statement is as below:

As seen when we are evaluating the shorthand if statement, we always implement it by placing the statement that must get executed after the expression holds TRUE, and this statement must be placed on the same line.

Code:

Output:

Short Hand If-Else

We implement the shorthand if else statement when there is only one statement that needs to be executed in both the if and else blocks. Here the if-else statements are presented in a single line.

Syntax: The syntax for the Short hand if-else statement is as below:

As seen when we are evaluating the shorthand if else statement, we always implement it by placing the statement that must get executed after the expression holds TRUE, and this statement must be placed on the same line. Also, when the condition doesn't hold TRUE, then the else statement will get executed.

Code:

Output:

Conclusion

• To make decisions in the python programing language we make use of the conditional statement that is, if-else in python, and related to which we saw various ways to implement the same.
• Minimal code is used when we want to execute conditional statements by simply implementing the declaration of all the conditions and statements in a single statemenat while executing the code.
• All the combinations that we studied in the article were created by the combinations of if-elif-else lops where:
• The if condition is implemented when one of the conditions is true or false, and we want to print the output when our conditions are satisfied.
• The Elif condition is implemented when we have a second possibility as the output. Also, we can even have multiple elif conditions to verify for the 3rd, 4th, 5th, and 6th possibilities in our program
• The else condition is implemented when we estimate the failure possibility in our code. To avoid encountering any exception or to give the code a solution while encountering a failure possibility, we use this statement.