Skip to main content

Command Palette

Search for a command to run...

The Zen of Python

Updated
4 min read
The Zen of Python

Python was introduced into the programming scene in 1991 by Guido van Rossum in the Netherlands as a successor to the ABC programming language.

Improvements and new releases have ever since been made despite Rossum leaving the team and giving up his title as "Benevolent Dictator for Life" of python on July, 2018 when he announced his retirement. (from the name I guess he still holds the title though)

Without getting to all things python and it's cool features, I'll deep dive into what's called the Zen of Python, a concept unique to python and sparks curiosity vis-a-vis the python language.

The Zen of Python is a set of 19 guidelines introduced by Tim Peters, an American software developer and big contributor to the CPython framework. He came up with these guidelines that define programming best practices especially in the implementation of Python.

To view the zen of python locally:

  1. Create a main.py file

  2. Copy the following code to you main.py file:

import this
  1. Run the file in the terminal as python3 -m main.py

  2. Output:

The Zen of Python by Tim Peters

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!

Below I give insight on every line of the Zen

"Beautiful is better than ugly." - Besides functionality, code should also be easy to read and aesthetically pleasing.

"Explicit is better than implicit." - Code should be obvious from any reader's point of view (another programmer of course) rather than relying on background behaviors.

"Simple is better than complex." - Code implementation should be simple while maintaining optimum functionality.

"Complex is better than complicated." - If code complexity is at all necessary make it straightforward.

"Flat is better than nested." - Code structure should avoid exaggerated nesting of control structures.

"Sparse is better than dense." - This emphasizes on paragraphing code to enhance readability.

"Readability counts." - Quite straightforward; code should be readable.

"Special cases aren't special enough to break the rules." - Python comes with guidelines and conventions of writing code; manipulating code to go around the conventions in an attempt to execute a "special" behaviour is therefore not an option.

"Although practicality beats purity." - Well practically achieving a behaviour under restricting guidelines may be hard, this zen gives room for special case code to simplify the work at hand.

"Errors should never pass silently." - Exceptions and errors must never be ignored even if it doesn't affect output.

"Unless explicitly silenced." - Suppressing exceptions is the next alternative to managing hard to handle exceptions.

"In the face of ambiguity, refuse the temptation to guess." - Coding should be intentionally rather than trial and error to accomplish a behaviour.

"There should be one -- and preferably only one -- obvious way to do it." - Code should be consistent all along a program and establish flow down the lines.

"Although that way may not be obvious at first unless you're Dutch." - This is a more of a humorous pun directed at Rossum, creator of Python, who is Dutch.

"Now is better than never." - Implement a solution as it comes; don't be hard on perfection which may never be achieved.

"Although never is often better than right now." - Trying to be perfect is not necessary but also take time to implement it's best attainable code solution.

"If the implementation is hard to explain, it's a bad idea." - Code should be made easy to explain the execution flow.

"If the implementation is easy to explain, it may be a good idea." - Code that's easy to explain is by high chance well Implemented.

"Namespaces are one honking great idea -- let's do more of those!" - Variables and other namespaces that could be used globally in other parts of the code parts of the code is always better than constantly defining code.


The Zen of Python is not necessarily a contract with consequences if broken but rather a friendly guideline to any developer out there; novice or expert. So take your time on every Zen.

✌️

More from this blog

All Things Tech

2 posts

A technologically-adept explorer of the digital realm. With my boundless passion for the command of the elusive language of code, success in cyber inovations is nothing less part of my script.