← Back to list

7 Python Tricks That Instantly Made My Code Cleaner

When I first started learning Python, my focus was simple: write code that works. Over time, I realized that working code isn’t always good…

JANGAM SAUMYA · 2026-07-31 06:25 · 7 claps · 1.5 min read
#python #programming #software-development #coding #python-tips
Open on Medium ↗
Wiki topics: EDU · Education & Learning 💻 · Programming ⏱️ · Productivity

7 Python Tricks That Instantly Made My Code Cleaner

When I first started learning Python, my focus was simple: write code that works. Over time, I realized that working code isn’t always good code. Clean, readable code is easier to debug, maintain, and share with others.

Here are seven Python tricks that genuinely improved the way I write code.

1. Use List Comprehensions

Instead of writing:

numbers = []
for i in range(10):
    numbers.append(i * 2)

Write:

numbers = [i * 2 for i in range(10)]

It’s shorter, cleaner, and easier to read once you’re familiar with the syntax.

2. Swap Variables Without a Temporary Variable

Instead of:

temp = a
a = b
b = temp

Simply write:

a, b = b, a

Python makes swapping values effortless.

3. Use enumerate() Instead of Manual Counters

Rather than keeping track of an index yourself:

index = 0
for fruit in fruits:
    print(index, fruit)
    index += 1

Use:

for index, fruit in enumerate(fruits):
    print(index, fruit)

It’s cleaner and less error-prone.

4. Use zip() to Iterate Over Multiple Lists

If two lists are related, iterate over them together.

names = ["Alice", "Bob"]
scores = [90, 95]
for name, score in zip(names, scores):
    print(name, score)

This avoids unnecessary indexing.

5. Use f-Strings

Instead of:

print("Hello " + name)

Write:

print(f"Hello {name}")

f-Strings are easier to read and support expressions directly.

6. Check Membership with in

Instead of writing multiple comparisons:

if color == "red" or color == "blue":

Write:

if color in ("red", "blue"):

This is both cleaner and easier to extend.

7. Use pathlib for File Paths

Instead of manually joining paths with strings, use pathlib.

from pathlib import Path
file = Path("data") / "students.csv"

It works across operating systems and makes file handling much simpler.

Final Thoughts

Clean code isn’t about writing fewer lines — it’s about writing code that’s easy to understand six months later, whether by you or someone else. Small improvements like these can make a noticeable difference in your coding style over time.

Which Python trick has improved your code the most? I’d love to hear your favorite in the comments.


메타데이터
post_id
072c7bef7ae9
slug
7-python-tricks-that-instantly-made-my-code-cleaner-072c7bef7ae9
url
https://medium.com/@24211a05m5/7-python-tricks-that-instantly-made-my-code-cleaner-072c7bef7ae9
canonical_url
https://medium.com/@24211a05m5/7-python-tricks-that-instantly-made-my-code-cleaner-072c7bef7ae9
author_url
https://medium.com/@24211a05m5
status
ok
fetched_at
2026-08-06 20:16:35