🐍 Python for Everyone

← Back to practice bank

Chapter 3 Interview Prep 8 min read

Interview Questions: Operators

1 What's the difference between / and // in Python?

/ is true division and always returns a float, even when the numbers divide evenly (10 / 2 is 5.0). // is floor division — it divides then rounds down to the nearest whole number, returning an int if both operands are ints.

Saying // just "rounds" — it specifically rounds toward negative infinity, which matters for negative numbers (-7 // 2 is -4, not -3).

"What does -7 // 2 evaluate to?" (Answer: -4, because floor division rounds toward negative infinity, not toward zero.)

2 Why doesn't Python have a ++ operator?

It was a deliberate design choice. In languages that have it, ++ secretly has two forms (pre-increment and post-increment) with different behavior depending on position, which causes subtle bugs. Python requires the explicit, unambiguous x += 1 instead.

Not knowing this at all and trying to write x++ in actual Python code (it's a syntax error).

"What does x += 1 actually expand to?" (Answer: x = x + 1.)

3 What is short-circuit evaluation, and why does it matter?

Python stops evaluating a logical expression as soon as the final result is already determined. In a and b, if a is False, b is never evaluated. This lets you safely write guard conditions like x != 0 and 10 / x > 1, where the second check would crash if it ran when x is 0.

Not realizing that operand order matters because of this — putting the "safe" check first is a deliberate pattern, not an accident.

"Does or short-circuit too, and under what condition?" (Answer: yes — if the first operand is True, the second is never evaluated.)

4 How would you use bitwise operators to store multiple true/false flags in one number?

Assign each flag a power of 2 (1, 2, 4, 8, ...) so each occupies a unique bit. Combine flags with | (e.g., read | write = 4 | 2 = 6). Check if a flag is set with & (e.g., permissions & write is non-zero if write is set). This is exactly how Unix file permissions work.

Trying to use decimal values like 1, 2, 3 for flags instead of powers of 2 — that breaks the ability to combine and check them independently.

"How would you turn a specific flag off, keeping the others intact?" (Answer: use & with the bitwise complement, e.g., permissions & ~write.)

5 What's the difference between == and is?

== compares values — are these two things "equal"? is compares identity — are these the literal same object in memory? Two separate lists with the same contents are == but not is.

Using is to compare strings or numbers for equality in real code — it can work by coincidence for small cached values but isn't reliable or the correct tool for the job.

"When is is the correct operator to use?" (Answer: mainly for comparing against None, e.g., if x is None:.)

6 What does in do, and what types can you use it with?

in checks membership — whether a value exists inside a collection like a string, list, tuple, set, or dictionary keys. "a" in "cat" is True (substring check); 3 in [1, 2, 3] is True (element check).

Thinking it only works on lists — it's a general operator across every built-in collection type, including strings.

"What's the opposite check called?" (Answer: not in.)

Rapid-Fire Review

Quick recall check — fill in each blank and hit Check.

🎉 Perfect score — you're ready to talk about this chapter in an interview.
← Practice Bank Build the Project →