TGViewer
Python Learning Python Learning @python_bds · 5.74K subscribers
Post #1246 591
🔥 12 Python Tricks That Make Your Code Cleaner

Here are some Python tricks every developer should know.

1. Swap variables without a temporary variable.
a, b = b, a

2. Reverse a list.
nums[::-1]

3. Chain comparisons.
10 < age < 30

4. Multiple assignment.
x = y = z = 0

5. Unpack lists.
first, *middle, last = nums

6. Use underscores for ignored values.
name, _, age = data

7. Format strings with f-strings.
print(f"Hello {name}")

8. Merge dictionaries.
new = dict1 | dict2

9. Remove duplicates.
unique = list(set(nums))

10. Check membership using sets.
if color in {"red", "green", "blue"}:

11. Readable large numbers.
salary = 1_000_000

12. Use with for files.
with open("data.txt") as f:
data = f.read()
  • ❤ 2
More from @python_bds
  1. Oct 8, 2026🐛 This Python Bug Looks Random, But It Isn't Look at this: functions = [] for i in range(…
  2. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  3. Oct 4, 2026Python Set Methods ✍️
  4. Oct 3, 2026Post #1310
  5. Oct 1, 2026🐍 Python Beginner Notes
  6. Sep 30, 2026🧠 return vs print() in Python These are not interchangeable. def add(a, b): print(a + b)…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →