Introduction
If you're a junior developer writing Python, chances are you've made at least one of these mistakes ā and you didn't even know it. These aren't syntax errors that your IDE catches. They're subtle bugs and bad habits that slip through code reviews and slow down your growth.
Let's fix all five.
Mistake #1: Mutable Default Arguments
The problem:
def add_item(item, lst=[]):
lst.append(item)
return lst
This looks harmless, but Python creates the default list once at function definition ā not on every call. So every call shares the same list.
The fix:
def add_item(item, lst=None):
if lst is None:
lst = []
lst.append(item)
return lst
Always use None as your default for mutable arguments, then create the object inside the function.
Mistake #2: Skipping List Comprehensions
The problem:
squares = []
for i in range(10):
squares.append(i ** 2)
The fix:
squares = [i ** 2 for i in range(10)]
Cleaner, faster, and more Pythonic. Senior devs will notice immediately.
Mistake #3: Bare Exception Catching
The problem:
try:
result = 10 / 0
except:
pass
A bare except catches everything ā including KeyboardInterrupt and SystemExit. This makes bugs invisible and debugging a nightmare.
The fix:
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
Always catch specific exceptions.
Mistake #4: String Concatenation in Loops
The problem:
result = ""
for word in words:
result += word
Each += creates a brand new string object in memory. In a loop, that's O(n²).
The fix:
result = "".join(words)
.join() is O(n) and the idiomatic Python way to build strings from iterables.
Mistake #5: Not Using enumerate()
The problem:
for i in range(len(fruits)):
print(i, fruits[I])
The fix:
for i, fruit in enumerate(fruits):
print(i, fruit)
enumerate() exists exactly for this. Use it ā it's cleaner and avoids off-by-one errors.
Which one were you guilty of? Drop a comment below š
Follow me on Instagram https://instagram.com/techqueen.codes for visual SQL, Python & Snowflake tips every week.
Enjoyed this? Save the visual cheat sheet on Instagram ā https://instagram.com/techqueen.codes
Top comments (0)