Zen of Python: how to write good python code?



Zen of Python is 19 guiding principles for writing good python code.

It was created on 19-Aug-2004 by Tim Peters. You can access this brief set of principles for writing good Python code by entering >>>import this into your interpreter.

If you want to be a good python coder, you should be familiar with the zen of python which has 19 guidings principles.

All the 19 guiding principles for writing good code in python is listed below:

1. Beautiful is better than ugly.

Well-designed codes to solve problems are beautiful.

2. Explicit is better than implicit.

3. Simple is better than complex.

Simple code will be easier to maintain, and it will be easier for you and others to build on that code later on.

4. The complex is better than the complicated one.

Always use the simplest solution that works to solve messy problems.

5. The flat is better than nested.

6. Sparse is better than dense.

7. Readability counts.

Even when your code is complex, aim to make it readable. When you’re working on a project that involves complex coding, focus on writing informative comments for that code.

8. Special cases aren’t special enough to break the rules.

9. Although practicality beats purity.

10. Errors should never pass silently.

Always catch an exception raised by the program. No error should 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.

Effective and optimized code is more preferable to fairly compatible solutions.

14. Although that way may not be obvious at first unless you’re Dutch.

15. Now is better than never.

No matter whether your code is perfect or not, but you need to start coding now. Earlier the better. As you continue to the next chapter and start digging into more involved topics, try to keep this philosophy of simplicity and clarity in mind.

16. Although never is often better than *right* now.

17. If the implementation is hard to explain, it’s a bad idea.

Always focus on writing self-explanatory code, precise, and readable code. Include comments while coding.

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!

Read why you have to learn python.

Read about the zen of python on the python official page.