Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

if-elif and else Condition Statements in Python

The most basic programming conditional structure is the standard if statement. if-elif and else statements in Python are used to check logical conditions of an expression. if-elif and else statements are the selection statements in Python. Programs use selection statements to choose among alternative courses of action. For example, the passing marks in an examination are 50 out of 100. To show if the student is passed look at the following pseudocode.
If student’s marks are greater than or equal to 50:
Print “Student is passed”
In the above example, the ‘if’ part of the statement determines whether the condition “student’s marks are greater than or equal to 50” is true. If true, statement is executed and “Student is passed” is printed. If the condition is false, the print statement is completely ignored by the Python interpreter.

Python uses the Boolean concept of true and false while determining whether to execute the statements or not. If the condition is true, the statements are executed. If it’s false, the statements are not executed.

The above mentioned pseudo-code is written in Python code is as under:

Student is Passed
The above Python code completely corresponds closely to the pseudocode.

Here, one thing must be in your mind that the indentation of the second line is syntactically required. Because in Python,  indentation is a necessary part of code construct and layout. Failure to use a colon (:) sign at the end of the condition expression is a syntax error. Be very mindful as your code with conditional statements.

Now let’s see what happens if student marks are less than 50.

No output, because condition tested as false, and therefore, print statement is totally ignored by the Python interpreter.

Let’s do a little bit more syntax related discussion. You can write your conditional statement in one line if you want. It totally depends on the liking and disliking of the programmer. General rule by most of the programmers is, write single line statement if execution if block is a single line. Whatever you adopt, but it is a good practice to stick with the language standards.

Look at the following code example.

Student is Passed.

We can write our print statement just after the colon (:) which separate the first line to the second one.
You may run across code in this format. However, I think it’s best to use the multiline format with indentation to improve readability, consistency and minimize errors.


The if-else statement in Python Examples and Explanation

The next control structure we will discuss is the if – else conditional statement.
The if statement performs some action only when the condition is true, otherwise the action is completely skipped or ignored by the Python compiler. The if-else is a double selection statement which allows you to execute one or more statement when a condition is true and executes one or more other statements when the condition is false. Look at the following pseudo-code statement to understand the concept.

If the student marks are greater than or equal to 50
print “Student is passed”
print “Student is failed”

It is clear from the above statement that if the student marks are greater than or equal to 50, the program prints “Student is passed”. On the other hand, by the addition of else statement, we now capable of printing “Student is failed”, if the student marks are less than the passing marks i.e. 50. Now we convert the above pseudocode into Python programming instructions or code.

Student is passed

Now we take an example where condition becomes false and the second action is performed.

Student is failed

Here note that the statement of the “else” part is also indented which is compulsory in Python and syntactically required. Missing out the colon (:) after else is also a syntax error.


Nested if-else Statement in Python with Examples and Explanation

Now we take an example where we test multiple conditions by using if–else statement inside other else–if statements to create nested if–else statement.

Student scored grade D

Explanation of if-else in Python:

If the variable StudentMarks is greater than or equal to 90., the first four condition in the nested if-else statements are true but only the first statement will execute. After the execution of that statement, the remaining part of the outermost if-else statement is skipped or ignored.
Nested if-else statements look nasty and complex to handle. But there is an easy way of elif statements to do things as we are able to do in nested if – else statement. In the Following section, we will discuss elif statements.


elif Statement in Python with Examples and Explanation

Multiple statements can also be tested by elif statements. The following code is the alternate to the above example of nested if – statement.

Student secured grade D

Explanation of if-elif in Python:

The above code does the same thing as nested if-else statement structure do. This code looks clean and easy to handle. It is a preferable method for all programmers to test any expression in Python. Some beginner level programmers use if-else nested structure to test their logical expression but it is not the recommended way. It doesn’t follow the general programming standards of all levels as well.  All comes to personal choice of a programmer but it should follow the standards and should be readable.

As you may have noticed, nested if-else structure is not easy to read and if structure nesting level is deep then it is quite cumbersome and complex. A general thumb rule is, “follow the standard programming rules at the start of your programming career of any language and it will definitely fruit you at all level”. Happy programming.

LearnAndLearn.com © 2018 - All rights reserved