Control Flow in Python

Control Flow in Python

If, Else, Elif, Complex Boolean Expressions

Lol, I did it again. I know I mentioned that the first step in learning programming is to make sure that you are coding at least everyday. Well that didn't work out so well the past two weeks. I was in a different headspace and honestly, trying to look at the terminal wasn't appealing at all. Additionally, I pressured myself into churning out articles at least 3 times a week.

However, in my first article, I also insisted on the importance of taking a break. So instead of kicking myself for not being able to get on track, I sought it fit to just take a breather and come back when my mental state is in check. And back I am baby! Let's crack on with Control Flow in Python.

If Condition

The if statement checks whether the block of code evaluates to True. It is a boolean expression. If the condition is True, the code is executed. If the condition is not met by the if clause, nothing is executed. The statement should end with a colon :

It is important to note that the block of code beneath the if statement is written in an indented format. Failure to which, the code will not run. To be certain, the indent is exactly four spaces from the margin. Using the 'tab' button to indent code is not ideal and may result in the code not running.

In this example, I set a condition where if my car fuel level was below 15 litres, my vehicle should get an additional fuel of 50 litres. At the same time, my bank would debit Kshs. 5,000 to cater for this addition in fuel.

if conditions.png

In the event that my fuel level is not below 15 litres, the block of code below the if statement will not be executed.

Else Condition

In cases where the if condition is not met, we may want to execute another block of code. Here we use else. The else condition is also a boolean expression and is terminated using a colon :. This condition does not need to be indented by a block of code beneath it as it will be executed since the first if statement was not met. For example:

else condition 1.png

In this example, the user is prompted to enter their age as a string. The condition that has been set in the if statement is that the user will be eligible to drink if they are 18 years and above. However, we can see the use of the else condition when the initial statement is not met.

If a user inputs any age below 18 years, the message You need to exit the premises will be displayed.

Elif Condition

Elif is short for else-if. This condition is used when there is more than one condition to be checked or evaluated. The example below checks for different job requirements for a Master's finalist who is applying for various jobs.

elif condition.png

Noteworthy, I have used the comparison operator == instead of an assignment operator =. If your code returns errors, you may want to check which operator you have used.

The else clause is applied at the end of the code. It will be executed when all other clauses evaluate to False. It does not require a condition/block of code.

Complex Boolean Expressions

I honestly need more practice in this because I could already see how useful such expressions would be in evaluating multiple conditions at the same time.

For complicated conditions, one can combine ands, ors and nots. Usage of parentheses is encouraged as it makes your code more comprehensive. In my example, I combined my if statement with and.

complex boolean expressions.png

I will definitely revisit these to get a better understanding of how they can be implemented. For now, I'm stocked that I can comprehend what exactly is going on. On to the next concept.

Cheers!