🐍 9 Hidden Python Features You Didn’t Know Existed
Even if you’ve been coding Python for years… These hidden gems will surprise you! 👇
1️⃣ else with Loops
Did you know a for or while loop can have an else?
It only runs if the loop finishes normally (no break) 👇
for i in range(3):
    print(i)
else:
    print(”Loop completed without break”)
✅ Output:
0  
1  
2  
Loop completed without break
💡 Great for search loops or validations.
2️⃣ _ for Last Result in REPL
Python automatically stores the last output in _ when you’re using the REPL.
5 + 7
_ + 3
✅ Output: 15
Perfect for quick math or testing without extra variables.
3️⃣ Multiple Assignment
Assign several variables in one line — elegant & Pythonic!
a, b, c = 1, 2, 3
print(a, b, c)
✅ Output: 1 2 3
💡 Makes your code cleaner and more readable.
4️⃣ zip() for Parallel Iteration
Loop over multiple lists at the same time!
names = [”Alice”, “Bob”]
ages = [25, 30]
for n, a in zip(names, ages):
    print(f”{n} is {a} years old”)
✅ Output:
Alice is 25 years old  
Bob is 30 years old
💡 Ideal for combining related data.
5️⃣ enumerate() to Track Index
No need to manage counters manually!
for i, val in enumerate([”a”, “b”, “c”], start=1):
    print(i, val)
✅ Output:
1 a  
2 b  
3 c
💡 Clean, readable, and beginner-friendly.
6️⃣ Underscore in Numeric Literals
Make large numbers human-friendly!
million = 1_000_000
print(million + 5000)
✅ Output: 1005000
💡 Same as 1000000, just easier to read.
7️⃣ Swap Variables Without a Temp Variable
No need for a third variable!
x, y = 5, 10
x, y = y, x
print(x, y)
✅ Output: 10 5
💡 One-liner elegance — pure Pythonic style.
8️⃣ else with Try/Except
The else block executes only if there’s no exception.
try:
    print(”No errors here”)
except:
    print(”Error occurred”)
else:
    print(”Else runs if no exception”)
✅ Output:
No errors here  
Else runs if no exception
💡 Great for separating error-free logic.
9️⃣ if __name__ == “__main__”:
Tells Python whether to run code or just import it!
def main():
    print(”Script running directly”)
if __name__ == “__main__”:
    main()
✅ Output:
Script running directly
💡 Helps make scripts both reusable and runnable.
🚀 These 9 hidden features make your Python code:
✅ Cleaner
✅ Faster
✅ More professional
Which one blew your mind? 💭
Drop your favorite below 👇





