Boolean expressions
3 min read
•
Updated Jan 2024
•
Peer-Reviewed Learning Resource
Boolean expressions play a crucial role in programming and are fundamental to decision-making in conditional statements. In Python, boolean expressions evaluate to either True or False. Understanding how to create and use boolean expressions is essential for next-generation biologists engaged in bioinformatics, where logical conditions often guide data analysis and interpretation.
Basic Boolean Expressions:
1. Comparison Operators:
Comparison operators are used to compare values and create boolean expressions.
Equal to (==):
Not equal to (!=):
Greater than (>):
Greater than or equal to (>=):
Less than (<):
Less than or equal to (<=):
Pyton
x = 5
y = 10
print (x == y) # False
print (x != y) # True
print (x > y) # False
print (x >= y) # False
print (x < y) # True
print (x <= y) # True
2. Logical Operators:
Logical operators combine boolean expressions.
AND (and):
OR (or):
NOT (not):
Pyton
age = 25
is_student = True
print(age > 18) and is_student # True
temperature = 30
is_summer = True
result = (temperature > 25) or is_summer # True
print(result)
is_raining = False
result = not is_raining # True
print(result)
Some more examples
Pyton
sequence_quality = 28
is_high_quality = sequence_quality >= 30
print("Is the sequence of high quality?", is_high_quality)
Pyton
gene_variant = 'KRAS_G12C'
is_pathogenic = gene_variant.startswith('KRAS') and ('G12' in gene_variant or 'G13' in gene_variant)
print("Is the mutation pathogenic?", is_pathogenic)
Pyton
cell_marker_expression = {
'CD3': 0.8,
'CD19': 0.1,
'CD56': 0.5
}
is_t_cell = cell_marker_expression['CD3'] > 0.5
is_b_cell = cell_marker_expression['CD19'] > 0.5
is_nk_cell = cell_marker_expression['CD56'] > 0.5
print("Is it a T-cell?", is_t_cell)
print("Is it a B-cell?", is_b_cell)
print("Is it an NK cell?", is_nk_cell)