3. Control Flow: Conditionals
Conditionals
Until now, we have rarely seen code that wasn't executed in the exact order it was listed. However, in our quest to write more complicated programs we will need tools to manipulate the sequential execution of our code. These tools are referred to as control structures, they direct the order of execution of expressions and statements in the code. In other words, they determine the control flow of the code. The first control structure we will see is the conditional statement or if-statement.
In our lives, we constantly perform actions conditionally. For example, if it's raining outside you might decide to wear a jacket, or not to go outside at all. Python provides a tool to emulate this conditional decision-making in the form of the if-statement.
#\color{#8959A8} {\mathtt{if}} \textit{ expression}\mathtt{:}#
#\hspace{.6em}\textit{statement}#
Here, expression is a valid Python expression that can be evaluated, like a boolean expression or a comparison. statement refers to a block of code that will be executed if expression evaluates to #\color{#F5871F} {\mathtt{\text{True}}}#, it will be skipped over if expression evaluates to #\color{#F5871F} {\mathtt{\text{False}}}#. Note that an if-statement is always preceded by the #\color{#8959A8} {\mathtt{\text{if}}}# keyword and closed by a colon #\mathtt{\text{:}}#. The statement is always indented to indicate that it's a part of the control structure.
Let's take a look at an example of an actual if-statement. We can clearly identify the expression #\mathtt{\text{x} \color{#3e999f}{\text{ >= }} \color{#F5871F}{\text{0}}}# and the statement #\mathtt{\color{#8959A8}{\text{print}}\text{(}\color{#718c00}{\text{"x is non-negative"}}\text{)}}#. This program uses a conditional statement to test whether #\mathtt{\text{x}}# is greater or equal to #\color{#F5871F}{\mathtt{\text{0}}}#, if that's the case it lets us know with a message. |
|
Sometimes you might want to perform different operations for many different scenarios, then using just if-statements won't suffice. To this end, Python provides elif- and else-statements, a way to introduce checking for different scenarios within the same conditional construct.
#\color{#8959A8} {\mathtt{if}} \textit{ expression}_\textit{1}\mathtt{:}# #\hspace{1.3em}\textit{statement}_\textit{1}# #\color{#8959A8} {\mathtt{elif}} \textit{ expression}_\textit{2}\mathtt{:}# #\hspace{1.3em}\textit{statement}_\textit{2}# |
#\hspace{2em}\text{and}# | #\color{#8959A8} {\mathtt{if}} \textit{ expression}\mathtt{:}# #\hspace{1.3em}\textit{statement}_\textit{1}# #\color{#8959A8} {\mathtt{else}} \mathtt{:}# #\hspace{1.3em}\textit{statement}_\textit{2}# |
The #\color{#8959A8} {\mathtt{\text{elif}}}# keyword can be used after an if-statement to specify an action that is to be executed when a different expression is evaluated to #\color{#F5871F} {\mathtt{\text{True}}}#. The difference between an #\color{#8959A8} {\mathtt{\text{elif}}}# construction and two sequential if-statements is that, unlike an elif-statement, a second if-statement can still be executed even if the preceding one was already triggered. The #\color{#8959A8} {\mathtt{\text{else}}}# keyword can be used to specify a block of code that has to be executed when none of the preceding conditionals have been triggered. Both elif- and else-statements require a preceding if-statement.
As you can see, you can fit elif-statements between if- and else-statements. In a construct like this, if one of the statements is found to be #\color{#F5871F} {\mathtt{\text{True}}}#, all remaining statements will be skipped. |
|
Sometimes, conditional constructs can get too large and limit the readability of your code. Python offers a few options for this, one of these is the inline if-statement or if-expression. Note that these shorthands should also be used wisely, they're not supposed to be spammed just because they're shorter, always keep readability in mind.
#\color{#8959A8}{\mathtt{if}}\textit{ expression}\mathtt{:}\textit{ statement}_\textit{1}\mathtt{;}\textit{ statement}_\textit{2}\mathtt{;}#
If your conditional construct is quite compact, you can consider using inline conditional statements. Note that every expression within the statement should be separated by a semicolon #\mathtt{\text{;}}#. |
|
Another if-statement shorthand is the if-operator, this construct allows you to write an if/else-statement as an expression. It is recommended to use these when assigning or returning values conditionally. But again, only if writing it as an expression doesn't sacrifice readability.
#\mathtt{\text{var} \color{#3e999f} {\text{ = }} \textit{value}_\textit{1}\textit{ }\color{#8959A8} {if} \textit{ condition } \color{#8959A8}{else} \textit{ value}_\textit{2}}#
As opposed to a conditional structure, this usage of #\color{#8959A8} {\mathtt{\text{if}}}# is more like an operator, in this case #\color{#8959A8} {\mathtt{\text{if}}}# is referred to as a conditional or ternary operator. It is especially useful when assigning values conditionally to a single variable. In such a case, using a ternary operator is much more readable than using a conditional construct. |
n = input()
odd = True if n % 2 > 0 else False n = input()
if n % 2 > 0: odd = True else: odd = False |
Or visit omptest.org if jou are taking an OMPT exam.