The Zen of Python (PEP-20 easter egg)

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.

The 19 rules from the Zen of Python

The 19 rules listed in the Zen of Python are as follows:

  1. Beautiful is better than ugly.
  2. Explicit is better than implicit.
  3. Simple is better than complex.
  4. Complex is better than complicated.
  5. Flat is better than nested.
  6. Sparse is better than dense.
  7. Readability counts.
  8. Special cases aren’t special enough to break the rules.
  9. Although practicality beats purity.
  10. Errors should never pass silently.
  11. Unless explicitly silenced.
  12. In the face of ambiguity, refuse the temptation to guess.
  13. There should be one – and preferably only one – obvious way to do it.
  14. Although that way may not be obvious at first unless you’re Dutch.
  15. Now is better than never.
  16. Although never is often better than right now.
  17. If the implementation is hard to explain, it’s a bad idea.
  18. If the implementation is easy to explain, it may be a good idea.
  19. 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:

  1. %-formatting
  2. Using str.format()
  3. 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!

Get certified with our courses

Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

Leave a Comment