Home » Programming Languages » Python

Best Naming Conventions When Writing Python Code

  • ZoeZbar 

Naming Conventions 

During a recent workshop, bootcamp instructor Alex Baransky shared some of the best practices in writing Python code for better readability. Through this series of articles, we will summarize and give you a few examples of those best practices to help you write more elegant Python code, benefiting those who may read and use your code in the future, including yourself.

Python is an object-oriented programming language. This means you’ll create, name, and use all kinds of objects in any given Python project. Naming conventions are the rules of thumb for naming the objects that you create. Understanding these conventions is the first step to keeping your code readable for you and your teammates. 

Through our bootcamps and courses, we share to students that there are tons of ways to name objects, some being better than others. Understanding the difference between good and great is the way to impress prospective employers and peers. In this article, we will share best practices in naming conventions that you can follow and adopt when writing your Python code.

Basic Naming Conventions

Let’s start with the most common options for naming objects in Python. The following are basic ways to name: 

  • Single lowercase
    • Often used for local variables in functions, such as x or i
  • Single uppercase
    • Often used to name matrices
  • Lowercase word
    • The most frequent naming convention for the widest variety of objects
  • Lowercase word with underscores
    • Same uses as Lowercase word, but for more complicated names
  • Uppercase word
    • Used for single-word static variables
  • Uppercase word with underscores
    • Used for multi-word static variables
  • Capitalized words (aka CapWords or CamelCase)
    • This is where each word is capitalized, and there are no spaces or underscores between them
    • Used for naming classes (even if the name is just a single, capitalized word)
  • Mixed Case
    • This is where you start with a lowercase word followed by every other word capitalized
    • This convention is predominantly used in Java and less in Python 

Each of these options has different use cases. They are not only for aesthetics, but each option also helps you create meaningful objects in your code. 

Names to Avoid

Just as there are things you should do, there are things you shouldn’t. These are conventions you will want to avoid when writing Python code. 

  • Lowercase letter “el” – instead, use a capital letter “L”
  • Uppercase letter “oh” – “O” 
  • Uppercase letter “eye” – “I”

We avoid those names because in some fonts, they are indistinguishable from the numbers “1” and “0”, which makes it difficult for the user to understand what’s going on.

You should also avoid using Python keywords and built-in class/function names for your variable names. For example, words like “max,” “sum,” “class,” and “list” are words that already exist in any Python environment. Therefore, to use them for something other than their function could make things messy and confusing.

Modules and Packages

A module is a collection of pre-built functions and other objects used to perform certain tasks. Modules should have short, lowercase names. Underscores can be used in the module name if it improves readability (like for names with multiple words). 

Packages are like directories that contain modules and other objects. Python packages should follow the same convention, although the use of underscores in package names is discouraged. 

Variables and Functions

Do you know the difference between a function and a method?To understand the difference, think of the square-rectangle relationship: a method is a function, but not all functions are methods. A method is a special type of function that belongs to a class. Only objects of that class type can use these special functions. This is part of the object-oriented paradigm of Python and can help improve the organization and readability of our code. In Python, the names of variables and functions should be lowercase. Individual words can be separated by underscores when needed. This will improve readability within your code. Method names should follow the same conventions as function names. 

Here are a few best practices to follow when naming your variables and functions:

  • Constants should be represented by all capital letters and separated by underscores when needed 
  • Use names that are representative of the meaning of the object rather than meaningless, single-character names
  • Names i, j, and k should be reserved for representing index values

Understanding and adopting these best practices is a great way to polish your coding skills. Writing more elegant code will not only impress your current colleagues, but it will also help you build better coding habits that may catch the attention of future employers. 

Ready to advance your programming skills? Check out this three-course program focused on building and advancing your Python Programming skills, or start your journey towards data science mastery by enrolling in our upcoming remote live and online bootcamps this Winter.