89DEVs

The Zen of Python by Tim Peters - command

The Zen of Python is an easter-egg in the Python programming language. It includes 20 guiding principles for good and clean Python code. 

zen of python
The Zen of Python is shown after typing import this

Clean code saves time

Why is it so important to write clean code? Even bad code can work. But if a developer works in a team or a long-term project, the code needs to be maintainable. You should be able to understand the code after years of not looking at it within seconds, not minutes. The biggest advantage of clean code is the time you can save debugging it. Code is never perfect the first time it is written and debugging is just a natural process. It is much easier to debug clean and well written code and the time saved can be invested in much more fun things.
clean code quote
clean code quote by Brian W. Kernighan
And it's also important to consider that other people read, maintain, and change your code. So the better written your code is, the better for everyone in the team. That's where the Zen of Python can help.

command to print Zen of Python

How to print the Zen of Python? Use the following command to show the Zen of Python. import this The command imports the this module, which will show the Zen of Python. You can save the Zen of Python as string or in a file.

The Zen of Python, by Tim Peters

The Python interpreter will then show the Zen of Python: 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!

How to save the Zen of Python?

We can save the Zen of Python as string using the following code. # save zen of python as string import this zen_of_python = "".join([this.d.get(c, c) for c in this.s]) We can also save this string to a file. # write zen of python to file f = open("zen_of_python.txt", "w") f.write(zen_of_python) f.close()

What are PEPs in Python?

A PEP in Python is a Python Enhancement Proposal. It is a document that describes new features or other aspects like design and safety to the Python community. The Zen of Python is PEP 20 and was created on 19th August 2004.

Who wrote Zen of Python?

Tim Peters wrote the Zen of Python. He is a software engineer and most known for the Timsort hybrid sorting algorithm. He is a longtime contributor to the Python programming language and quite active on Stack Overflow. In this video, you can see an interview with Tim Peters at Pycon 2006 in Dallas.

Poster and Wallart

Looking for some Wall Art? The Zen of Python is available as a poster. For example on Etsy or REDBUBBLE, some artists are selling framed prints with programming content. A Zen of Python poster can definitely improve any office atmosphere and help to make sure that no one in the team forgets the essential principles of Zen.
zen of python wallart
Zen of Python wallart on REDBUBBLE

translations

The Zen of Python was translated into different languages by contributors. If you want to contribute a translation into your native language, please contact us and we will get in touch with you to publish the translation here.
language link contributor
german Zen of Python deutsch 89DEVs
estonian Zen of Python eesti anonymous

source code

The source code of the import module, which is used to show the Zen of Python is shown below. The source code is located in the this.py file in the lib directory. If we look at the source code, we can see why some people say the Zen of Python is just a joke. The file is written in the most unpythonic way one can imagine. The string s is encoded in ROT13 and the code below is used to encode the string and print the content. # zen of python source code -- this.py s = """Gur Mra bs Clguba, ol Gvz Crgref Ornhgvshy vf orggre guna htyl. Rkcyvpvg vf orggre guna vzcyvpvg. Fvzcyr vf orggre guna pbzcyrk. Pbzcyrk vf orggre guna pbzcyvpngrq. Syng vf orggre guna arfgrq. Fcnefr vf orggre guna qrafr. Ernqnovyvgl pbhagf. Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf. Nygubhtu cenpgvpnyvgl orngf chevgl. Reebef fubhyq arire cnff fvyragyl. Hayrff rkcyvpvgyl fvyraprq. Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff. Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg. Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu. Abj vf orggre guna arire. Nygubhtu arire vf bsgra orggre guna *evtug* abj. Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn. Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn. Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!""" d = {} for c in (65, 97): for i in range(26): d[chr(i+c)] = chr((i+13) % 26 + c) print("".join([d.get(c, c) for c in s]))

                
        

Summary


Click to jump to section