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. 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!
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')vs
if is_valid(a) and b == 0 or s == 'yes':Which is more readable?
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.
if something: if something_again: if something_here:vs
if something: do this if somthing_again: do thisTry to use second approach.
Sparse is better than dense.
Do not try to fit more code in one line. So instead of
if status: print(status)use
if 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
try: mylist = get_items() exception Exception as e: pass return 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.
for 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.