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:

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:

>>> 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:

>>> 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):

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:

>>> 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:

>>> "ATGC" + "CGTA"  # Results in "ATGCCGTA"
ATGCCGTA
>>> "ATGC" * 2  # Results in "ATGCATGC"
ATGCATGC
>>> "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.

← Previous Next →