In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Branching statements, sometimes also referred to as conditional statements or Selection statements, give us this ability. The simplest form of branching is the if statement. This is sometimes referred to as binary selection since there are two possible paths of execution.
if BOOLEAN EXPRESSION:
STATEMENTS_1 # executed if condition evaluates to Trueelse:
STATEMENTS_2 # executed if condition evaluates to False
The boolean expression after the if statement is called the condition. If it is true, then the immediately following indented statements get executed. If not, then the statements indented under the else clause get executed.
The if statement consists of a header line and a body. The header line begins with the keyword if followed by a boolean expression and ends with a colon (:).
The more indented statements that follow are called a block. We will see this construction repeated several times in the future. Indent is 4 spaces in Python and must be same for each line. In your Python interpreter, you can easily indent lines using the tab key on your keyboard.
As a program executes, the interpreter always keeps track of which statement is about to be executed. We call this the control flow, or the flow of execution of the program. When humans execute programs, they often use their finger to point to each statement in turn. So you could think of control flow as "Python’s moving finger".
Control flow until now has been strictly top to bottom, one statement at a time. We call this type of control sequential. In Python, flow is sequential as long as successive statements are indented the same amount. The if statement introduces indented sub-statements after the if heading.
Back to the example, each of the statements inside the first block of statements is executed in order if the boolean expression evaluates to True. The entire first block of statements is skipped if the boolean expression evaluates to False, and instead all the statements under the else clause are executed.
There is no limit on the number of statements that can appear under the two clauses of an if statement, but there has to be at least one statement in each block.
Each compound statement includes a heading and all the following further-indented statements in the block after the heading. The if - else statement is an unusual compound statement because it has more than one part at the same level of indentation as the if heading, (the else clause, with its own indented block).
Python will always execute either the if-block (if the condition is true) or the else-block (if the condition is false). It would never skip over both blocks.
Although TRUE is printed after the if-else statement completes, both blocks within the if-else statement print something too. In this case, Python would have had to have skipped both blocks in the if-else statement, which it never would do.