Elif Python with examples

Read Time:4 Minute, 28 Second

Table of Contents

The elif statement allows you to test multiple expressions and run a piece of code when one of the conditions is true. The elif statement, like else, is optional. In contrast to other statements, which can only have one, an if can have an arbitrary amount of elif statements following it.

Elif statement in Python

The syntax of the Elif statement is as follows:

The elif statement also works with an ‘else’ statement that can be used in conjunction with an if statement. When the conditional expression present in the prevalent if statement resolves to 0 or FALSE, the else statement executes a block of code. The else statement is optional, and there can only be one other statement after the if statement.

The ‘if…else’ statement’s syntax is as follows:

What is the original Structure of Statements?

Statements in any Python script are executed sequentially from the first to the last by default. The sequential flow can be changed in two ways if the processing logic warrants it:

SUGGESTED READ

Python’s ‘if’ keyword is used to implement decision control. Following the ‘if’ keyword is any Boolean statement that evaluates to True or False. To begin a block with an increased indent, use the: sign followed by Enter. If the Boolean expression evaluates True, one or more sentences with the same indent level will be performed.

Reduce the indentation to finish the block. Following statements will be executed outside of the if condition. The ‘if’ condition is demonstrated in the following example.

Example: The if Block contains a single statements

Because the expression price 100 evaluates True in the preceding example, the block will be executed. The if block begins on a new line, and all statements within the if condition begins with an increased indentation, either a space or a tab. The if block above has simply one sentence. The if condition in the following example contains numerous statements.

Example: The if Block contains multiple statements

The if condition above contains several sentences with the same indentation. If all statements are not in the same indentation, either space or tab, an IdentationError will be raised. For example, try out the following code.

Statements with the same indentation level as the ‘if’ condition is ignored in the if block. In fact, they will think about getting out of the if predicament.

SUGGESTED READ

Example: Statements out of Block

The following example shows several ‘if’ conditions.

Example: Demonstration of Many ‘if’ Conditions

Each ‘if’ block has a statement in a different indentation, which is correct because they are distinct from one another. For your information, the default indentation level should be four spaces or a tab for improved readability.

Example: if ..else statement

The vital thing to note is that before even using if … else or elif, you first must understand how the if statement works as a standalone statement. Below is the syntax to help you put this into perspective.

In this case, the program analyzes the test expression and executes the statement(s) only if the test expression is True. The statement(s) are not executed if the test expression is False.

The indentation in Python indicates the content of the if statement. The body begins with an indentation and ends with the first unintended line. As a matter of fact, Python considers non-zero numbers to be True. Additionally, None and 0 are considered False.

SUGGESTED READ

Core Python does not include a switch or case statements like other languages. However, we may emulate switch cases with if…elif…statements, as shown below.

Nested if statements in Python

An if…elif…else statement can be contained within another if…elif…else statement. In computer programming, this is known as nesting. These statements can be nested within one another indefinitely. The only way to determine the depth of nesting is by indentation. They can be perplexing, so they should be avoided unless absolutely necessary.

Below, we look at an example of using the nested if statements in Python.

Example: Checking if a given number is either Negative or Positive

Upon running the code snippet above, ‘is a positive number’ is printed when variable var_num is positive. However, if var_num is equal to zero, the value ‘is zero’ is displayed as the output. Additionally, ‘is a negative number’ is printed if var_num is negative.

Example: Using if, elif, nested & else Conditions

Conclusion

Elif is an abbreviation for else if. It enables us to search for numerous expressions. If the condition for if is False, the condition of the next elif block is checked, and so on. However, if all of the conditions are False, the ‘else’ body is executed.

SUGGESTED READ

According to the condition, just one of the numerous if…elif…else blocks is executed. There can only be one other else block in the if block. However, it is possible to have numerous elif blocks.

Also, remember that when we wish to execute code only if a given condition is met, we must make a decision. Python’s if…elif…else statement is pivotal in making decisions.

Source: https://www.codeunderscored.com/elif-python-with-examples/

CyberSEO Pro - OpenAI GPT-3 autoblogging and content curation plugin for WordPress