Interview Questions: Variables & Data Types
1 Why is Python called a "dynamically typed" language?
Because a variable's type is determined at runtime from the value assigned to it, not declared upfront by the programmer. The same variable name can even hold different types at different points in the program — x = 10 then later x = "ten" is legal.
Confusing "dynamically typed" with "not typed at all" — Python values absolutely have types, they're just not declared explicitly.
Follow-up"What's one downside of dynamic typing compared to a statically typed language like Java?" (Answer: type errors surface at runtime instead of being caught before the program even runs.)
2 What's the difference between type() and id()?
type() tells you what kind of value something is (str, int, etc.). id() tells you where in memory it lives. Two variables can have the same type but different ids (different objects), or the same id (literally the same object in memory).
Thinking they're interchangeable or that id() is only useful for debugging — it's also the mechanism behind the is operator.
"What does the is operator actually check?" (Answer: whether two variables share the same id() — the same object — not just equal values.)
3 Why might id(x) and id(y) be equal after x = 10 and y = 10?
Because small integers are immutable, Python reuses the same underlying object for the same small value instead of creating a new one every time. Both variables end up pointing at that one shared object.
Red flagsAssuming this always happens for every value/type (it's an implementation detail specific to small integers and a few other cases, not a general guarantee).
Follow-up"If you then run x = 20, does y change too?" (Answer: no — a brand new object is created for 20, and only x is repointed to it.)
4 What happens if you run int("hello")?
It raises a ValueError, because "hello" can't be meaningfully interpreted as a number. Type conversion only succeeds when the value makes sense in the target type.
Guessing it silently returns 0 or None — Python raises an explicit error instead of failing silently.
"How would you safely attempt this conversion without crashing the program?" (Answer: wrap it in a try/except — covered in Chapter 12.)
5 What does bool("False") return, and why does this surprise people?
It returns True. bool() on a string only checks whether the string is empty or not — it never reads the actual text. Since "False" is a non-empty string, it converts to True.
Guessing False — a very common, very reasonable-sounding wrong answer that reveals the candidate hasn't hit this edge case yet.
"What's the only string that converts to False?" (Answer: the empty string "".)
6 Name the five basic data types covered so far and give an example of each.
str ("hello"), int (12), float (4.9), bool (True), and complex (3+4j).
Forgetting complex entirely — understandable since it's rare in beginner code, but worth knowing it exists.
"Which of these five are immutable?" (Answer: all of them — every one of Python's fundamental built-in types is immutable.)
Rapid-Fire Review
Quick recall check — fill in each blank and hit Check.