28 min to read
Expressions
Operators and operands make EXPRESSIONS.
Hello everyone! Welcome back to Python for Bioinformatics. This is Ali Hassan, and I’m thrilled to bring you the third video of this exciting series. Now, let’s get into today’s lesson: Expressions in Python. Whether you’re just getting started or brushing up on your skills, understanding expressions is fundamental to bioinformatics scripting.
What Are Expressions?
In Python, an expression is a combination of operators and operands. Think of it like biological processes:
- Operators: The “machinery” performing the action (e.g., ribosomes translating mRNA into proteins).
- Operands: The inputs or “raw materials” being processed (e.g., mRNA and amino acids). Here’s a real-life analogy: when you buy a ticket at an airport, the agent is the operator, and your passport and money are the operands.
Similarly, in Python:
>>> 2 + 3 # Here, '+' is the operator, and 2, 3 are the operands.
5
Types of Operators in Python
1. Numerical Operators
Python supports basic arithmetic operations:
- + (Addition): 2 + 3 = 5
- - (Subtraction): 3 - 1 = 2
- * (Multiplication): 2 * 3 = 6
- \/ (Division): 11 / 4 = 2.75
>>> 2 + 3 # Addition
5
>>> 3 - 1 # Subtraction
2
>>> 2 * 3 # Multiplication
6
>>> 11 / 4 # Division
2.75
>>> 2 ** 3 # Power
8
>>> 11 // 4 # Integer Division
2
>>> 11 % 4 # Modulo
3
Python also handles advanced operations:
- Power: 2 ** 3 (2 to the power of 3) results in 8.
- Integer Division: 11 // 4 gives 2 (ignores decimals).
- Modulo: 11 % 4 gives 3 (the remainder).
>>> 2 ** 3 # Power: 2 to the power of 3
8
>>> 11 // 4 # Integer Division: 11 divided by 4, ignoring decimals
2
>>> 11 % 4 # Modulo: remainder of 11 divided by 4
3
⚠️ Python Version Matters: Be aware of the Python version you’re using. Division behaves differently in Python 2 vs. Python 3.
2. Logical Operators
Logical operators return Boolean values (True or False):
- and: Returns True if both conditions are true.
- or: Returns True if at least one condition is true.
- not: Reverses a Boolean value.
Example:
>>> True and False # Results in False
False
>>> True or False # Results in True
True
>>> not True # Results in False
False
3. Comparison Operators
These are used to compare values:
- == (Equal to)
- != (Not equal to)
- < (Less than)
- <= (Less than or equal to)
- > (Greater than)
- >= (Greater than or equal to)
>>> 5 == 5 # Equal to
True
>>> 5 != 3 # Not equal to
True
>>> 3 < 5 # Less than
True
>>> 3 <= 5 # Less than or equal to
True
>>> 5 > 3 # Greater than
True
>>> 5 >= 3 # Greater than or equal to
True
String Operations
In bioinformatics, strings often represent DNA, RNA, or protein sequences. Python offers several operations to manipulate them:
- Concatenation: Combine sequences with +
>>> "ATGC" + "CGTA" # Results in "ATGCCGTA"
ATGCCGTA
- Repetition: Repeat sequences with *.
>>> "ATGC" * 2 # Results in "ATGCATGC"
ATGCATGC
- Membership: Check for substrings with in or not in.
- Repetition: Repeat sequences with *.
>>> "AT" in "GATC" # True
True
>>> "GC" not in "GATC" # False
False
Slicing Strings
Extract specific parts of sequences using slicing:
>>> seq = "ATGCCGTA"
# Extract the first 3 characters
>>> seq[0:3] # "ATG"
"ATG"
# Extract the last 3 characters
>>> seq[-3:] # "GTA"
"GTA"
# Reverse the sequence
>>> seq[::-1] # "ATGCCGTA"
"ATGCCGTA"
Combining Operators and Expressions
Operators can be combined to form compound expressions:
>>> 2 * 3 + 4 - 1 # Follows operator precedence: multiplication > addition > subtraction.
9
Use parentheses to control order:
>>> (2 + 3) * (4 - 1) # Results in 15
15
Bonus: Function Calls
Python functions, like len() and print(), are essential for bioinformatics workflows:
>>> seq = "ATGCCGTA"
>>> len(seq) # Outputs: 8
8
This concludes our discussion on expressions and operators. Try experimenting with different combinations and scenarios to solidify your understanding. In the next post we will be talking about Names & Naming Conventions.
Comments