Fundamentals 7 min read

10 Common Pythonic Coding Practices and Tips

This article presents ten common Pythonic coding techniques—including variable swapping, efficient loops, enumerate, string joining, context-managed file handling, list comprehensions, decorators, appropriate list usage, sequence unpacking, and dictionary iteration—explaining why they are more readable, memory‑efficient, and idiomatic for Python developers.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
10 Common Pythonic Coding Practices and Tips

Python is praised for its concise syntax, and writing code that reads like pseudocode improves readability and performance; as Harold Abelson said, "Programs must be written for people to read, and only incidentally for machines to execute."

1. Variable swapping can be done without a temporary variable:

>> a = 1
>>> b = 2
>>> a, b = b, a

2. Looping over a range is more Pythonic using range (or xrange in Python 2):

for i in range(6):
    print i2

3. Iterating with index is cleaner with enumerate :

for i, color in enumerate(colors):
    print i, '-->', color

4. String concatenation should prefer str.join to avoid repeated temporary strings:

print ', '.join(names)

5. File handling is safest with a with statement, which automatically closes the file:

with open('data.txt') as f:
    data = f.read()

6. List comprehensions provide a concise one‑liner for building lists:

[i*2 for i in xrange(10)]

7. Decorators can abstract caching logic from business code:

def cache(func):
    saved = {}
    def wrapper(url):
        if url in saved:
            return saved[url]
        page = func(url)
        saved[url] = page
        return page
    return wrapper

@cache
def web_lookup(url):
    return urllib.urlopen(url).read()

8. Choosing the right list structure improves performance; for frequent insertions/removals at both ends, collections.deque is preferable:

from collections import deque
names = deque(['raymond', 'rachel', ...])
names.popleft()
names.appendleft('mark')

9. Sequence unpacking assigns multiple variables in a single statement:

name, gender, age, email = p

10. Dictionary iteration is most efficient using items() (or iteritems() in Python 2) to avoid extra lookups:

for k, v in d.items():
    print k, '-->', v

These Pythonic patterns make code cleaner, faster, and easier to maintain; many more exist, and exploring open‑source projects can provide further inspiration.

pythonProgrammingbest practicescoding stylepythonic
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.