From now on, you can learn about conditional statements in Python. Learn what you can do with IF, ELSE and ELIF and how they can be used to manage decisions inside your program.
What are conditional statements? Basically, it expresses code that is being executed only when a certain condition has been fulfilled. They are also known as “IF statements“. It can be explained by the following sentence: IF this has happened, DO the following, ELSE, do the other thing.
The keyword IF is being used to define the condition, followed by the action that needs to be done. It is possible to define an alternative action in case the condition is not fulfilled. The alternative action is defined after the ELSE keyword. The syntax in Python is as follows:
if <condition>: <primary action> else: <alternative action>
Please note that the above code example contains pseudo-code inside angle brackets (<>). Pseudo-code is a common practice to show the idea of the code without implementing real functioning logic. Also, always make sure the condition is ended with a colon (“:”). This tells the computer that the definition of the condition has ended and the definition of the actual code starts. Omitting the colon will result in errors.
With the help of this conditional statement, it is possible to change the flow of the program, depending on the state of some event or element. The if statement can be divided into two major parts: the condition and the actions that are executed.
The condition can be a simple variable or an expression. The condition will be checked whether it is True or False. This means that the type of the condition is a boolean. So, the condition can be a variable in which a True/False value has been stored or it can be the result of an expression like a comparison or even the result of multiple comparisons combined with the keywords AND or OR. Here are two examples:
battery_is_full = True if battery_is_full: <primary action> else: <alternative action> sensor_left = 0.2 sensor_right = 0.5 if (sensor_left < 0.1 AND sensor_right < 0.1): <primary action> else: <alternative action>
The AND and OR keywords are elements for binary logic that will verify the state of several boolean states combined. The OR will result in True if at least one of the elements is True and the AND will result in True if all elements are True.
The next part is the action that should be executed when the condition is true. This can be any type of code. The only thing that you need to pay attention to is that the code representing the action must be indented. This means that there are some white spaces on the left side of the code compared to the line indicating the condition. This is to represent that the code is inside a code-block. These blocks are essential for the computer to understand which part still belongs to the actions under the if statement and which excluded from the if statement. This means that the blocks are necessary for the logical structure of the program, but also they keep the code organized. It is rather simple to see which part belongs to the action under the conditional statement and which does not. The non-indented code will then be executed regardless of the condition.
It is even possible to have another if statement inside the action part of an if statement. This procedure is called nesting. A nested if statement would be as follows:
battery_is_full = True battery_voltage = 4.8 if battery_is_full: if (battery_voltage > 4.5): <nested action> else: <alternative nested action> else: <alternative action>
Nesting blocks inside each other is a very common technique to make more complex decision algorithms. This can lead to some difficult to read and difficult to understand code which is more likely to contain logical mistakes. You could try to break the nested structure into smaller pieces:
battery_is_full = True battery_voltage = 4.8 if (battery_is_full AND battery_voltage > 4.5): <primary action> else: <alternative action> if (battery_is_full AND battery_voltage < 4.5): <primary action> else: <alternative action>
In the above example, there is no nested structure anymore, making the code a little more readable. However, there are some disadvantages. What would happen if the battery_is_full variable would be False? Then, the conditions for both if statements would be false and for both cases, the code under the ELSE part would be executed even though in the nested part, there was only one ELSE that would be executed. To differentiate the cases where battery_is_full = False would be covered, you would need to have four conditional statements under each other. You see, the code would be more explicit, but also more lines of code and more to type in order to cover the same logic. Also, you can see that the conditions are now more complex than with the nested structure. These are some things that you might want to consider before writing the code.
The final part of the conditional statement is the alternative code that is being executed. It is defined after the ELSE keyword followed by another indented block of code. This block defines the code that is being executed if the condition is False and the first code block is not executed. This part is not mandatory and can be skipped.
battery_is_full = True battery_voltage = 4.8 if (battery_is_full AND battery_voltage > 4.5): <primary action> if (battery_is_full AND battery_voltage < 4.5): <primary action>
There is one last thing that can be used to make the code more efficient. In the case above, the two conditional statements are verified even though they are mutually exclusive, meaning if one is true, the other can’t be true as well. This means that the second statement is obsolete when the first one is already being executed. (To be fair, for the example above, this makes no difference, but when the conditions are getting much more complex and there are many more situations to verify, this can be time-consuming for the computer.) To avoid this, there is a combination of IF and ELSE that helps you combining the advantage of a nested structure with the advantage of a flat code structure: ELIF. The ELIF part can be used between the IF and the ELSE part of the conditional statement. There can be as many ELIF parts as you need. The ELIF keyword is followed by another condition, just like the IF keyword in the first part.
battery_is_full = True battery_voltage = 4.8 if (battery_is_full AND battery_voltage > 4.5): # battery is full <primary action> elif (battery_is_full AND battery_voltage < 4.5 AND battery_voltage > 3.3): # battery is still usuable <alternative action 1> elif (battery_is_full AND battery_voltage < 3.3): # battery start to be empty, set battery_is_full to False <alternative action 2> else: # battery is empty <alternative action 3>
The above structure keeps the code somehow organized and with the comments, it is rather easy to keep track of all possible statements. Also, the list of different cases can go on and on without creating a nest in a nest in a nest… There is no one-size-fits-all solution for code, but sometimes one solution has more advantages. In general, you can go for the more structured method over the more efficient method as most computers are very efficient. And the more structured the code is, the easier it is to understand, modify and maintain.