Beauty of Python: Zen says it all

Beauty of Python: Zen says it all

It has been a long time that Pythoneer Tim Peters briefly wrote the guiding principles for Python design into 20 aphorisms. Let’s revisit them in simplified way. After all, your choice of Python as a technology is all about simplifying tough things!
1.Beautiful is better than ugly
Write simple expressions. Programs should be human readable and understandable. So keep it consistent. It says that “don’t make me think much”.
Example:
Logical operators – Use of and, or instead of &&, || respectively. Though it is subjective, code seems more readable and beautiful this way.
if (is_valid(a) && b == 0 || s == ‘yes’) {
versus
if is_valid(a) and b == 0 or s == ‘yes’:

2.Explicit is better than implicit
No hidden loop variables. Use Boolean types wisely. Use ‘self’ to refer to object inside a method.
While calling function, each and every time, name its module explicitly.
Example:
import os
print os.getcwd()

instead of this

from os import *
print getcwd()

3.Simple is better than complex.
Python provides interactive interpreter(IDLE) for experimentation purpose. Python has simplest way to print anything.

4.Complex is better than complicated.
There is pythonic way to simplify scientific computations. Python is library rich for the same. For advanced maths and data science, python has scipy, numpy, pandas. Addtionally, python has interactive graphics to visualize the results

5.Flat is better than nested.
Python has standard library structure for each kind of application. E.g. networking, internet protocols, command line, parallel processing, databases
NameSpace is flat. No need to use long names.

6.Sparse is better than dense
Python says, don’t try to fit too much code on one line. Arrange it beautifully. computers have long notepads and editors where one can neatly arrange the code. Python has ‘pip’, that lets you install any package just by writing pip install packagename. Python allows separate release schedules.

7.Readability counts
Python believes that Well-indented code is more readable. So no need to use hundreds of opening and closing brackets everywhere. Python has minimal punctuation and built in extensive documentation.

8.Special cases aren’t special enough to break the rules
Python allows duck typing and run time attribute lookup. Everything is an object.

9.Although practicality beats purity.
Multiple programming models are present in this world such as procedural, object oriented, functional. So, it should not be tough choice to select programming style. Your application says it all.

10.Errors should never pass silently.
Rich exception handling, traceback and debugging.
Unless explicitly silenced. Details of catch are explicit, used when required only.

11.In the face of ambiguity, refuse the temptation to guess.
One cannot add string and numbers. But can multiply them! Coerce types only when not surprising.

12.here should be one– and preferably only one –obvious way to do it.
Eliminate redundancy, simple to learn, easier to remember. How many ways could you provide to “iterate over a sequence” in C++? Iterating over an array with integers or pointers; iterating over STL vectors with various kinds of iterators… etc. If you want to be proficient in C++ (and be able to read other people’s code), you must learn them all. In Python you must learn only one:
for element in sequence:
And isn’t it obvious?
There should be one module for every need.
Although that way may not be obvious at first unless you’re Dutch.
Lots of GUIs and web frameworks are available that makes python obvious
Now is better than never.
Python can easily be integrated with other existing technologies such as c, java using cpython, jython, ctypes. Even there are differnet versions of python itself gives functionality rich facility.
Although never is often better than *right* now.
Not everything goes in stdlib anymore. It supports language moratorium.

13.If the implementation is hard to explain, it’s a bad idea.
As the idiom rightly says, implementation should be easy to explain and simple in practice.

14.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!
Furthermore, namespace is a nice feature of python that allows to store mappings from names to objects. Simply, it is called as a dictionary.

For more interesting readings, refer:
python humor

Leave a Reply

Your email address will not be published. Required fields are marked *