DEV Community

Cover image for 10 Python Tricks You Probably Didn’t Know 💡🐍
Nishkarsh Pandey
Nishkarsh Pandey

Posted on

10 Python Tricks You Probably Didn’t Know 💡🐍

Python is beautiful. But even if you’ve been using it for years, there are some hidden gems that can make your code cleaner, faster, or just more fun.

Here are 10 Python tricks that might just blow your mind 🤯 — with examples you can copy straight into your next project.

  1. 🥚 Walrus Operator :=
if (n := len("Hello")) > 3:
    print(f"String is long ({n} characters)")

Enter fullscreen mode Exit fullscreen mode

✅ Great for loops and conditionals.
💡 Python 3.8+.

2.🧼 Clean List Comprehension with Conditionals
You can filter and transform in one line.

squares = [x*x for x in range(10) if x % 2 == 0]

Enter fullscreen mode Exit fullscreen mode

➡️ Only keeps even numbers, squares them.

3.🧙‍♂️ Multiple Assignment (Unpacking)
Python makes this elegant:

a, b, c = [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Or even:

first, *middle, last = [10, 20, 30, 40, 50]
Enter fullscreen mode Exit fullscreen mode

Use * to grab “the rest.”

4.🔄 Swap Variables in One Line
Forget temp variables.

a, b = b, a
Enter fullscreen mode Exit fullscreen mode

Clean. Pythonic. Instant swap.

5.🧵 Join Strings Like a Pro
No more manual looping:

words = ["Hello", "World", "2025"]
print(" - ".join(words))  # Hello - World - 2025
Enter fullscreen mode Exit fullscreen mode

Efficient and readable.

6.📚 Use enumerate() Instead of Range + Len
Clean up indexed loops:

names = ["Alice", "Bob", "Charlie"]
for idx, name in enumerate(names):
    print(f"{idx}: {name}")

Enter fullscreen mode Exit fullscreen mode

No need for range(len(...))

7.🪄 One-Line If/Else (Ternary)
Use inline for simple conditions:

age = 20
status = "adult" if age >= 18 else "minor"
Enter fullscreen mode Exit fullscreen mode

Perfect for concise assignments.

8.🔁 Loop with an else Block
The else runs if the loop was not broken.

for n in range(5):
    if n == 7:
        break
else:
    print("7 was not found!")
Enter fullscreen mode Exit fullscreen mode

Super underrated.

9.🕳️ Use get() with Dicts to Avoid Key Errors
No more KeyError surprises:

data = {"name": "Nora"}
print(data.get("age", "Not provided"))

Enter fullscreen mode Exit fullscreen mode

Clean fallback values.

10.🕵️‍♂️ Dictionary Comprehensions
Just like list comprehensions:

squares = {x: x*x for x in range(5)}

Enter fullscreen mode Exit fullscreen mode

Fast and readable dictionary creation.

🎁 Bonus: Zen of Python
Run this in any Python terminal:

import this
Enter fullscreen mode Exit fullscreen mode

You'll get 19 lines of poetic Python wisdom.

💬 Final Thoughts
Python is full of magic 🪄 — the more you explore, the more elegant it gets.

Which trick was your favorite?
Got one that I missed? Drop it in the comments 👇
Let’s keep the Pythonic goodness flowing!

Top comments (1)

Collapse
 
dreama profile image
Dream

Love these Python tricks, especially the walrus operator and unpacking lists. Super handy!