if-else in Python

an illustration of if-else in programming. It allows flow controls for a program based on checking of conditions

The road of life has countless of forks. At each of those, you decide which turn to take depending on facts that you know. If you like computer science, you will go to one college, but if you like business, you go to another. Life is full of if-else‘s. And similarly, programming is also full of if-else. Be it you do software engineering, data analytics, security, whatever branches, you will inevitably use if-else. With that being said, this post will give you a quick introduction on if-else in programming and in Python. You can download the notebook here.

if-else in Programming

All programming languages have their if-else structures, also called conditional structures. Conditional structures allow a program to execute different logics depending on a check. Basically, it is the way for you to tell your program that “if something is something, go ahead and do this action; else if it is something else, you need to do another action”. If-else structures provide your program with infinitely more flexibilities and capabilities.

Data analytics also relies on numerous applications of conditional structures. One example, in predictive analysis, you can automate a model to keep training as long as its prediction accuracy is lower than a certain acceptable value like 90%. When the model surpasses 90% accuracy, it automatically stop training and saves its states so you can reuse for future data.

So to reiterate, if-else is a highly important concept to grasp in both programming and data analytics. Now, let us learn about if-else in Python, starting with the simple if statement.

The simple if

The simple if structures allow us to write codes that execute if a condition is satisfied and otherwise do nothing. For example, you want to have a variable x acts as the denominator of a division. However, when the x is 0, you gets an error message

With the use of if, you can tell Python to calculate the division only if x is not 0. As you can see from the code and output below, we no longer get the error when x is set to 0. The reason is that the check x is not 0 fails which trigger Python to not perform the division.

Now let see how to write the simple if in Python. The syntax is as follows

In short, when Python reaches the if statement, it will first check the <condition expression>. If the expression is correct (True), Python will run everything in the block of codes. On the other hand, if the expression is wrong (False), Python will skip everything in the block of codes and go to the next part in the program. Now, you may have noticed, in the syntax, the block of codes is indented to the right with respect to the if statement. This is a very important detail, so I will spend a whole section discuss it.

Indentations in Python

Indentations are very highly critically tremendously important in Python. They decide the structure of your codes. Consecutive lines of codes with the same indentation belong to a same block. If a block of codes is right-indented compared to a statement before it, the block belongs to the statement. Belonging has different meanings depending on the parent statement. In if-else structure, it means the block will or will not be executed depending of the check of the parent statement.

Let us examine the two cells below. The first cell does not have any output, because print() is put inside the block if by its indentation. The second cell remove the indentation from print() which brought it to the same level as if, and will be executed regardless of the if outcome.

Next, let us discuss a more complete version of if structures, the if-else structure.

if-else Structure

The if-else statement allows us to add actions for the False case of the expression check in if. It allow you to tell Python to execute some codes if a condition is satisfied and otherwise execute some other codes The syntax is as below

In this case, Python first check the <condition expression>. If the expression is correct (True), Python will execute everything in the block of True. If the <condition expression> is wrong (False), the codes in the block of False will be executed instead. To understand this concept, you can play with the value of x in the code below and see how the output becomes.

if-elif-else Structure

Unlike the if-else statement which only allows two outcomes, the if-elif-else statement can check a chain of conditions. The syntax of this structure is as follows.

Each <Expression> in the structure will be evaluated in the order that they appear (1, 2, 3, etc.). If an expression is correct, its corresponding block of codes will be executed, and the rest of the expressions and codes are skipped. The block of the else statement is executed only when all the previous expressions are False. You can have as many <Expression> as you want, and the else statement is optional.

An application of this logic that you will see a lot in data analytics is the binning of values. For example, you want to assign groups to patients in a data set based on their ages like below 20, 20-30, 30-40, 40-50, and above 50. The codes may look like below.

Now, try to practice by yourself with the code like changing the values of age of each age threshold. You can even add more or remove some elif statements and see what the results become.

Conclusion

I guess that this is a fairly long post, however, there are a lot about condition structures and we have just covered some surfaces. So, please take your time to understand all the concepts and logics, practice a bit so you can write some simple if-else. We will definitely see them again in the future and hopefully you will be ready at that point!