4. Python Pitfalls Flashcards

1
Q

What happens if you use a mutable default argument in a function?

A

It retains changes between calls. Use None and assign inside the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the difference between “==” and “is”?

A

==’ checks value equality, ‘is’ checks object identity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between “/” and “//” in Python?

A

/’ returns float division, ‘//’ returns integer (floor) division.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why can assigning one list to another cause bugs?

A

Both variables point to the same list. Use .copy() or list().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does “None == False” return?

A

False. None is not equal to False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the issue with “x = x + 1” inside a function without declaring “x”?

A

If x is a global variable, you’ll get an UnboundLocalError unless you declare ‘global x’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if you modify a list while iterating over it?

A

Can cause skipped elements or unexpected behavior. Use a copy or iterate backward.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the risk of using “except:” without specifying the error?

A

It catches all exceptions, including ones you may not want to hide.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does “a = [] * 3” create?

A

A list with three references to the same list. Modifying one modifies all.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does “False == 0 == []” return?

A

True == False and False == [], so chained comparison can mislead; evaluate each part.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can “is” give unexpected results with small integers or strings?

A

Python caches some small objects, so ‘is’ might return True even for different variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does “not a == b” mean in Python?

A

It means ‘not (a == b)’, not ‘(not a) == b’—operator precedence matters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly