Logical Flow
If Statements
An if statement specifies the part of the code that runs only when a given condition is met. This allows a programmer to control the logical flow and execution of a computer programme. If statements consist of
if
keyword,- a Boolean condition followed by a colon (
:
), and - an indented code block.
The following is a definition of Python if statement.
if condition:
# code in this block runs only when "condition" is True
# code outside the block always gets executed
The indented code block under the if condition:
executes only when the condition
is equal to True
. Let’s see it in an example.
Run the code with different numbers (
int
orfloat
) for thevalue
.
I used comparison operations as conditions for the statements above. You can use
any Boolean expression that produces True
or False
as a condition of the
statement.
The code above uses multiple checks of the same value which is
redundant. Once we check that a value is not larger than 42, we also know that
the value must be either less than or equal to 42. You can use else
statement
when you have an alternative code that runs when the condition is False
, as
shown below.
if condition:
# runs when "condition" is True
else:
# runs when "condition" is False
if-else
uses two different code blocks. Only one block is executed
depending on whether a condition is met or failed.
In the above, we are still checking the value twice using two separate if
statements. These if statements potential print redundant information, e.g.,
when value = 42
. Also, it is not immediately clear whether these if
blocks are
logically connected without carefully reading the code first.
Python allows us further combine these statements into a single if-elif-else
block as shown below.
if condition1:
# runs only when "condition1" is True
elif condition2:
# runs only when
# "condition2" is True but "condition1" is False
else:
# runs if
# both "condition2" and "condition1" are False
elif
stands for “else if”. E.g., see the following code.
Only one block, either if
, elif
, or else
executes at a time.
The condition of the elif
block (condition2
) is only checked when the first
condition (condition1
) is False
, i.e., if the first condition fails. Also,
we can instantly recognise that the code blocks of the if-elif-else
are
logically connected.
The only required part of the
if
statement is the firstif
block. Theelif
andelse
blocks are optional.
Conditional Expressions
Python provides another type of if statement in addition to the ones shown above. These are called conditional expressions or ternary operators. These expressions allow value assignment using an in-line if statement. The syntax of a conditional expression is as follows.
output = value if condition else alternative_value
output
is set tovalue
if thecondition
isTrue
,output
is set toalternative_value
otherwise.
Ternary means that the operator consists of three parts. The parts of the above operator are a value if a condition is met, a condition, and an alternative value if the condition is false.