Comprehensive Python

Table Of Contents:

Python Program Flow Control

Welcome guys to this other part of our tutorial series whereafter we will be able to give our program ability to execute specific blocks of code, had they passed a specific propotional check. In this part of the series, we will write a calculate program, where we will compute/calculate based on the user option.
The IF Statement

As the word suggests, if is used to check if an item passess a specific operation.
The code block will only execute if a given condition is true, otherwise, the interpreter will not executed it.

The good example for this is when we make conditional sentences in english as:
If it rains today, I wont go to school.
This sentence exactly explains how conditional statemets in Python work.

In python, an if statement has the following syntax:

>>> if test:
...     # your block here
...     # another block here

If you notice well, you will know that the block after the for test is indented inside the for itself and not on the same column.
All the blocks covered by for must start on the same column but not same column as for itself.

To understand better how to apply this in real life, we will invent our new simple mathematics program.
Taken a primary school student wants to do some mathematical calculations, so we give him/her an option as to what he would like to do as:

            Select what you would like to do?
                1. Addition
                2. Subtraction
                3. Multiplication
                4. Division
        

The Student then will select what he wants to do, based on a particular problem and based on his choice, we will help him with the calculation.

In that scenario however, here is how we can work it out in python.

# Print the headers to make program intuitive
>>> print("Select what you would like to do?\n")
>>> print("\t1. Addition \n\t2. Subtraction \n\t3. Multiplication \n\t4. Division")
# Take user option/selection from keyboard input
>>> option   = input()
# Then let him enter the numbers for the calculations
>>> number1 = input("Alright then, kid, enter the first number: ")
>>> number2 = input("Yes, great! enter the other number: ")

Then based on the user selection, we will do the calculations as using our if statements as:

# Then check the option, and execute block based on given option
>>> if option == 1:
...     print(number1 + number2)
... if option == 2:
...     print(number1 - number2)
... if option == 3:
...     print(number1 * number2)
... if option == 4:
...     print(number1 / number2)

Looking closely at the above program, we will see lots of if's in repetition. But as in our previous english sentence, we said:
If it rains today, I wont go to school. Else, I wont be absent for a sec.
Then how are we supposed to build statements like this in python?
That's where an else phrase comes into the scene.

The IF... ELSE Statement

As the word suggests, an else is the final statement of the if condition.
This means after the if, the final statement of that conditional statement a listener will expect is else.

If the test on if fails, then else part will be executed at all costs.
The else part works as the default block, as can be seen below:

# Check the truthfulness
>>> if option == 1:
...     print(number1 + number2)
... else:
...     print("Option unknown")
In the above code, the test is for two items only, so that means if firstcondition_not_true then else do_the_other.

The ELIF Statement

But consider where we have alot of tests, that is more than two conditions. Keep repeating if's gets annoying to the listener.
How can we avoid that, that's where we call elif into the ground.
To those familiar with other programming languages, elif in python is a version of else if in other languages.
Below is a proper to use elif in python in our previous program:

# Then check the option, and execute block based on given option
>>> if option == 1:
...     print(number1 + number2)
... elif option == 2:
...     print(number1 - number2)
... elif option == 3:
...     print(number1 * number2)
... elif option == 4:
...     print(number1 / number2)

In the example above, we only use if and elif for the tests, but consider where a user enters option that is not on the tests.
In this case, we use default, which is else, the default, which when all other tests fail, then user must have set invalid option.
Take this as an option to tell the user that he should opt a valid option. as in:

# Then check the option, and execute block based on given option
>>> if option == 1:
...     print(number1 + number2)
...
... elif option == 4:
...     print(number1 / number2)
... else:
...     print("The Option Must Be between 1 and 4")

In so doing, say a user enter 6, then the else part/block will be executed telling him that he must choose 1 to 4.

Ladies and Gentlemen, that's all about program flow control. In the next topic, we will look on Loops