The Zen of Python

Python is one of the programming languages that are very versatile and relatively easy to learn. Hence it is the choice of many new programmers, regardless of what area of tech they are interested in. It is particularly popular in all data science branches.



The Zen of Python
Imag by Editor

 

Despite the relative simpleness of Python, writing good, readable, maintainable Python code is challenging. Luckily for all Python enthusiasts, Python comes with its own wisdom, or what's known for all Pythonistas as "The Zen of Python." 

The Zen of Python is a nice, short poem presenting guidelines for writing high-quality Python code.

Now, in case you have never heard of it, you can read The Zen of Python by running this line of code:

import this

 

Which will print out the poem of Python mastery.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!


Though the poem offers clear and straightforward guidelines for writing succinct Python code, let's talk about some free resources that guide you through the different verses of the poem with examples.

The first seven verses in the poem are all about style. Python is about simplicity, so if you want to learn how to write simple Python code with good style, look no further than the Hitchhiker's Guide to Python. It will give you the basics of how to style your code. Another tremendous and short article about optimizing your Python code is "How to Simplify Your Python Code for Optimal Readability" by Zoe Zbar and Alex Baransky in NYC Data Science Academy. Another option is The Real Python YouTube video about Writing Pythonic Code - Idiomatic Python. This 9 mins video will guide you through writing good, idiomatic Python code.

The next verse is about handling errors and exceptions. The assert statement is your friend in catching errors and not allowing them to break your code. Python Exception Handling: AssertionError article by Frances Banks in AirBrake covers how to use the assert statement optimally. The exception Handling In Python YouTube tutorial by Edureka offers a broader view of handling errors in Python.

This brings us to the poem's end, going for the simple and clear way of solving a given problem. In programming and life, there are many ways of handling problems. Some are better and more straightforward than others. Finding the most optimal solution to a programming problem is a skill beyond Python mastery. Geeksforgeeks's "How To Approach A Coding Problem?" article about approaching and solving coding problems will help you build the intuition you need to address any problem effectively.

The Zen of Python is just a set of 19 guidelines for writing good code (not just in Python). Tim Peters originally wrote the poem in his Python mailing list in 1999. Writing good Python code takes practice; the more code you write, the better your style will be. So, remember, try to follow the guidelines, and your code will be zen-full!

 
 
Sara Metwalli is a Ph.D. candidate at Keio University researching ways to test and debug quantum circuits. I am an IBM research intern and Qiskit advocate helping build a more quantum future. I am also a writer on Medium, Built-in, She Can Code, and KDN writing articles about programming, data science, and tech topics. I am also a lead in the Woman Who Code Python international chapter, a train enthusiast, a traveler, and a photography lover.