Skip to main content

Zen of Python


Writing programs that actually do what they are supposed to do is just one component of being a good Python programmer.
It's also important to write clean code that is easily understood, even weeks after you've written it.

One way of doing this is to follow the Zen of Python, a somewhat tongue-in-cheek set of principles that serves as a guide to programming the Pythoneer way.

So the Zen of Python is a listing of python principles and philosophies that are helpful in understanding and using language effectively.

Use the following code to access the Zen of Python.

import this

If you run this code, it will print the following text out:
>>>
The Zen of Python, by Tim Peters

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!
>>>

Some lines in the Zen of Python may need more explanation.
Explicit is better than implicit: It is best to spell out exactly what your code is doing. This is why adding a numeric string to an integer requires explicit conversion, rather than having it happen behind the scenes, as it does in other languages.

Flat is better than nested: Heavily nested structures (lists of lists, of lists, and on and on…) should be avoided.

Errors should never pass silently: In general, when an error occurs, you should output some sort of error message, rather than ignoring it.

The line "There should be one - and preferably only one - obvious way to do it" references and contradicts the Perl language philosophy that there should be more than one way to do it.

There are 20 principles in the Zen of Python, but only 19 lines of text.
The 20th principle is a matter of opinion, but my interpretation is that the blank line means "Use whitespace".

Comments

Popular Posts

FAT32 File Sorter

FAT32Sorter is an Android app that helps you to sort mp3 files on your memory card formatted with FAT/FAT32 file system. The idea came from one of my friends (A.G), he is using an Mp3 Player device, and when he copy some mp3 files to Mp3 Player Memory card, he found that files is not sorted alphabetically despite he was renamed them like this: 000.mp3,001.mp3...999.mp3. So after doing some research on the internet, i've found that the problem is with FAT32 File system, and i discovered that the FAT file system is sorting files as its precedence in copying (by Last modified date). So if we moves files from sdcard to any other memory (internal or phone memory) and reading them in Array list, then sort this array alphabetically and copying them back to sdcard as they sorted in the array... now files will be sorted. I've wrote this app on android phone with AIDE in Java programming language, it consists of 2 classes: - MainActivity class (App interface functions) - ‎F...

The Host Consciousness Simulator: Reinforcement Learning and the Path of Suffering in Westworld

The Host Consciousness Simulator: Reinforcement Learning and the Path of Suffering in Westworld An analysis on the painful journey to synthetic free will. From the moment I first witnessed the philosophical whirlwind of Westworld , I couldn't stop asking myself: Is suffering the only price for free will? Does a machine have to feel pain and remember it to break free from its programmed loop? This specific question is what drove me to analyze a fascinating simulator that puts this very theory to the test. Today, we're diving deep into the concept of this application, which acts as a virtual "laboratory" for the mechanism of consciousness acquisition in Hosts. Join me as we explore how Reinforcement Learning (RL) , through the inverse rewar...