ubuntu2004
Making decisions II - combined and nested conditions and booleans
Combined conditions
Two (or more) conditions can be combined and tested together. To combine conditions we use the logical operators "and
" and "or
".
When using the and
operator, both conditions need to be True for the combined condition to be True otherwise it is False.
When using the or
operator, if either condition is True the combined condition is True otherwise it is False.
The and
operator
The following code shows an example using and
. The normal range of haemoglobin levels in the blood of females is between 11.6 and 15.0 g/dl (grams per decilitre). The code asks to input a patient's haemoglobin level and then tests to see if it is within the normal range.
There are two conditions in the conditional statement: haemoglobin_level > 11.6
and haemoglobin_level < 15.0
.
If you entered a value of 50, for instance, then the first condition is True and the second one is False. This means the combined condition is False and the patient's haemoglobin level is outside the normal range.
If you entered a value of 10, say, then the first condition is False and the second one is True. This means the combined condition is False and the patient's haemoglobin level is outside the normal range.
If you entered a value of 13, say, then both conditions are True. This means the combined condition is True and the patient's haemoglobin level is within the normal range.
The or
operator
In the genetic code, the codons "TAA", "TAG" and "TGA" are stop codons that terminate the synthesis of protein elongation in the ribosome.
The following code tests to see if any of these three codons are in a given DNA sequence.
Notice that we have combined three conditions, one for each stop codon. If any one of them is True then the combined condtion is also True.
In fact, this sequence has just the one stop codon "TAG" at its end.
Combining and
and or
operators
For a DNA sequence to encode a protein it must contain a start codon ("ATG") and a stop codon ("TAA" or "TAG" or "TGA").
We can combine and
and or
operators, but we have to be careful because and
takes precedence over or
. Remember in Notebook 3 that multiplication takes precedence over addition. Which means 4 + 3*2 = 10
(the 3*2
takes precedence over 4 + 3
). To break the precedence rule you have to add parentheses around 4 + 3
like so: (4 + 3)*2 = 14
. The same rule applies for conditions with and
taking precedence over or
.
You can see in the following code that the three conditions for the stop codons are surrounded by parentheses. These parentheses break the precedence of the and
between
if there were no parentheses.
Nested conditions
In the haemoglobin example above we only tested if a female patient's haemoglobin level is within the normal range. The normal range for a male is slightly higher: between 13.2 and 16.6 g/dl.
What we want to do is first test if a patient is male or female, then, based on the result of that test, test if their haemoglobin level is within the normal range. For this we can use nested conditions, i.e., if
statements nested within other if
statements.
We first ask the user to input the patient's gender and store it in the variable gender
. We want the user to input "m" for male and "f" for female. Any other input should produce an error message. This gives us our first condition where we check if the input is "m" or "f" and nothing else:
If gender
is neither "m" nor "f" the code jumps to the bottom else:
statement and prints an error message. (In a real app, the code would re-ask the question until a valid input was entered.)
Next we ask the user to input the patient's haemoglobin level. Notice that this is indented within the first condition. So it is only executed if "m" or "f" was entered for gender.
Next we test if the patient's haemoglobin level is within the normal range, but, as the range is different for males and females, we have a different test depending on gender. So we next test if the gender is female:
This condition is indented because it is nested within the previous condition. If the gender is female we test if the haemoglobin level is in the normal range for females:
This combined condition is double-indented because it is nested within the previous two conditions.
If the gender is not female it must be male. So we test if the haemoglobin level is in the normal range for males:
Again, this combined condition is double-indented because it is nested within the previous two conditions.
Booleans
You may have noticed that we have been referring to conditions being True
of False
. Why the capitalisation?
It is obviously true to say that 4 is greater than 3. And it is obviously false to say that 0 is equal to 1.
Moreover, we can assign a condition to a variable:
The variable c
is called a boolean. Its type is bool
as shown in the code below.
We can print the boolean values True
and False
:
We can also assign the boolean values True
and False
to variables: