Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

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.

Run the code and enter some numbers to see how it works.
# Input a female patient's haemoglobin level (in g/dl), casting the entered string to a float # and assign it to the variable haemoglobin_level. haemoglobin_level = float( input("Enter patient's haemoglobin level: ") ) # Test if the patient's haemoglobin level is within the normal range for females of 11.6 to 15.0 g/dl. if haemoglobin_level >= 11.6 and haemoglobin_level <= 15.0: print("The patient's haemoglobin level is within the normal range") else: print("The patient's haemoglobin level is outside the normal range")
Enter patient's haemoglobin level:
The patient's haemoglobin level 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.

Run the code to see how it works.
# Assign a DNA sequence to a string variable called dna_seq. dna_seq = 'ATGCCCGGATCGATTTGGGCCAGGATCGGACCGATTGTAG' if 'TAA' in dna_seq or 'TAG' in dna_seq or 'TGA' in dna_seq: print( 'The DNA sequence contains a stop codon' ) else: print( 'The DNA sequence does not contain a stop codon' )
The DNA sequence contains a stop codon

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

'ATG' in dna_seq and 'TAA' in dna_seq

if there were no parentheses.

if 'ATG' in dna_seq and ('TAA' in dna_seq or 'TAG' in dna_seq or 'TGA' in dna_seq): print( 'The DNA sequence contains a start and a stop codon' ) else: print( 'The DNA sequence does not contain both a stop and a start codon' )
The DNA sequence contains a start and a stop codon

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.

Look at the following code to understand what is going on then run it and enter some values.
# Ask for the patient's gender and assign it to the variable gender. gender = input("Enter patient's gender (m or f): ") # Only ask for haemoglobin level if either "m" or "f" was entered for gender. # If anything other than "m" or "f" was entered do not proceed and print an error message. if gender == 'm' or gender == 'f': # Input a patient's haemoglobin level (in g/dl), cast the inputted string to a float # and assign it to the variable called haemoglobin_level. haemoglobin_level = float( input("Enter patient's haemoglobin level: ") ) # Test which gender was entered. if gender == 'f': # Gender is female. # Test if the female patient's haemoglobin level is within the normal female range of 11.6 to 15 g/dl. if haemoglobin_level >= 11.6 and haemoglobin_level <= 15.0: print("The female patient's haemoglobin level is within the normal range") else: print("The female patient's haemoglobin level is outside the normal range") else: # Gender is not female so it must be male. # Test if the male patient's haemoglobin level is within the normal male range of 13.2 to 16.6 g/dl. if haemoglobin_level >= 13.2 and haemoglobin_level <= 16.6: print("The male patient's haemoglobin level is within the normal range") else: print("The male patient's haemoglobin level is outside the normal range") else: # Neither "m" or "f" was entered for gender so print an error message. print('An unknown gender was entered. Use either m or f for gender')
Enter patient's gender (m or f):
An unknown gender was entered. Use either m or f for gender

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 == 'm' or gender == 'f':

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:

if gender == 'f':

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:

if haemoglobin_level >= 11.6 and haemoglobin_level <= 15.0:

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:

if haemoglobin_level >= 13.2 and haemoglobin_level <= 16.6:

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.

In Python we can print the truth of a condition. Run the following code cells to see how.
print( 4 > 3 )
True
print( 0 == 1 )
False

Moreover, we can assign a condition to a variable:

# Assign the condition "4 > 3" to the boolean variable c c = 4 > 3 print(c)
True

The variable c is called a boolean. Its type is bool as shown in the code below.

type(c)
bool

We can print the boolean values True and False:

print(True) print(False)
True False

We can also assign the boolean values True and False to variables:

# Assign True to the boolean variable called answer. answer = True print(answer)
True

Exercise Notebook

Next Notebook