Python Code Readability - What Type of Line is Better?
The skills the authors demonstrated here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.
Introduction to Python Codes
In another article, we discussed how to simplify your Python code for optimal readability. That piece covered why writing simpler code will help with the readerβs comprehension of your syntax and force you to think about problems in a way that is easier to explain to those who may not be as technically savvy. This article will cover how to separate your code into multiple lines and why it will strengthen your Python code.Β
Although you should always try to keep your code concise, you donβt want to do it at the expense of readability. Donβt try to write code all on one line when you can write the same code on multiple lines in a more interpretable way. This will confuse anyone reading it when they try to decipher your dense lines of code.Β
Thereβs a delicate balance here that you should strive to reach. As you become more experienced in coding, you might be more inclined to start condensing things by writing them on one line, but donβt go overboard. Forcing everything onto one line will not always portray you as a more advanced coder. It will have the opposite effect if doing so makes the syntax challenging to read.Β
Donβt confuse aiming for sparsity with reducing the efficiency of your code. Your goal should always be efficiency but try to do so in a way where the code is still easy to read.
Examples of Python Codes - Pt. 1
Letβs go over a few examples of dense code. Then weβll talk about how to improve them.
The first example prints a string that tells the user how a certain number of bytes can represent many possible values. It is an extremely long print statement, which makes it hard to read. Itβs so long that you would need to scroll to the right to view the whole statement!Β
For interpretability, you can break this string into multiple steps. Even though it may be nice to solve a problem using one line of code, you donβt want to make it difficult for someone to read through this line and understand what youβre attempting to do.Β
The second example enters a gray area. This type of coding can be okay, but you should avoid making your conditions as complicated as they are here. You could write this in separate statements as opposed to one long expression.
Examples of Python Codes - Pt. 2
The third example is challenging to read. It uses both map and filter, with multiple lambda functions, in one line of code. It may solve the problem, but it scores no points for readability.
Now letβs fix these examples by making the dense code a little sparser.
The first example shows our long bytes values print statement broken down into multiple steps. This allows the reader to easily follow the step-by-step process, where before they would have to spend some time deciphering the long line of code.
For the second example, we wrote out βif-elseβ statements, which are more logically straightforward than the original expression of nested conditionals. This example can be done either way. What you choose to do comes down to personal preference, but if you want your colleagues to follow your logic easily, the if-else statements accomplish that goal.
The last example is split into multiple steps to make it simpler to read. What makes this example even better is the use of list comprehension. With this, you can do the equivalent of both map and filter operations in a single, logically simple step. Hopefully, this example shows that one-line solutions are not necessarily wrong, but you need to consider your solutionβs complexity.Β
Letβs look at if-else statements for another example. If-else one-liners are generally fine to use if the conditions arenβt too complicated. Below is an example of this. If the conditions get more complicated than this example, you should use the traditional multi-line if-else statements.Β
Conclusion to Python Code Readability
Everything discussed in this article is only one small peek at the much greater topic of optimal code readability. This, along with many other coding best-practices, is stressed in our programs and bootcamps.Β
Ready to advance your programming skills to the next level? 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 data science bootcamps.