How to write beautiful Python code: The Zen of Python & PEP-8



The Zen of Python-How to write beautiful Python code


Writing clean and beautiful code is really impressive and very advantageous. Writing code with clarity acts as a strong foundation for your code and improves your relationship with the code readers. While programming the first mistake is that you forget that you have to read the code again. Developing habits to write elegant codes is important and a principle called The Zen of Python to discuss the various principles for writing beautiful codes.
The Zen of Python-How to write beautiful Python code
The Zen of Python is a collection of 19 programming principles for writing beautiful and clean python codes. In 1999 Software Engineer Tim Peters first wrote this collection and posted it in the python mailing list. He left the 20th position to filled in by others. Technically he wrote " for Guido to fill in " at the 20th position referring to the great python developer also the author of the famous book Python Programming Language Guido Van Rossum. This collection is also included in the Python Enhancement Proposal (PEP) at number 20. It is also included in python interpreter which you can access by:
import this

The principles in that collections are:

  • 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 are not special enough to break the rules.
  • Although practicality beats purity.
  • Error should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • Ther should be one-- and preferably only one --obvious way to do it.
  • Although that way may not be obvious at first unless you are Dutch.
  • Now is better than never.
  • Although never is better than *right * now.
  • If the implementation is hard to explain it is a bad idea.
  • If the implementation is easy to explain it may be a good idea.
  • Namespaces are one honking idea --let's do more of those!

PEP-8

PEP stands for Python Enhancement Proposal. PEP-8 is a python style guidelines that is adapted from Guido's original style guideline and some of the Barry's Guide[1].  It provides the best practices and guidelines on how to write beautiful python codes. It was first created by Guido Van Rossum, Barry Warsaw and Nick Coghlan in 2001. And it is continuously evolving by time. The main goal is to increase the readability and consistency of python code. I will mention some basics of this principle and you have to practice in detail on your own. 

I haven't mentioned some advanced topics and guides here in this article you can learn in detail about the PEP 8 here.


Post a Comment

Previous Post Next Post