Skip to content

Variables & Data Types

Python has a small set of built-in data types that cover most everyday needs. This page walks through each one with examples drawn from bioinformatics. By the end you will know how to create variables, check their types, and convert between types.

A variable is a name that points to a value. You create one with the = operator.

Store a gene count as an integer:

gene_count = 1542
print(gene_count)
1542

Store a sample identifier as a string:

sample_id = "SAMPLE_01"
print(sample_id)
SAMPLE_01

Store a boolean flag:

is_significant = True
print(is_significant)
True

Variable names should be descriptive. Use lowercase letters and underscores. Avoid single letters except in short loops.

Integers are whole numbers with no decimal point. They are useful for read counts, chromosome positions, and gene totals.

read_count = 15000
print(type(read_count))
<class 'int'>

Python integers have no upper size limit. You can represent very large numbers without overflow.

Floats are numbers with a decimal point. They are common for expression values, p-values, and fold changes.

expression_value = 23.45
print(type(expression_value))
<class 'float'>

You will often work with multiple float variables together. F-strings make it easy to print them in a readable format.

pvalue = 0.003
log2fc = 2.5
print(f"p-value: {pvalue}, log2FC: {log2fc}")
p-value: 0.003, log2FC: 2.5

Strings hold text. In bioinformatics you will use them for gene names, chromosome labels, DNA sequences, and file paths.

F-strings let you embed variables directly inside a string. Prefix the string with f and put variables in curly braces.

gene_name = "BRCA1"
chromosome = "chr17"
print(f"Gene {gene_name} is on {chromosome}")
Gene BRCA1 is on chr17

The len() function returns the number of characters in a string. This is handy for checking sequence lengths.

print(len(gene_name))
5

Strings come with many built-in methods. Two useful ones for bioinformatics are .upper() and .count().

Convert a lowercase DNA sequence to uppercase:

sequence = "atgcgatcga"
print(sequence.upper())
ATGCGATCGA

Count how many times a base appears in a sequence:

print("ATGCGATCGA".count("G"))
3

Other helpful string methods include .lower(), .replace(), .startswith(), and .split(). You will see these throughout the course.

Booleans are either True or False. They result from comparisons and logical operations.

Check whether a p-value is below a significance threshold:

pvalue = 0.003
is_significant = pvalue < 0.05
print(is_significant)
True

Combine multiple conditions with and. This is a common pattern when filtering differential expression results.

log2fc = 2.5
passes_filter = is_significant and abs(log2fc) > 1
print(passes_filter)
True

Python also supports or and not for combining boolean values.

None represents the absence of a value. It is Python’s way of saying “nothing here.” You will encounter it when a variable has not yet been assigned meaningful data, or when a function returns no result.

annotation = None
print(annotation)
None

Use is to check for None. Do not use ==.

print(annotation is None)
True

Once you assign a real value, the check returns False:

annotation = "tumor suppressor"
print(annotation is None)
False

Use type() to see what data type a value has.

print(type(gene_name))
<class 'str'>
print(type(42))
<class 'int'>
print(type(3.14))
<class 'float'>
print(type(True))
<class 'bool'>

Use isinstance() when you need to check if a value belongs to a specific type. This function also accepts a tuple of types.

print(isinstance(gene_name, str))
True
print(isinstance(42, (int, float)))
True

Sometimes you need to convert a value from one type to another. Python provides built-in functions for this.

Convert a string to an integer. This is common when reading count data from text files.

count_string = "1500"
count_number = int(count_string)
print(count_number + 100)
1600

Convert an integer to a string:

print(str(42))
42

Convert a string to a float:

print(float("3.14"))
3.14

Convert a float to an integer. Python truncates toward zero, dropping the decimal part.

print(int(3.7))
3
Type Example Use case
int 1542 Read counts, gene totals
float 0.003 P-values, fold changes
str "BRCA1" Gene names, sequences, file paths
bool True Significance flags, filters
None None Missing or unset values

You now know how to store and inspect individual values. Real datasets contain many values grouped together. Head to Data Structures to learn about lists, dictionaries, tuples, and sets.