Programming Search⌘K

Featurecore

Pythonic code

Also called Pythonic, idiomatic Python, Python idioms, The Zen of Python, import this, PEP 20.

Pythonic code uses the common idioms of Python: loop over the items, unpack tuples, use with for resources, build lists with comprehensions, test truth directly and use the standard library first.

Language
Python
Proposals
pep-20, pep-8

Synopsis

food = ["rice", "pho", "banh mi"]
prices = {"rice": 2, "pho": 5, "banh mi": 3}

for number, piece in enumerate(food, start=1):
    print(number, piece, prices[piece])

cheap = [piece for piece in food if prices[piece] < 4]
print(cheap, "pho" in prices, ", ".join(food))
Output
1 rice 2
2 pho 5
3 banh mi 3
['rice', 'banh mi'] True rice, pho, banh mi

Semantics

r1. Pythonic means "An idea or piece of code which closely follows the most common idioms of the Python language, rather than implementing code using concepts common to other languages."1 The glossary gives the loop as the example: "a common idiom in Python is to loop over all elements of an iterable using a for statement."1 Example: idioms.

r2. The Zen of Python in PEP 20 states the values behind the idioms in 19 short lines, such as "Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex."2 The statement import this prints them.2 Example: zen.

r3. Loop over the items, not over the indexes. When the index is also needed, use enumerate: "the position index and corresponding value can be retrieved at the same time using the enumerate() function."3 For a dictionary, "the key and corresponding value can be retrieved at the same time using the items() method."3 For two sequences, "the entries can be paired with the zip() function."3

r4. Unpack instead of indexing. An assignment such as a, b = b, a + b gives new values to two names at once, and "the expressions on the right-hand side are all evaluated first before any of the assignments take place."4 Example: unpack.

r5. Build a list with a comprehension. "List comprehensions provide a concise way to create lists."3 A comprehension also does not leave its loop variable behind, as a for loop does.3 Join strings with str.join, because PEP 8 says that "the ''.join() form should be used" in code that must be fast on all implementations.5

r6. Test truth directly. "By default, an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero".6 So PEP 8 says: "For sequences, (strings, lists, tuples), use the fact that empty sequences are false".5 But "Comparisons to singletons like None should always be done with is or is not, never the equality operators."5 Example: truthiness.

r7. Use with for a resource. "It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point."7 Example: with-files.

r8. Use the standard library first. "Python has a “batteries included” philosophy."8 A built-in such as sum, max, any or sorted, or a module such as collections, does the common jobs. Example: stdlib-first.

r9. PEP 8 lists more idioms: use startswith() and endswith() instead of slices, use isinstance() instead of a type comparison, and "Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier".5 To test for a key, use key in d, not key in d.keys().9 Example: lambda-name.

Examples

import contextlib
import io

buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
    import this

lines = buffer.getvalue().splitlines()
print(lines[0])
print(len(lines) - 2, "aphorisms")
print(lines[8])
Output
The Zen of Python, by Tim Peters
19 aphorisms
Readability counts.

Importing the module this prints the text (rule r2). The first two lines are the title and a blank line.

a, b = 0, 1
a, b = b, a + b
print(a, b)

first, *rest = [3, 1, 4, 1, 5]
print(first, rest)

scores = {"ann": 31, "bob": 27}
for name, score in scores.items():
    print(f"{name}: {score}")
Output
1 1
3 [1, 4, 1, 5]
ann: 31
bob: 27

The swap needs no temporary name, the star name takes the rest of the list, and the loop unpacks each pair from items() (rules r3 and r4).

def describe(items=None):
    if items is None:
        return "no argument"
    if not items:
        return "empty"
    return f"{len(items)} items"


print(describe(), describe([]), describe([1, 2]))
print([bool(x) for x in (0, 0.0, "", [], {}, None, "0", [0])])
Output
no argument empty 2 items
[False, False, False, False, False, False, True, True]

is None tests for the missing argument, and not items tests for an empty list (rule r6). The string '0' and the list [0] are true, because they are not empty.

import tempfile
from pathlib import Path

with tempfile.TemporaryDirectory() as folder:
    path = Path(folder) / "notes.txt"
    with open(path, "w", encoding="utf-8") as f:
        f.write("one\ntwo\n")
    with open(path, encoding="utf-8") as f:
        lines = [line.rstrip("\n") for line in f]
    print(lines, f.closed)
Output
['one', 'two'] True

Each with block closes its file at the end, and the outer one removes the folder (rule r7). The file object is an iterator over its lines.

from collections import Counter

words = "the cat and the hat and the bat".split()
print(Counter(words).most_common(2))
print(sum(len(w) for w in words), max(words, key=len))
print(any(w.startswith("h") for w in words))
Output
[('the', 3), ('and', 2)]
24 the
True

Counter counts the words, and the built-ins take a generator expression, so no list is built (rule r8). max returns the first of the longest words.

double = lambda x: 2 * x


def triple(x):
    return 3 * x


print(double.__name__, triple.__name__)
Output
<lambda> triple

The def form gives the function its name, which is "more useful for tracebacks and string representations in general."5

Versions

Since 3.10

has the strict argument. With strict=True, it "raises a ValueError if one iterable is exhausted before the others".10

Pitfalls

This code runs but gives a wrong answer on purpose.

def set_timeout(seconds=None):
    if not seconds:
        seconds = 30
    return seconds


print(set_timeout(), set_timeout(0), set_timeout(5))
Output
30 30 5

The caller asked for 0 seconds and got 30, because 0 is false. PEP 8 warns: "beware of writing if x when you really mean if x is not None".5 Write if seconds is None:.

This code runs but gives a wrong answer on purpose.

class Anything:
    def __eq__(self, other):
        return True


x = Anything()
print(x == None, x is None)
Output
True False

A class can define == in any way, so x == None can be true for an object that is not None. The is test cannot be changed by a class. Ruff checks this with the rule E711.11

This code runs but gives a wrong answer on purpose.

names = ["ann", "bob", "cy"]
ages = [31, 27]
print(list(zip(names, ages)))
try:
    list(zip(names, ages, strict=True))
except ValueError as error:
    print(type(error).__name__)
Output
[('ann', 31), ('bob', 27)]
ValueError

By default, zip "stops when the shortest iterable is exhausted", so cy is lost with no error.10 When the lengths must be equal, pass strict=True.

Comparison

An idiom of one language can be a bad habit in another. The glossary notes that people new to Python "sometimes use a numerical counter instead" of a direct loop, because "Many other languages don’t have this type of construct".1 For the error-handling idiom of Python, see EAFP and LBYL. For the details of each idiom, see Looping Techniques, Iterable unpacking, List comprehensions, Truth value testing and Predefined Clean-up Actions.

References

  1. Glossary, Python 3.15 documentation, Pythonic
  2. PEP 20, The Zen of Python, The Zen of Python; Easter Egg
  3. The Python Tutorial, Python 3.15 documentation, 5.1.3 List Comprehensions; 5.6 Looping Techniques
  4. The Python Tutorial, Python 3.15 documentation, 3.2 First Steps Towards Programming
  5. PEP 8, Style Guide for Python Code, Programming Recommendations
  6. Built-in Types, Python 3.15 documentation, Truth Value Testing
  7. The Python Tutorial, Python 3.15 documentation, 7.2 Reading and Writing Files
  8. The Python Tutorial, Python 3.15 documentation, 10.12 Batteries included
  9. in-dict-keys (SIM118), Ruff documentation, Why is this bad?
  10. Built-in Functions, Python 3.15 documentation, enumerate(); zip()
  11. none-comparison (E711), Ruff documentation, What it does; Why is this bad?

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source