Common Python Bugs That Don’t Throw Errors
Logical mistakes that execute successfully but produce incorrect results
Common Python Bugs That Don’t Throw Errors
Logical mistakes that execute successfully but produce incorrect results
Photo by Emile Perron on Unsplash
Some Python bugs do not crash your program.
There is no error message. No traceback. The code runs successfully. Yet the output is wrong.
This usually happens because Python allows certain behaviors that look correct but work differently than expected.
Let’s understand these bugs one by one.
1. Mutable default arguments
Look at the following function; at first glance, it looks completely correct. It takes an item and adds it to a list. If no list is provided, it simply uses an empty list as the default.
def add_item(item, items=[]):
items.append(item)
return items
Most beginners expect this function to create a fresh list every time it’s called. Surprisingly, that’s not what Python does.
Now look at the output:
add_item(1) # Output: [1]
add_item(2) # Output: [1, 2]
Why is this happening?
- The default list
items=[]is evaluated only once, when the function is defined. - Python reuses the same list for every function call
- So values keep getting added to the same list
This is usually not what we want.
Correct way:
The recommended approach is to use None as the default value and create a new list inside the function whenever it's needed.
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
Now the output becomes:
add_item(1) # Output: [1]
add_item(2) # Output: [2]
Each function call now gets its own fresh list, making the function behave exactly as you expect.
2. Using is instead of ==
At first, is and == may seem to do the same thing because both are used for comparisons. However, they check two completely different things, and confusing them can lead to unexpected results.
Consider this example:
a = [1, 2]
b = [1, 2]
Now compare them:
a == b # True
a is b # False
Here’s why:
==checks valuesischecks memory location
Even though a and b look the same, they are stored in different memory locations.
Common mistake:
if value is 10:
...
This may appear to work sometimes, but it isn’t the correct way to compare values and can behave differently depending on Python’s internal optimizations.
Correct rule:
- Use
==for value comparison - Use
isonly when checking object identity, such asNone,True, orFalse.
Remember this simple rule: == asks “Do these have the same value?”, while is asks “Are these the exact same object?”
3. Modifying a list while looping
Modifying a list while you are iterating over it is a common mistake, especially for beginners. Although the code looks correct, changing the list during iteration can lead to unexpected results.
Consider the following example:
nums = [1, 2, 3, 4]
for n in nums:
if n % 2 == 0:
nums.remove(n)
Expected output:
[1, 3]
Actual output:
[1, 3, 4]
Why does this happen?
- When an element is removed, the remaining elements shift to fill its place.
- However, the loop continues to the next index, causing some elements to be skipped without any warning.
Python doesn’t throw an error here, which makes this bug even harder to notice.
Better approach:
nums = [n for n in nums if n % 2 != 0]
A better approach is to create a new filtered list instead of modifying the existing one while iterating.
4. Truthy and falsy value confusion
Python treats certain values like 0, "", [], {}, and None as False in conditional statements. This is convenient, but it can sometimes hide bugs when you are actually checking for the existence of data.
Example:
data = {"count": 0}
if data.get("count"):
print("Count exists")
This prints nothing.
At first glance, it may seem like the key doesn’t exist. In reality, the key does exist. Its value is simply 0, and Python treats 0 as False inside an if statement.
Correct check:
If your intention is to check whether the key exists, it’s better to check the key directly instead of relying on its value.
if "count" in data:
print("Count exists")
This checks for the presence of the key, regardless of whether its value is 0, False, or an empty string.
5. Integer division mistake
Python provides two different operators for division, and using the wrong one can easily produce unexpected results. This is especially common when you are calculating averages or percentages.
Consider this example:
avg = 5 // 2
print(avg)
Output:
2
Many beginners expect the result to be 2.5, but Python returns 2.
Reason:
//always performs floor division- It removes the decimal part
Correct version:
avg = 5 / 2
This returns 2.5.
6. Overwriting built-in names
Python comes with many built-in functions like sum(), list(), max(), and str(). Using these names for your own variables can accidentally replace the original functions within your program.
Consider this example:
list = [1, 2, 3]
sum = 10
Later, you write:
sum(list)
This causes problems.
Why?
- You replaced Python’s built-in
sumfunction - Python now thinks
sumis an integer
What makes this mistake frustrating is that the error usually appears much later in the code, making it difficult to trace back to where the problem actually started.
Best practice:
A good habit is to avoid using names that already exist as Python’s built-in functions.
Some common names to avoid are:
listdictsummaxstr
7. Ignoring errors silently
Exception handling is useful because it prevents your program from crashing unexpectedly. However, completely ignoring an error can be even more dangerous than letting the program stop.
Example:
try:
risky_operation()
except:
pass
If an error occurs:
- Python hides the exception.
- The program continues running.
- You never know that something went wrong.
Although the program doesn’t crash, it may continue with incorrect data or incomplete operations, making the actual problem much harder to identify later.
Better way:
A better approach is to catch the exception and at least log the error.
try:
risky_operation()
except Exception as e:
print(e)
This gives you valuable information about what failed, making debugging much easier.
These bugs are dangerous because:
- The program runs successfully.
- No error message is shown.
- The output is incorrect.
Unlike syntax errors, these mistakes don’t stop your program; they quietly produce unexpected behavior, making them much harder to detect.
As you write more Python code, understanding these subtle pitfalls will help you debug faster, write more reliable programs, and avoid hours of unnecessary frustration.
If you found this post helpful, consider buying me a Coffee, it really helps me stay motivated and keep sharing more.
메타데이터
- post_id
- 7ef7c5cbbdc8
- slug
- common-python-bugs-that-dont-throw-errors-7ef7c5cbbdc8
- url
- https://medium.com/top-python-libraries/common-python-bugs-that-dont-throw-errors-7ef7c5cbbdc8
- canonical_url
- https://medium.com/top-python-libraries/common-python-bugs-that-dont-throw-errors-7ef7c5cbbdc8
- author_url
- https://medium.com/@shwetag04
- status
- ok
- fetched_at
- 2026-07-09 15:12:33