One of the earliest Python PEP’s is PEP-20, written in 2004. It lists 19 theses or rules for Python programming called The Zen of Python. The Python language is built around these guiding principles, and the rules are, in turn, inspired by PEP-8, the original style guide for Python written by Guido van Rossum himself.
The writer of the Zen of Python, long-time Pythoneer Tim Peters, left the 20th rule empty. His idea was for Guido to contribute number twenty, but that never happened.
Table of Contents
The 19 rules from the Zen of Python
The 19 rules listed in the Zen of Python are as follows:
- 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!
How the Zen of Python is visible throughout Python
As mentioned, these rules are partly inspired by PEP-8, the Python style guide written by Guido van Rossum. To this day, Python developers, especially those working on the Python project itself, try to adhere to these principles.
As an example of how these rules apply to the Python language, we can look at rule 13 (there should be one – and preferably only one – obvious way to do it). It is clearly applied to how we can get an object’s length in Python. After all, Python has the len() function that works on any object with a length, giving us only one clear way to get the length of an object. In contrast, some other languages that don’t have such a defined way of doing things end up with a myriad of object methods like object.length()
, object.size()
, etcetera, all with slightly different names depending on the one who implemented it.
It’s not all rainbows and sunshine, however. As an example, there are now three ways to format strings in Python:
- %-formatting
- Using str.format()
- f-strings
Python devs kept finding new and better ways to do this over the years but had to keep the old ways intact for compatibility.
The Zen of Python easter egg
A little Easter egg that has been present in Python for a long time lists the Zen of Python. You can trigger the easter egg by importing the module this
. If you do so in a REPL, you’ll see this:
>>> import this
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!
Code language: plaintext (plaintext)
So as long as you have a Python REPL, you can get these rules on your screen!