How To Use Python Naming Conventions

Join over 2 million students who advanced their careers with 365 Data Science. Learn from instructors who have worked at Meta, Spotify, Google, IKEA, Netflix, and Coca-Cola and master Python, SQL, Excel, machine learning, data analysis, AI fundamentals, and more.

Start for Free
Martin Ganchev Martin Ganchev 24 Apr 2023 3 min read

python naming conventions, python programming

Why are Python Naming Conventions Important?

While programming, you and your teammates will constantly need to give names to text files, variables, functions, and other entities in your code and documentation. The latter is applicable when you make it to the level of creating your own software.

The choice of which naming convention to stick to in different circumstances is key, as the way you name your text files and variables in Python is a sign of good or bad coding style.

If the names you use:

  • are unclear or misleading;
  • don’t correspond to any common or intra-company standard,

this will not be accepted by your peers as best practice.

And that's not the only issue.

Your naming conventions might be confusing to the point where they actually incite people to focus on the names you’ve used rather than on the main issues that your code aims to resolve.

This is something you really want to avoid.

At the end of the day, when you code, you want to demonstrate that you are a highly skilled professional. So, you can’t allow yourself not to stick to the best practices.

Therefore, because it will save a lot of time and will reduce the effort of others to understand your work, you will usually be provided with rules to abide by before you deliver your project. That means you won’t be allowed to use your own naming convention.

So, here's a brief overview of the most frequently encountered Python naming conventions. We believe you'll find them useful and you should incorporate them into your practice.

What Are the Most Common Python Naming Conventions?

UpperCamelCase

This is a naming convention in which the name you give is composed of multiple words that are attached to each other in a sensible order, and the first letter of each of these words is capital.

Example: UpperCamelCase – “LoanID”.

lowerCamelCase

This Python naming convention is very similar. The difference is that the first letter of only the first word is lowercase. Python is a case-sensitive language, therefore using UpperCamelCase to name your classes or column names, for example, can be a great idea to adopt in your work.

Example: lowerCamelCase – “loanID”.

lowercase_letters_separated_by_underscores.

This naming convention has been recommended by Python for names other than class names or constants. So, this naming convention can help you a great deal “internally”. For example, when naming your Python functions and variables.

You may encounter this Python naming convention also referred to as snake_case.

Example: “price_per_category”.

Kebab-case

Let's think beyond Python for a moment. You will often encounter names, particularly file names, where multiple words have been separated by a hyphen. That is, hyphens have been used instead of spaces. This naming convention has been called in a few different ways. Interestingly enough, the most popular of them is the kebab-case. This naming convention can be useful while sending files over the internet. So, you can adopt it for naming your text files.

Example: “Lending-company.csv”

On a Final Note – What General Rules You Should Follow in Naming Conventions?

As a first general rule, you shouldn’t leave spaces between words in the name of a file or object.

Different pieces of software may treat empty space in different ways, and sometimes they might even be unable to do so. Remember – don’t use whitespaces when naming files or programming entities.

Example: DON’T USE “File name 01”.

Second, and this is more stylistic rather than technical, try to use names that are concise and specific.

For instance, don’t use “numeric_variable”. Try to be specific. What is this numeric variable going to denote? For example, “loan_price”.

Example: “loan_price”. So, the most important takeaway would be to:

  • pay attention when naming text files and Python objects;
  • and develop a habit of using the different naming conventions at the right time.

This will help you write a more focused and easy-to-understand code. And this will further enhance the quality of your work in Python.

You can find an extended list of Python naming conventions in the official PEP 8 Style Guide for Python Code. If you’d like to boost your Python skillset, check out our Working with Text Files in Python course. Or learn everything there is to know about Python programming in the 365 Learning Python Programming Guide.

Ready to take the next step towards a career in data science?

Check out the complete Data Science Program today. Start with the fundamentals with our Statistics, Maths, and Excel courses. Build up step-by-step practical experience with SQL, Python, R, and Tableau; and develop in-demand competencies with Machine Learning, Deep Learning, Credit Risk Modeling, Time Series Analysis, and Customer Analytics in Python. If you still aren’t sure you want to turn your interest in data science into a full-scale career, we also offer a free preview version of the Data Science Program. You’ll receive 12 hours of beginner to advanced content for free. It’s a great way to see if the program is right for you. 

Martin Ganchev

Martin Ganchev

Instructor at 365 Data Science

Martin holds an MSc degree in Economic and Social Sciences from Bocconi University. His diverse academic and research experience combined with his friendly and explanatory approach to teaching have made him one of the most beloved instructors on our team. Some of the courses he has authored include: SQL, SQL + Tableau, SQL+Tableau+Python, Introduction to Python, Introduction to Jupyter, to name a few.

Top