Like any programming language, PythonA Brief Introduction to Python — the universal programming language has rules that make it function. But, like spoken language, those rules aren’t rigid. You can get your point across, or write great code, in a number of ways. Still, when it comes to coding professionally, there are Python coding standards and best practices that ensure your code is functional and legible. It’s especially important for beginners to learn great coding habits from the beginning. We asked TripleTen expert Jeremy Lehner for his checklist for writing elegant code, no matter what problem he’s solving.
About our expert
After earning his PhD in computational physical chemistry, Jeremy made a career switch into data science after graduating from a bootcamp. He now works at TripleTen, where he creates online data analysis and data science courses on Python development, SQL, time series analysis, and more.
A note about PEP 8
Before we launch into the 5 best practices, a note: although there are no rules on how you write your code, there is a widely accepted set of Python coding guidelines. The Python style guide that most developers refer to is called PEP 8 (“PEP” stands for “Python Enhancement Proposal”). PEP 8 covers a wide range of recommendations and good coding practices. You can read the PEP 8 style guide here.
As a beginner, this is a great set of guidelines to refer to when in doubt. You will rarely be working on code in isolation (for example, most standard libraries use PEP 8). And these guidelines will help you plug right in when working on shared Python projects in externshipsWhy Externships Are Great for Your Portfolio and paid projects.
(There are occasional exceptions to the guidelines: a Python project you're working on may have different conventions, which should be followed for internal consistency. But PEP 8 is a great tool when you’re just starting your coding career).
5 Python best practices every developer needs to know
Here are 5 Python best practices for consistent code quality that Jeremy adheres to. “I use these practices to make sure my code is neat, readable, and easy to understand both for my future self and for anyone else who reads my code,” he says. Bookmark these standards and practices for beginner Python coders:
1. Use descriptive variable names
Jeremy says, “Using variable names that describe what the code physically represents or does helps me quickly decipher code that I haven't looked at in a while.” For example, let's say you want to write code that calculates the square footage of a room, given the length and width of the room in feet (6 ft. x 4 ft. in this case). Bad variable names would look something like this:
If you looked at this code 6 months later, you might not know what any of those values mean. On the other hand, good variables names would be:
Now it's immediately clear what these values are and why you used them! Descriptive naming saves you time and makes it much easier to share code with colleagues.
2. Use variables even for constants
Jeremy says, “Sometimes I'm tempted to use numbers in my code instead of variables, but experience has taught me that it's best to use variables in most situations, even for numbers that do not change.”
For example, you may need to use an approximation of pi to perform calculations. You could use the abbreviated number 3.14 across your code. But, if you ever needed to change the value to be more precise — 3.1416 — it would be a tremendous effort to change each instance in the code. Instead, by creating a variable for pi at the top of your code, you’d only need to make one quick edit.
3. Leave brief and helpful comments
Good comments will also go a long way in helping you understand code that you've written a long time ago or that was written by other people. According to Jeremy, “A good comment is brief and gives context into what a chunk of code is doing, or gives us additional information about variables.” To continue the example from earlier, it would look like this:
Comments (and docstrings) are also helpful to include in functions to describe what the function does, what it expects as input, and what it returns as output.
4. Make good use of whitespace
Using whitespace helps make your code more readable. Examples include: putting spaces between operators and operands, separating function arguments with spaces, and using blank lines to separate pieces of code into blocks.
Here's an exaggerated example of what bad whitespace usage looks like:
The code will still run, but it's hard for a human to read. A much better layout would look like this:
There’s a lot of personal preference in how to use whitespace. Different people will have different ideas about what looks best and why — and that's okay! But that brings us to our final best practice...
5. Be consistent!
Everyone eventually develops their own coding style. (Of course, if you're working for a company that has a particular style for things like comments and whitespace, then go with that.) Whatever style you choose, stick with it throughout your code. This is especially important when multiple people will use and read your code. Consistency will save everyone a headache and leave you plenty of mental energy to write elegant, fresh lines of code.
Learn Python best practices at TripleTen
Have you been tinkering with Python, and feel ready to level up? You can improve your code quality with professional support at TripleTen's bootcamps. Each bootcamp path is staffed with experienced code reviewersWhat Is Code Review? How It Works And How to Ace Your Next Review who will help you write clean code in any programming language. Take our career quiz to explore your strengths and interests, and to see which next step is right for you.