4. Python Pitfalls Flashcards
What happens if you use a mutable default argument in a function?
It retains changes between calls. Use None and assign inside the function.
What’s the difference between “==” and “is”?
==’ checks value equality, ‘is’ checks object identity.
What is the difference between “/” and “//” in Python?
/’ returns float division, ‘//’ returns integer (floor) division.
Why can assigning one list to another cause bugs?
Both variables point to the same list. Use .copy() or list().
What does “None == False” return?
False. None is not equal to False.
What’s the issue with “x = x + 1” inside a function without declaring “x”?
If x is a global variable, you’ll get an UnboundLocalError unless you declare ‘global x’.
What happens if you modify a list while iterating over it?
Can cause skipped elements or unexpected behavior. Use a copy or iterate backward.
What is the risk of using “except:” without specifying the error?
It catches all exceptions, including ones you may not want to hide.
What does “a = [] * 3” create?
A list with three references to the same list. Modifying one modifies all.
What does “False == 0 == []” return?
True == False and False == [], so chained comparison can mislead; evaluate each part.
How can “is” give unexpected results with small integers or strings?
Python caches some small objects, so ‘is’ might return True even for different variables.
What does “not a == b” mean in Python?
It means ‘not (a == b)’, not ‘(not a) == b’—operator precedence matters.