The Zen of Python is a set of guiding principles for writing computer programs in the Python language. Authored by Tim Peters, these principles emphasize simplicity, readability, and the importance of explicitness over implicitness. The Zen of Python can be accessed directly in Python by typing import this
in the Python interpreter.
Here are some key principles from the Zen of Python:
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Readability counts.
The Zen of Python provides a philosophy that encourages developers to write clean, understandable, and maintainable code. These principles have become a cornerstone of Python’s design and are widely respected in the Python community.
For the full list of principles, you can refer to the Zen of Python by using the Python interpreter or exploring Python documentation.
these nearly twenty theses got a proper name and were formally codified in a Python Enhancement Proposal document. Each PEP document receives a number. the Zen of Python received the number PEP 20 to signify the incomplete number of aphorisms in it.
Tim Peters introduced very interesting 20 new “guiding principles”. But only 19 principles have been officially written and accepted, which influences in Python programming language development.
The last one- 20th principle, “for Guido to fill in” were not considered which was referred to Guido Van Rossum, the original author of the Python language.
Import this ?
Import this!
What?
Tim Peters says, – just import this!
It means importing “this” package – The Zen of Python theory! Go ahead and see the result by firing command “import this” on your python shell or command prompt or Python Jupyter Notebook!
import this
The Zen of Python, by Tim Peters!
>>> 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!
You must have wondering from where this package is getting imported!
You can easily find this! Since this is PEP officially accepted, this.py file is available with standard python set up.
Where is this.py?
File location might be differ depending on your installation and operating system. But you can see the details by using this command.
Here is my this.py file located.
Tip: You must execute “import this” command first, otherwise you will get error while running “this” command.
What is exactly inside this.py?
Ideally, it should be exactly same lines written with print command, I assumed. But there is totally different content present in the this.py file, may be in different language!
Go to file location, write click and open (this.py) with IDLE , text editor, notepad++ or any other editor to see the content.
Interestingly, it has very strange text which even we can not read it properly!
this.py
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]))
If you are also lazy like me to find out location for this.py, navigate to that location, write click and open with idle or text, etc.
Here is the trick for you!
print(this.s)
Let’s play with this !
While going through the Zen of Python notes, I have got something very interesting which you might love it!
May be it’s good idea for you to try and come up with the new interesting lines, I would love to read from you!
Here you go with my collections!
PS: Before you begin do not forget to run “import this” command if you are not already.
you-love-this
>>> love=this
>>> you=love
>>>
>>> love is love
True
>>> you is you
True
>>> love is you
True
>>> this is not you
False
>>> love is not you
False
>>> you is not love
False
>>> love is this
True
>>>
What is opposite of this ? May be THAT !
Daniel Greenfeld come up with the sort of opposite theory of Zen of python called as The Anti-Zen of Python! This is exactly opposite of what Time Peter have introduced. Game of words!
Warning:
This is a joke. Don’t use these bits unless you want to be hated. :-p
The Anti-Zen of Python
You can have a look at the principles on your shell by importing that using command “import that”
“That” package is not distributed with standard Python. Hence you have to install it manually.
If you still attempt to run import that then you will get this error – ModuleNotFoundError: No module named ‘that’.You need to install that package using pip.
How to Install THAT package?
Open command prompt/terminal and run following commands
$ pip install that
$ python
>>> import that
H:>pip install that
Collecting that
Downloading https://files.pythonhosted.org/packages/64/72/12dc208c9086e2584f15f681fcd03c74179341d0295a6/that-1.0.7.tar.gz
Installing collected packages: that
Running setup.py install for that ... done
Successfully installed that-1.0.7
You are using pip version 18.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
H:>
Oops..!
I’ve got the warnings to upgrade the pip version. Don’t worry, if you also get the same error. Let’s continue to upgrade it or you may skip this!
H:>python -m pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df25dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 6.4MB/s
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip-18.1
Successfully installed pip-19.0.3
H:>
The Anti-Zen of Python, by Daniel Greenfeld
import that
>>> import that The Anti-Zen of Python, by Daniel Greenfeld Ugly is better than beautiful.
Implicit is better than explicit.
Complicated is better than complex.
Complex is better than simple.
Nested is better than flat.
Dense is better than sparse.
Line code counts.
Special cases are special enough to break the rules.
Although purity beats practicality.
Errors should always pass silently.
Spelchek iz fur loosers.
In the face of ambiguity, one guess is as good as another.
There should be many ways to do it.
Because only a tiny minority of us are Dutch.
Get things running, then fix them later.
If the implementation is hard to explain, it's enterprisey.
If the implementation is easy to explain, it won't take enough time to do.
Namespaces are too hard, just use "from module import *"!
>>>
Conclusion
In summary, the Zen of Python serves as a guiding philosophy for writing clean, readable, and maintainable code, emphasizing simplicity, clarity, and practicality.
Its aphorisms provide a framework for developers to create software that is not only effective but also elegant.
Conversely, the Anti-Zen of Python highlights behaviors and practices that undermine these principles, reminding us of the pitfalls to avoid in our coding journey.
By embracing the wisdom of the Zen and consciously steering clear of the Anti-Zen, developers can strive to achieve code that not only functions well but also stands the test of time.
Ultimately, understanding and applying these guiding principles helps in fostering a Python community that values thoughtful craftsmanship and continuous improvement.
ADDITIONAL RESOURCES: