Back to articles list Articles
8 minutes read

What Is The Zen of Python?

Have you heard about the Zen of Python? These are the guiding principles that every Python programmer should know; they are as concise and beautiful as Python itself. Let’s explore these principles together.

The Zen of Python is a collection of 19 guiding principles that influence the design of Python. The principles were written by software engineer Tim Peters; he wanted Guido van Rossum, the creator of Python, to add a 20th principle. However, this never happened, so the current number stands at 19. Even so, this set of principles is also referred to as PEP-20.

PEPs (Python Enhancement Proposals) are documents providing important information to the Python community, or describing a new feature for Python. In the case of PEP-20, it is just a list of these 19 principles. Including these guidelines to PEPs acknowledges them as an important part of Python culture, something that every Python programmer should be aware of.

Finding The Zen of Python in Python

To display The Zen of Python, run the following command in Python interpreter:

>>> import this
What Is The Zen of Python?

PEP 20 – The Zen of Python

Check out our interactive Learn Programming with Python track to see how these principles are applied with real code. But first, let’s briefly examine each principle.

Principles of the Zen of Python or PEP-20

Even though the principles of the Zen of Python were prepared by a software engineer, they are written in anything but technical language. So we might actually need to translate these beautifully written principles into actionable insights.

1. Beautiful is better than ugly.

Beautiful code is better than ugly code. If two code snippets are both working but one is simple and easily readable while the other is messy and hard to understand, the first one is definitely the winner. Python is known for its simplicity, readability, and elegance. If you want your code to look pythonic, pay attention to its consistency and simplicity.

2. Explicit is better than implicit.

Your code should be understandable to someone who knows nothing about your program. No prior knowledge should be required. Make your code as explicit as possible without hiding code functionality behind obscure language.

3. Simple is better than complex.

If you have a simple problem that can be solved with a simple solution, go for it. If you have a complex problem, break it down into several simple problems that can be solved with a simple solution. Don’t overcomplicate your code to look smart. This is not appreciated in Python.

4. Complex is better than complicated.

When your problem requires a complex solution, it still shouldn't be too complicated. Complicated code makes your fellow programmers confused and sucks up tons of time and effort. Keep your code as simple and readable as possible, even when working with complex problems.

5. Flat is better than nested.

Are you a big fan of organizing things in categories, sub-categories, and sub-subcategories? Well, when it comes to organizing your code, this hierarchical structure often adds more confusion than organization. Stick to a flat code structure with minimal nesting. This is super important for code readability – and as you already know, we care a lot about readability in Python.

6. Sparse is better than dense.

Don’t try to impress anybody by compressing a ton of functionality into one line of code. It might sound impressive to non-programmers but in fact, it just confuses your fellow programmers who’ll need extra time to understand your code. It’s usually preferable to have several easy-to-follow lines of code than a dense one-liner.

7. Readability counts.

Often, programmers try to save time by abbreviating variable and function names or skipping comments (or making comments overly brief). Remember: You may write code only once, but people will likely need to read it multiple times. If you actually want to “save time”, make your code readable by using easy-to-understand variable and function names, extensive documentation, and proper indenting.

8. Special cases aren't special enough to break the rules.

In Python, there are lots of best practices that make your code more readable for fellow programmers. Follow these practices instead of doing it “your way”. This rule is especially important when you create modules or libraries for others to use.

9. Although practicality beats purity.

Still, each rule may have an exception. If it’s more practical to solve a problem “your way” and it keeps code readable and easy-to-follow, you may sometimes deviate from established best practices. It can be challenging for programming newbies to navigate between this and the above principle, but it gets easier with experience.

10. Errors should never pass silently.

If there’s an error and your program returns None or just an error code, you are having a silent error. That’s not good. Silencing errors eventually leads to bugs that are harder to squash (as it’s harder to track the original cause). It’s better for a program to crash than to silence an error and continue running.

11. Unless explicitly silenced.

In some cases, you may want to ignore the errors that your program might cause. Then the best practice is to silence this error explicitly in your code.

12. In the face of ambiguity, refuse the temptation to guess.

If your code is not working, don’t just blindly try different solutions until one of them seems to work. You might be just masking the problem rather than solving it. Instead, apply critical thinking to understand the problem and then find an appropriate solution.

13. There should be one-- and preferably only one --obvious way to do it.

There is flexibility when you have multiple solutions for the same problem. However, this also adds complexity and effort, as you need to be familiar with all possible solutions.

Python is all about simplicity, and a good example of this is hidden inside this particular principle. Note the postfix operator (the double dash) after “one” and the prefix operator before “obvious” in this principle. New programmers are often confused about when to use postfix or prefix operators. Python solves this problem by not supporting either.

14. Although that way may not be obvious at first unless you're Dutch.

This principle refers to Python creator Guido van Rossum, who is Dutch. Obviously, recalling and understanding any rule in Python would be easier for him than for anyone else.

15. Now is better than never.

This principle has at least two different interpretations. Some people think that it refers to infinite and never-ending loops that you should obviously avoid in your code. Another possible interpretation is that you just need to avoid procrastination with your programming projects. I think it won’t hurt if we accept both of these interpretations.

16. Although never is often better than *right* now.

This principle elaborates on the ideas of the previous one. If we apply it directly to our code, we can say that it’s better to wait for a program to end than to terminate it early and get incorrect results. If we are talking about programming projects in general, we can interpret this principle as a call to put some thought and planning into the project instead of jumping into it right away.

17. If the implementation is hard to explain, it's a bad idea.

If it’s challenging for you to explain your implementation to your co-workers, it’s probably written in bad code. Most likely, you made it overcomplicated and violated a few of the above principles. Try to simplify your solution until you feel comfortable explaining it to other programmers.

18. If the implementation is easy to explain, it may be a good idea.

If you can easily explain your implementation to your co-workers, it might be good. It can still be wrong, but you are on the right track in terms of code readability and simplicity.

19. Namespaces are one honking great idea – let's do more of those!

In Python, you can have isolated namespaces or a collection of names that allow each and every object in your program to have a unique name. Namespaces create a system where names in one of your modules are not conflicting with names in another. This makes them very handy.

That’s it! We’ve covered all 19 principles of the Zen of Python. Now, it’s time for practice.

Start Practicing Python’s Zen

The simplicity, readability, and conciseness of Python code make it possible to learn this programming language very quickly . However, as with any new programming language, lots of practicing with real code is vital. Here on LearnPython.com, we have lots of interactive courses that allow you to dive right into coding without having to invent your own problems and exercises.

I recommend starting with Python Basics. Practice. It’s aimed at beginners who know the basics of Python but lack coding experience. The 15 interactive tasks will have you creating simple Python applications, using variables in Python, applying basic mathematical operators, using loops and if statements, and working with basic Python data structures.

For additional practice and fun, check out Python Practice: Word Games. The course includes 27 tasks organized into three fun thematic sections: (1) implementing a simple cipher in Python; (2) analyzing the text of one of the Sherlock Holmes novels, and (3) implementing functions helpful for playing Scrabble.

If you are looking for a comprehensive learning track that will introduce you to the fundamentals of programming in Python, Learn Programming with Python is definitely worth your attention. It includes 5 interactive courses covering the basics of Python syntax, Python data stru?tures, and built-in algorithms in Python. With its carefully designed learning path and gradually increasing complexity, this track will allow you to learn Python in less than a month.

Bonus. Here is our list of top free Python courses.

Thanks for reading, and happy learning!