How to Simplify Your Python Code for Optimal Readability
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 a previous article, we discussed how and why you should be explicit while writing your Python code. That covered another avenue of improving your Python codeβs readability, including naming conventions, calling objects from other modules and packages, and more. In this article, weβll be discussing why writing simpler code will help not only with the readerβs comprehension of your syntax, but it will also force you to think about problems in a way that is easier to explain to those who may not be as technically savvy.
You should always try to write simple rather than overcomplicated code. Recognizing when you can simplify your code comes with practice and experience. There are many built-in Python functions that you can use to enable simplification. Try to become familiar with these functions to avoid needing to reinvent the wheel. Remember that you donβt need to memorize these functions and what they do, but you must know they exist so you can look them up when theyβre needed. Ignorance is your greatest enemy when youβre a programmer; it makes working on problems much more difficult.
Bad Examples
All of the examples in the cell above try to perform operations from scratch. While writing your Python code, youβll want to look for more βPythonicβ ways to write. Your syntax will look more professional and, at the same time, be easier to read and understand.Β
To avoid wasting time and effort, donβt try to reinvent how something works. There are almost always existing functions to use.
In the first example above, weβre trying to compute a cumulative sum. What is written here might look good to a novice programmer, but a more experienced coder will recognize a sum function that can be implemented.Β
Trying to recreate functions from scratch will also cause your code to run slower than the code that is already implemented. Many of the built-in functions in Python are written in a more machine-level language that is much faster and more optimized for these operations. In this way, there will be an efficiency advantage when using built-in functions rather than defining your own. This efficiency can also be seen when using functions and methods in the NumPy and pandas packages. Sometimes you will need to define your own function when you need to accomplish a specific task, but try to use already defined functions when possible rather than creating your own.
The fourth example is from CodeWars, which is an excellent website for coding challenges. There is a class called Counter within the collections module thatβs great for turning an iterable of elements into a dictionary representing the frequency of each unique element.Β
There is a more straightforward way to do this, though, shown in #4 in the following example.
Good Examples
All of the examples above use built-in Python functions to simplify the code. The zip function is another handy tool. It takes two iterables, then zips them together, and uses the dict function to convert the tuples of key/value pairs into a dictionary. We stress the usage of the zip function in much more detail in our data science bootcamps.Β
Itβs important to implement these built-in Python functions in your code to ensure optimal readability and be as efficient as possible, to make sure your code looks clean and organized.Β
The way you can use what youβve learned in your work is simply by practicing. Always make a note of the different functions and understand that theyβre available to you. Again, itβs unnecessary to memorize them all, but itβs essential to know that they exist.Β
For example, suppose youβre working on a project and remember that the string class has a method that counts the number of some characters within a string. In that case, you can quickly look that function up on Google or StackOverflow or any other mass information repository. Knowing that these functions exist makes them available to you, even if you donβt have them memorized. Itβs acceptable to use Google to find more information about code; experienced programmers do it all the time! Itβs a great way to discover new techniques and tools.
Though weβve gone into some detail here, this is only scratching the surface of optimal code readability. We go into much deeper detail 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.