Python if…else Conditional Statement (With Examples)

We would have seen most protagonists utter the words ‘No ifs & buts…..’ in the movies, to get things done in the only way they want. It’s all fine in the movies but in reality, things don’t work that way! There shall be a truckload of ‘ifs’ & ‘buts’ thrown at your face in every real-life situation you face. That leaves programming as no exception.

But the thing about programming is that it assists you in navigating through these ‘ifs’ (They don’t seem to like ‘buts’ I suppose). In this article, we shall look into the if-else statement in Python and its variations alongside examples. Let’s get started with some rudimentary concepts such as knowing what exactly the if-else statement is destined to do.

Python if…else Statement

The if-else statement contains a condition against which the input data would be compared to find whether the condition is met or not. Once the condition is met, the line of codes following the ‘if’ shall be executed, whilst in the event of not being met, those following the ‘else’ shall be run. The condition generally is constructed using logical operators viz. <, >, !=, ==, <= or >=.

Flowchart of Python if…else Statement

Logic Of If Else Statement
The logic of the if-else statement

Forms of Python if…else Statement

Like a good old villain, the if…else statement offers a choice for the course of the program to pick up & this essentially has helped it gain its alias as the selection statement. Let us now get on with the different variants of the if…else statement that exists within Python.

  • Simple if statement
  • if…else statement
  • Nested if…else statement
  • if…elif…else statement

Python if Statement

Just as the name indicates this statement is decluttered from the else portion & only contains the if part. So, when the condition is not met, the line of codes following the if shall be skipped and the program is run with the subsequent lines of codes following the if statement.

Example:

Y = 1990
if Y<=1999 and Y>=1990:
    print('Hello there, you 90s kid')

It could be seen that following the if there seems to be an indentation. This is a prerequisite that ought to be followed while constructing any variant of the if statement. Following is the result when the above code is run.

Output:

Result Of Simple If Statement
Result of simple if statement

Python if…else Statement

This statement has both plan A & plan B for the program to choose from depending on whether it meets the specified condition or not. The following example explains it better.

Example:

Y = 2006
if Y<=1999 and Y>=1990:
    print("Hello there, you '90s kid")
else:
   print("It's sad you weren't born in the '90s" )

Output:

Result Of The If Else Statement
Result of the if-else statement

It could be observed since the input falls out of the range specified in the if condition, the else block is executed. The same can also be done using the below code in a shorter form.

Y = 2006
print("Hello there, you '90s kid") if Y<=1999 and Y>=1990 else print("It's sad you weren't born in the '90s" )

Output:

Short Form Of If Else Statement
Short form of if-else statement

Python Nested if…else Statement

At times it is not sufficient to get things done with one else block. This necessity paved way for the construction of nested if…else statements. The example below shows how a typical nested if…else statement shall work.

Example:

Y = 1986
if Y>1999:
    print("It's sad you weren't born in the '90s")
   if Y>1990:
        print("Hello there, you '90s kid")
else:
   print("How was life in the '80s?" )

Output:

Result of Nested If Else Statement
Results of nested if-else statement

Python if…elif…else Statement

This is Python’s gift to those who are sick & tired of typing multiple else’s and ifs. The if…elif…else statement effectively combines both the else & if statements together so that it is kept short and not repeated often. It serves the same purpose as the nested if…else statement albeit in a compact manner. Let’s have a look at an example.

Result For Elif Statement
The result for elif statement

Conclusion

Hope this article has thrown light upon the different variants of the if…else statement that exists within Python. Here’s another article emphasising the importance of Python for traders. There are numerous other interesting and equally informative articles in CodeforGeek that would be of great help for you to gain valuable insights. Until then, ciao!


Reference

https://docs.python.org/3/tutorial/controlflow.html

Arulius Savio
Arulius Savio
Articles: 26