Best Naming Conventions When Writing Python Code
Posted by Zoe Zbar
Updated: Sep 8, 2020
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 Fall.
Zoe Zbar
View all articlesTopics from this blog: python Events #trainwithnycdsa
Subscribe Here
Posts by Tag
- Meetup (101)
- data science (68)
- Community (60)
- R (48)
- Alumni (46)
- NYC (43)
- Data Science News and Sharing (41)
- nyc data science academy (38)
- python (32)
- alumni story (28)
- data (28)
- Featured (14)
- Machine Learning (14)
- data science bootcamp (14)
- Big Data (13)
- NYC Open Data (12)
- statistics (11)
- visualization (11)
- Hadoop (10)
- hiring partner events (10)
- D3.js (9)
- Data Scientist (9)
- NYCDSA (8)
- Web Scraping (8)
- Career (7)
- Data Scientist Jobs (6)
- Data Visualization (6)
- Hiring (6)
- Open Data (6)
- R Workshop (6)
- APIs (5)
- Alumni Spotlight (5)
- Best Bootcamp (5)
- Best Data Science 2019 (5)
- Best Data Science Bootcamp (5)
- Data Science Academy (5)
- Demo Day (5)
- Job Placement (5)
- NYCDSA Alumni (5)
- Tableau (5)
- alumni interview (5)
- API (4)
- Career Education (4)
- Deep Learning (4)
- Get Hired (4)
- Kaggle (4)
- NYC Data Science (4)
- Networking (4)
- Student Works (4)
- employer networking (4)
- prediction (4)
- Data Analyst (3)
- Job (3)
- Maps (3)
- New Courses (3)
- Python Workshop (3)
- R Shiny (3)
- Shiny (3)
- Top Data Science Bootcamp (3)
- bootcamp (3)
- recommendation (3)
- 2019 (2)
- Alumnus (2)
- Book-Signing (2)
- Bootcamp Alumni (2)
- Bootcamp Prep (2)
- Capstone (2)
- Career Day (2)
- Data Science Reviews (2)
- Data science jobs (2)
- Discount (2)
- Events (2)
- Full Stack Data Scientist (2)
- Hiring Partners (2)
- Industry Experts (2)
- Jobs (2)
- Online Bootcamp (2)
- Spark (2)
- Testimonial (2)
- citibike (2)
- clustering (2)
- jp morgan chase (2)
- pandas (2)
- python machine learning (2)
- remote data science bootcamp (2)
- #trainwithnycdsa (1)
- ACCET (1)
- AWS (1)
- Accreditation (1)
- Alex Baransky (1)
- Alumni Reviews (1)
- Application (1)
- Best Data Science Bootcamp 2020 (1)
- Best Data Science Bootcamp 2021 (1)
- Best Ranked (1)
- Book Launch (1)
- Bundles (1)
- California (1)
- Cancer Research (1)
- Coding (1)
- Complete Guide To Become A Data Scientist (1)
- Course Demo (1)
- Course Report (1)
- Finance (1)
- Financial Data Science (1)
- First Step to Become Data Scientist (1)
- How To Learn Data Science From Scratch (1)
- Instructor Interview (1)
- Jon Krohn (1)
- Lead Data Scienctist (1)
- Lead Data Scientist (1)
- Medical Research (1)
- Meet the team (1)
- Neural networks (1)
- Online (1)
- Part-time (1)
- Portfolio Development (1)
- Prework (1)
- Programming (1)
- PwC (1)
- R Programming (1)
- R language (1)
- Ranking (1)
- Remote (1)
- Selenium (1)
- Skills Needed (1)
- Special (1)
- Special Summer (1)
- Sports (1)
- Student Interview (1)
- Student Showcase (1)
- Switchup (1)
- TensorFlow (1)
- Weekend Course (1)
- What to expect (1)
- artist (1)
- bootcamp experience (1)
- data scientist career (1)
- dplyr (1)
- interview (1)
- linear regression (1)
- nlp (1)
- painter (1)
- python web scraping (1)
- python webscraping (1)
- regression (1)
- team (1)
- twitter (1)