The Zen of Python is a collection of 20 software principles that influences the design of Python Programming Language.
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.
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!
Zen Of Python:
Zen of Python is written as an informational entry number 20 in Python Enhancement Proposals (PEP). This is also included as easter egg in python interpreter which can be invoked by typing import this .
- Beautiful is better than ugly.
Code should be more readable and beautiful. Compare below code lines.
if (is_valid(a) && b == 0 || s == 'yes')
if is_valid(a) and b == 0 or s == 'yes':
- Explicit is better than implicit.
Always use module name with function call so that it is explicitly known which module this function belongs to. So instead of myfunction() , use mymodule.myfunction() .
- Simple is better than complex.
Do not write complex code. Your code should be readable. Consider this your code will be used by some psycho killer who knows where you live. Simple code may be lengthier but remember you have to write once but read/maintain the code always.
- Flat is better than nested.
Do not use loops inside loops and again inside loops. Or do not use if-else inside if-else inside if-else. Maximum indentation level should not exceed four. Write flat code. Flat code is easy to maintain and read.
Python123if something:if something_again:if something_here:
Python1234if something:do thisif somthing_again:do this
- Sparse is better than dense.Do not try to fit more code in one line. So instead of
Python1if status: print(status)
- Readability counts.
This is simple. Use indented code. Use comments. Read above aphorism.
- Errors should never pass silently.
Always log or raise exceptions. Do not do this
Python12345try:mylist = get_items()exception Exception as e:passreturn mylist
- In the face of ambiguity, refuse the temptation to guess.
Do not write ambiguous code. Do not leave room of guessing. For example before storing data to database, make sure you find out the charset of data and then decode data into required charset and then store.
- here should be one, and preferably only one way to do it.
In python all sequences are iterated in one way.
Python1for item in sequence:
Rest are easy I think. Let me know what you think of this. Correct me If you think something is not explained correctly.