<h2class="lecturer">LinkedIn Learning : Bill Weinman</h2>
<h2class="episodetitle">Conditionals</h2>
</div>
<article>
<h2class="sectiontitle">Conditional Syntax</h2>
<p>We have already seen several examples of if statements and they look pretty similar to if statements in other languages although there are a couple of things that may look a little strange if you are familiar with some of these other languages. The first one which is something I find a little weird is that the condition doesn't have to be placed in parentheses. That could take some getting used to but if you put the condition in parentheses, it is still going to work.</p>
<p>The second thing is not quite a feature that is unique to Python (but the only other language I know where you will see this is Perl) and that is the colon at the end of the if statement. From what I can tell, this is intended to let the interpreter know that a mandatory code block follows. The if statements code block is indented, which you may remember is used to signify a code block in Python. However, look at the code snippet below.</p>
<preclass="inset">
if True:
# print('if true')
elif False:
print('elif true')
else:
print('neither true')</pre>
<p>As you can see, the code block associated with the if statement consists of one line of code that has been commented out so there is no executable code in the block. The error message you see when you run this may seem a little strange at first.</p>
<preclass="inset">
IndentationError: expected an indented block</pre>
<p>Since the code block is mandatory, the interpreter will assume that the next line of executable code that it finds will be the first line of the ifs code block. In this case, that is the elif statement and since this is not indented, we get an indentation error. Of course, you can fix it by indenting the elif statement, but since the elif statement needs an if statement at the same level of indentation, this is just going to give you a different error.</p>
<p>In any case, if you see this error, the cause is likely to be either that you haven't include a code block for an if statement (or anything including elif, else and probably functions where the code block is mandatory) or you have included the code block but haven't indented it. So the solution would be to indent the code if (and only if) it belongs in the code block, otherwise, the code block is missing and needs to be added.</p>
<p>There may actually be occasions where you may not want to include the mandatory code block. Since it can't be omitted altogether, the simplest solution is to include a line of do nothing code.</p>
<p>Take a look at the code block below and think about what will happen when this code is executed.</p>
<preclass="inset">
if True:
bx=0
elif True:
print('elif true')
else:
print('neither true')</pre>
<p>You may be tempted to say that 'elif True' will be output since the condition is true, but the condition for both the if and elif clauses will always return true. The condition in the elif clause is only evaluated if the if condition returns a false value and since it always returns true, the elif clause is never executed and neither is the else clause.</p>
<p>In the if clause, the only executable statement sets the value of a variable, but since it is executable, it constitutes a code block and so the interpreter is happy that the mandatory code block is present.</p>
<p>Let's consider a more complicated option. You may see code like this when the user is expected to select a menu option with the options represented by the numbers 1 to 5.</p>
print('Please select an option from 1 to 5')</pre>
<p>We start with an if statement to determine whether the first option has been selected and we have elif clauses to check for each of the other valid options. The else clause if executed if none of the previous clauses returned a true value. For example, if the user selects option 7 which is not valid, the else clause displays a message telling the user that valid options are from 1 to 5.</p>
<p>This is, in effect, a switch statement which doesn't really exist in Python (although there is a <ahref="https://docs.python.org/3.10/whatsnew/3.10.html#pep-634-structural-pattern-matching">match-case</a> statement in Python 3.10 and later).</p>
<p>You may notice that we could use if statements rather than elif but the problem with that approach is that the else clause would only be associated with the last if statement so the only time it wouldn't be executed would be where the user selects option 5. It would be displayed if any other valid option was selected so we really need to use elif statements.</p>
<p>If statements can be nested so if we want to test for more than one condition, we can do that by testing for the first and then (if the first condition is true) testing for a second condition. For example, let's assume that we want to test whether the user is authorised to select a menu option. We can do that by putting all of the options inside another if statement, like this.</p>
print('User not authorised to access this menu.')</pre>
<p>The first condition will be either true or false. If it is true, its code block (which displays a message telling the user either which option has been selected or that no valid option has been selected) will be executed. If it is false, the message that tells the user that they are not authorised to select a menu item will be displayed.</p>
<p>Python 2.5 and later has a type of ternary operator and this can set the of a variable to one of two values, depending on whether some condition evaluates to true or false. For example</p>
<preclass="inset">
hungry = True
x = 'Feed the bear now!' if hungry else 'Do not feed the bear.'
print(x)</pre>
<p>If the condition is true, which it is in this case, x will be assigned the value 'Feed the bear now!'. If the condition were to evaluate to false, the alternative string would be assigned to x, so x would get the value 'Do not feed the bear.'.</p>
<p>This is just a rearrangement of an if..else statement so</p>
<preclass="inset">
if (hungry):
x = 'Feed the bear now!'
else:
x = 'Do not feed the bear.'</pre>
<p>Personally, I find this syntax a little weird and clumsy but this could be due to a lack of familiarity with it. It's not something you really need to know in order to write good Python code, but you may see it in other people's code so it's a good idea to at least be aware of it.</p>