April 8, 2022 • 2 min read

The Zen of Python - Towards Better Python Code

Rédigé par Mathieu Soul

Mathieu Soul

Writing clean and elegant Python is an art, and an essential one in producing quality code. Thankfully, the Python founders have left behind a set of guiding principles to aid developers in this endeavour: the Zen of Python.

What is the Zen of Python?

Your first step on the path to pythonic illumination is to find the Zen. Using your favorite Python interpreter, simply type:

Finding your Python Zen

Lo and behold, the Zen appears!

The Python Zen itself!

Written in 1999 by one of Python’s core developers, Tim Peters, the Zen of Python is a list of 19 aphorisms that state Python’s design philosophy. 5 years later, it had become such a standard amongst developers that PEP 20 (Python Enhancement Proposal) incorporated it into the official Python literature, creating the easter egg shown above.

Let’s take a look at the Zen’s philosophy, and how it can apply to our development practices!


To write clear code is to write code that is easy to perceive, understand, or interpret.

If you are a developer, chances are you’re going to be spending over 8 hours a day looking at code. This brings me to the first line of the Python Zen:

Beautiful is better than ugly. (1)

And of course the seventh:

Readability counts. (7)

In practice, several tools and standards can help follow these principles, from naming conventions (PEP 8) to code formatters (black).

It’s important to put yourself in an uninformed (but Python fluent) person’s shoes when evaluating your code, since “code is read much more often than it is written” (Guido Van Rossum, Python’s benevolent dictator for life).


Writing explicit code means writing code that leaves no room for doubt. Two parts of the Zen detail this concept:

Explicit is better than implicit. (2)
There should be one-- and preferably only one --obvious way to do it. (12)

For example, a first practical way of making your code more explicit is to use type hints (introduced in PEP 484) and automatic type checkers (mypy).

Making Python variable types explicit with type hints

This concept applies even more to error handling. If you’ve ever debugged code, you know that having no doubt on the cause/location of your error save you a lot of time and effort. So says the Zen!

Errors should never pass silently.
Unless explicitly silenced. (10-11)


Modularity is the idea dividing up your code into components that can be separated and recombined to allow for greater flexibility and independence.

Simple is better than complex. Complex is better than complicated. (3-4)

The difference between complex and complicated is essential here:

  • A complex code is hard to understand because it involves a large number of different but related entities.
  • A complicated code is hard to understand in itself.

The key takeaway here is: if you’re coding something difficult, break it down into simpler parts. Even if your code ends up being complex, each small part will be easier to discover Surgical Time Tracking in Python, debug and unit test Pyspark Project with Pytest!


In the real world, developers operate under constraints (business realities, time, budget...) while delivering a quality product that remains viable on the long term.

Developers must often be pragmatic in balancing out the best way and the fastest way of reaching the objective. This compromising attitude is a key element of the Zen of Python:

Special cases aren't special enough to break the rules.
Although practicality beats purity. (9-10)

Now is better than never.
Although never is often better than *right* now. (16-17)

Sometimes, this means writing a script to do you one-shot operation versus creating a full repository pattern for your data source. At other times, it can mean prioritising a code refactoring or a coverage improvement over starting a new feature!

Using the Zen

You’ve now been introduced to the Zen of Python: may its concepts guide you through your development journey! Keeping it in mind when making technical choices should set you on the path to clean and pythonic code.

For more details on our code best practices and the way we work, don’t hesitate to contact us! You can already check out this cool article on orchestrating Azure Durable Functions for ETL !

If you want to know more contact-us

Cet article a été écrit par

Mathieu Soul

Mathieu Soul