Zen of Python

importostar

importostar

Posted on June 21, 2020

Zen of Python

Zen of Python is a set of 19 "core values" that guide the structure of the Python language for computer programs.

There's an easter egg in Python, you can use import this to load the "Zen of Python".

The core values 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 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.[a]
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!

As usual with the internet, things evolve. For example, this became a song and a visual studio code extension

Eitherway, this philosophy is the core of Python. You can see the proposal from 2004 PEP20

Beautiful is better than ugly
Use OR instead of ||, use AND instead of ||

Explicit is better than implicit
Code is readable by anyone

Simple is better than complex
a simple solution is always better than a complex one. So you have print("Hello") instead of sys.out.println("hello")

Flat is better than nested.
When something is not flat, it's nested. A nested loop for example is

for a in apples:
    for b in breads:
        for c in codes:

you can see this gets confusing fast, so flat is better than nested.

Readability counts
If readability didn't matter, we'd all be writing in binary code. Readability matters when it comes to programming

You can see all these zen principles are part of the Python design.

💖 💪 🙅 🚩
importostar
importostar

Posted on June 21, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related