Writing Pythonic Code: Principles, Examples, and the Zen of Python
This article explains the concept of writing Pythonic code—leveraging Python’s language features for clear, concise, and maintainable programs—by contrasting non‑Pythonic examples with idiomatic solutions, illustrating the Zen of Python, and providing practical tips and references for developers.
Python is widely regarded as one of the easiest dynamic languages to learn, thanks to its cross‑platform nature, readability, extensive packages, and popularity among DevOps, testing, and development engineers.
Many developers use Python to complete tasks, but often stop at functional implementation without striving for more concise, elegant Pythonic code.
When I first started with Python I did not know the term Pythonic until a senior programmer mentioned that some project code was not "Pythonic" and needed refactoring. He meant that the code did not follow Python’s idiomatic style.
What is Pythonic
Pythonic code fully exploits the language’s features to produce clear, concise, and maintainable programs. Being Pythonic means the code is not only syntactically correct but also follows community conventions and uses the language in the way it was intended.
Examples
Below is a snippet written by a C/C++ programmer:
int a = 1;
int b = 100;
int total_sum = 0;
while (b >= a) {
total_sum += a;
a++;
}If you translate this directly into Python without adopting Pythonic patterns, you might write:
a = 1
b = 100
total_sum = 0
while b >= a:
total_sum += a
a += 1A Pythonic version would be much cleaner:
total_sum = sum(range(1, 101))Another common example: a Java for‑loop might look like this:
for(int index=0; index < items.length; index++) {
items[index].performAction();
}In Python the same logic is more readable using a for‑each loop:
for item in items:
item.perform_action()Or even as a generator expression:
(item.some_attribute for item in items)Thus, when someone says a piece of code is not Pythonic , they mean it can be rewritten in a way that better matches Python’s idiomatic style, often by using built‑in functions instead of reinventing the wheel.
Official Introduction to Pythonic
The Python interpreter already hides a short introduction to Pythonic style. Open a Python console and type import this to see the "Zen of Python" by Tim Peters:
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!In Chinese this translates roughly to Tim Peters' "Zen of Python".
Do you now understand what Pythonic means?
By the way, among all Python books I have read, "Python Cookbook" is my favorite; I have placed it on Baidu Cloud and can provide a download link upon request.
Reference
[1]
Built-in Functions: https://docs.python.org/3/library/functions.html
DevOps Engineer
DevOps engineer, Pythonista and FOSS contributor. Created cpp-linter, commit-check, etc.; contributed to PyPA.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.