The Zen of Python (PEP-20 easter egg)

One of the earliest Python pep’s is PEP-20, written in 2004. It’s a list of 19 theses or rules relating to 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 that was 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!

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 the way we can get an object’s length in Python. After all, Python has the len() function that works on any object that has 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.

The Zen of Python easer 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!

So as long as you have a Python REPL, you can get these rules on your screen!

About Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

No, thanks!