Conditional Execution
Programming gains its power through the flexibility to react to different inputs and circumstances. A desk calculator might be able to go through a complicated series of steps in order to calculate for example a square root, but it is not a computer.
The standard way to achieve flexibility in what a computer can perform is the introduction of alternate execution, and in particular the if statement. The if-statement in Python consists of a Boolean expression and one or more lines of code (a "code block")
It consists of the keyword "if" followed by a Boolean expression (an expression that can be True or False), followed by a colon. In a new line, but indented by one tab, there is a statement that is only executed if the Boolean expression is True.
Boolean expressions have to evaluate to either True or False. Python actually follows a convention that non-zero numerical arguments, non-empty strings, and many other objects evaluate to True, but this is at this point only a curiosity.
The code to be executed if the Boolean is true has to be indented by a tab. Depending on the text editor that you use, indentation might be automatic. You can either use a tab or a number of periods. The Python style sheet proposes 8 white spaces but that seems excessive to many. Python wants all the indentations to be the same, so mixing white spaces and indents will cause problems. Fortunately, the IDLE text editor will standardize and you will not have to worry about it.
The flexibility to guard code with an if-statement by itself is not enough, and Python has the if-else statement.
The two conditional blocks in the if-else statements are executed depending on the truth value of the Boolean condition. If the condition is true, then the block under the if is executed, if it is falce, then the block under the else is executed.
Python allows you also to check for cases in the if-elif-else statement. elif is a contraction of else if, and the name describes accurately its meaning.