3 Tips For Writing Pythonic Code

David Amos
15 min readMar 17, 2022

A common question I’ve seen from beginning Python programmers is, “How do I make my code more Pythonic?” The problem with a word like “Pythonic” is that its meaning is nebulous: it means different things to different people. The meaning isn’t static, either. Whether or not code is Pythonic can depend on which version of Python you’re using, and best practices for writing Pythonic code may change over time.

In this article, I’ll share my perspective on what makes code Pythonic by looking at a few concrete examples. I’ll also leave you with some hand-picked resources that will help you build a mental model for deciding when code is Pythonic or not.

But first, let’s agree on at least some kind of definition for the word Pythonic.

What does “Pythonic” mean?

The Python language is over 30 years old. In that time, Python programmers have collectively gained an enormous amount of experience using the language for a wide range of purposes. Over time, that collective experience has been shared and distilled into best practices — commonly referred to as the Pythonic way.

The Zen of Python, written by Tim Peters and accessible from any Python installation by typing import this into the REPL, traditionally exemplifies the Pythonic mindset:

--

--

David Amos
David Amos

Written by David Amos

Constantly curious coder | Writes about coding and technical writing