Table of contents
Logical statements are binary at either True or False:
Note the capitalisation of True and False. These will not be recognised if only lower case true and false are used.
Let's set the value True to a variable a and the value false to a variable b:
Now let's look at the if statement.
The if statement only allows code to br executed if the condition set is True. In this case only the first statement is printed because a is True and b is not True.
a is true
if not Condition
The word not can be used to flip a condition. i.e. code will only be executed if the condition is False. Now both statements are ran:
a is True
b is not True
if, else Condition
So far our code has been direct and as an analogy we can think of it as being as being a car driving along a straight road.
However quite often it will be the case that we will want code to be ran if some condition is satisfied and we will want to run alternative code if the condition isn't satisfied. For this we use an if else statement. Continuing with the analogy of being a car on the road, we can think of this as being a two way junction, where we turn either left or right depending on our destination which is our condition.
a is True
b is False
In this case there are two if, else conditions:
Pay attention to the spacing and indentation. if is not indented however, the code belonging to the if statement is indented by 1 tab (4 spaces), this is followed by a new line. Next there is the else statement which is also not indented however the code belonging to it is indented by one tab (four spaces) and this is once again followed by a new line. The spacing used is critical for Python to understand your if, else statement.
Think of this as a written sentence in English: "if condition 1 is satisfied do this…, else do that…" and "if condition 2 is satisfied do this…, else do that…"
Now think of the following as a written sentence in English "if condition 1 is meet do this…, if condition 2 is meet do this…, else do that, else do that". You can see that the sentence's meaning gets muddled. Doing so will give an invalid syntax error. It follows on that each else statement follows from its corresponding if statement.
So far we have only looked at two assigned conditions True and False but we can also use the relationship between numbers to decide whether a decision is True or False. Comparing two numbers we can use 6 relations to first number to the second:
- greater than (>)
- greater than or equal to (>=)
- equal to (==)
- less than or equal to (<=)
- less than (<)
- not equal to (!=)
Let's take the numbers 5 and 4 and compare them using the console.
Note the difference between equal to (==) to the assignment operator (=) let's assign a variable c to 5 is equal to 5.
Here we think of the right hand side as a function using the input arguments 5 and 5. Here the output argument of this function is False if 5 and 5 differ and True if 5 and 5 are the same. This logical True/False value is assigned to the variable c.
One can also use the NumPy function equal to visualise this:
if, elif (else if) and else Conditions
Following on from the comparison of two number we can use the logical operators >, < and == to create 3 separate conditions for if, elif and else code.
In this case, the code following the if condition is satisfied so the code in the if statement is executed. Note it is possible to use multiple elif statements for more complicated datasets, e.g. datasets consisting of multiple numbers. However one should realise that if the code is ran from an earlier branch, then any code from a subsequential branch will be ignored. For example the condition in the 0th branch (if), is identical to the condition in the 2nd branch (elif) and when the code is ran, because the code in the if branch has been ran, the subsequential code in the later branch has been ignored.
Multiple Conditions (and or or)
For this example let's look at a circuit with two switches. Each of the two switches can have one of two positions off  or on . Only if both switches are on [1,1], does current draw from the battery, along the wires to the bulb to give light on.
We can list the combinations in a two element vector, d
|d||d||bulb on or off|
Now let's create a numpy array d:
Let's now look at the two conditions individually:
We can combine both of these into a single condition using and:
Note when using multiple conditions it is recommended to enclose each condition in parenthesis:
And because these two conditions with the and statement itself is a condition, it should also be enclosed in parenthesis:
This can help where one gets to more complicated examples, for example if an additional switch is made in the circuit above:
The conditions available are:
|d||d||d||bulb on or off|
If the 0th and 1st switches are looked at as a collective and then compared to the 2nd switch, we can think of this as two conditions once again:
|d and d||d||bulb on or off|
The condition between the 0th and 1st switches:
The condition between (the 0th and 1st switches) and the 2nd switch:
Now let's look at a slightly different circuit:
In this circuit if either switch d or d is on, there will be a closed circuit giving current from a battery to the bulb and if both switches are on then there will be two closed circuits meaning the lamp receives power from both batteries and is on. In fact the only case when the lamp is not on is when both switches are off.
|d||d||bulb on or off|
Let's now look at a more complicated circuit.
In this case one can see that in order to turn the bulb on, either d==1 or d==1 and d==1
|d||d||d||bulb on or off|
Code can be put together to look at all these conditions:
If d is set to 0. We can rerun the code and now see that the bulb is off
Nested if Statements
Let's return to this circuit. We can also code it using a nested if statement.
Here the else statement in line 16 corresponds to the if statement in line 3. Code belonging to either is indented by 4 spaces (1 tab). For the if statement line 4-line 15 and for the else statement line 17-line 18. For the else statement, we know that if d!=0 it means that d==1 and irregardless of d or d the bulb will be on, so we do not need to look at their state.
Next there is a nested if statement line 5 and as this belongs to the first if statement it is indented by 4 spaces (1 tab). Code belonging to this nested if (line 6-12) is indented by 4 spaces plus another 4 spaces (8 spaces/2 tabs). This if statement has a corresponding else statement (line 13) and likewise is indented by 4 spaces (1 tab). Code belonging to it (line 14-15) is indented by 8 spaces (2 tabs). Here if d==1, we do not know if the bulb is on or off as it is also dependent on the switch d, so we need to look at d before making a decision, however in the else part, we know if d!=1 it means that d==0 and the bulb is off.
In line 7 is a nested, nested if statement, it is doubly indented (8 spaces/2 tabs) and code belong to it (line 8 and 9) is triply indented (12 spaces/3 tabs). It has a corresponding else statement (line 10) which is doubly indented (8 spaces/2 tabs) and code corresponding to it (line 11-12) is triply indented (12 spaces/3 tabs). Here we know that if d==1, given that d==1 then the bulb is on. If d!=1, i.e. d==0, then the bulb is off.
Spacing and indentation is incredibly important when using if, elif and else statements, particularly nested statements.
This circuit can also be examined using a nested if and else statements.