Branching with if, else and elif
One of the most powerful features of programming languages is branching: the ability to make
decisions and execute a different set of statements based on whether one or more conditions are
true.
The if statement
In Python, branching is implemented using the if statement, which is written as follows:
if condition:
statement1
statement2
The condition can be a value, variable or expression. If the condition evaluates to True, then the
statements within the if block are executed. Notice the four spaces
before statement1, statement2, etc. The spaces inform Python that these statements are
associated with the if statement above. This technique of structuring code by adding spaces is
called indentation.
Indentation: Python relies heavily on indentation (white space before a statement) to define code
structure. This makes Python code easy to read and understand. You can run into problems if you
don't use indentation properly. Indent your code by placing the cursor at the start of the line and
pressing the Tab key once to add 4 spaces. Pressing Tab again will indent the code further by 4
more spaces, and press Shift+Tab will reduce the indentation by 4 spaces.
For example, let's write some code to check and print a message if a given number is even.
,The else statement
We may want to print a different message if the number is not even in the above example. This can
be done by adding the else statement. It is written as follows:
if condition:
statement1
statement2
else:
statement4
statement5
If condition evaluates to True, the statements in the if block are executed. If it evaluates
to False, the statements in the else block are executed.
, The elif statement
Python also provides an elif statement (short for "else if") to chain a series of conditional blocks.
The conditions are evaluated one by one. For the first condition that evaluates to True, the block of
statements below it is executed. The remaining conditions and statements are not evaluated. So, in
an if, elif, elif... chain, at most one block of statements is executed, the one corresponding to
the first condition that evaluates to True.
In the above example, the first 3 conditions evaluate to False, so none of the first 3 messages are
printed. The fourth condition evaluates to True, so the corresponding message is printed. The
remaining conditions are skipped. Try changing the value of today above and re-executing the cells
to print all the different messages.
To verify that the remaining conditions are skipped, let us try another example.
Note that the message 15 is divisible by 5 is not printed because the
condition a_number % 5 == 0 isn't evaluated, since the previous condition a_number % 3 ==
One of the most powerful features of programming languages is branching: the ability to make
decisions and execute a different set of statements based on whether one or more conditions are
true.
The if statement
In Python, branching is implemented using the if statement, which is written as follows:
if condition:
statement1
statement2
The condition can be a value, variable or expression. If the condition evaluates to True, then the
statements within the if block are executed. Notice the four spaces
before statement1, statement2, etc. The spaces inform Python that these statements are
associated with the if statement above. This technique of structuring code by adding spaces is
called indentation.
Indentation: Python relies heavily on indentation (white space before a statement) to define code
structure. This makes Python code easy to read and understand. You can run into problems if you
don't use indentation properly. Indent your code by placing the cursor at the start of the line and
pressing the Tab key once to add 4 spaces. Pressing Tab again will indent the code further by 4
more spaces, and press Shift+Tab will reduce the indentation by 4 spaces.
For example, let's write some code to check and print a message if a given number is even.
,The else statement
We may want to print a different message if the number is not even in the above example. This can
be done by adding the else statement. It is written as follows:
if condition:
statement1
statement2
else:
statement4
statement5
If condition evaluates to True, the statements in the if block are executed. If it evaluates
to False, the statements in the else block are executed.
, The elif statement
Python also provides an elif statement (short for "else if") to chain a series of conditional blocks.
The conditions are evaluated one by one. For the first condition that evaluates to True, the block of
statements below it is executed. The remaining conditions and statements are not evaluated. So, in
an if, elif, elif... chain, at most one block of statements is executed, the one corresponding to
the first condition that evaluates to True.
In the above example, the first 3 conditions evaluate to False, so none of the first 3 messages are
printed. The fourth condition evaluates to True, so the corresponding message is printed. The
remaining conditions are skipped. Try changing the value of today above and re-executing the cells
to print all the different messages.
To verify that the remaining conditions are skipped, let us try another example.
Note that the message 15 is divisible by 5 is not printed because the
condition a_number % 5 == 0 isn't evaluated, since the previous condition a_number % 3 ==