Names & Naming Conventions

Following the naming conventions in Python can be really helpful.

Hello, Bioinformatics Guy here!

In this installment of our Python for Bioinformatics series, we’ll delve into the world of names and naming conventions—a small yet critical topic that forms the foundation of effective programming. Think of it as naming a newborn baby but without the family debate!

Let’s jump right in.

What Are Names in Python?

In Python, a name is essentially a label for a value. You can assign any name to any value using the = operator. This makes working with data—like protein sequences, DNA sequences, or numbers—much easier.

>>> pro1 = "MNK"  
>>> pro1
"MNK"
>>> pro2 = "AARHQGR"  
>>> pro2
"AARHQGR"
>>> dna1 = "ATGCTAGC"  
>>> dna1
"ATGCTAGC"
>>> dna2 = "THEGATACGA"  
>>> dna2
"THEGATACGA"

Here, names like pro1, pro2, dna1, and dna2 are assigned to respective protein or DNA sequences. Let’s check the length of some of these:

>>> len(pro1)
3
>>> len(dna1)
8

Let’s try what is stored inside this anme dna3:

>>> dna3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dna3' is not defined

Let’s assign same value to dna3 name:

>>> dna3 = dna2  
>>> dna3  
THEGATACGA
>>> dna2  
THEGATACGA  

Key Takeaways on Names:

Chaining and Shortcuts in Assignments

You can chain multiple names to the same value:

>>> a = b = c = D = 0
>>> a
0
>>> b
0
>>> c
0
>>> D
0

Chaining and Shortcuts in Assignments

You can chain multiple names to the same value:

>>> age = 0
>>> age = 1
>>> age = age + 1
>>> age 
2
>>> age += 1
>>> age
3
>>> age -= 1
>>> age
2

This shorthand, called augmented assignment, works with addition, subtraction, multiplication, and division.

Try this short cut assignment with multiplication and division and post your answers in the comments below.

Why Naming Conventions Matter

Proper naming conventions help others (and your future self) understand your code better. Imagine naming a variable l or O. These can be confused with 1 or 0 in most fonts, leading to unnecessary errors and confusion.

Here are some essential tips:

>>> l = 10  # Is it a lowercase L or a 1?  
protein_sequence = "MNK"  
# Incorrect: 1st_protein = "MNK"  
# Correct: first_protein = "MNK"  
# Not ideal: x = "MNK"  
# Better: protein_sequence = "MNK"  

Further Reading

To explore naming conventions in greater detail, check out PEP 8 Naming Conventions. This Python style guide offers best practices for clean and readable code.

Final Thoughts

Naming is a small but vital part of Python programming. It ensures clarity and prevents misunderstandings, especially when working on collaborative projects or revisiting your code later.

Thank you for reading, and I’ll see you in the next post about Defining Functions.

Happy coding!

← Previous Next →