Python Fundamentals Interview Questions (Python)
10 Python Fundamentals questions from the Python track. Read each short answer here, then open a question for the full explanation, examples, common mistakes, and sources.
- Junior
What happens to Python code from the moment you run it?
CPython reads the source file and compiles it to bytecode (usually cached in __pycache__ as .pyc files), then the interpreter executes that bytecode instruction by instruction inside the virtual machine.
Read the full explanation - Junior
What is the difference between mutable and immutable objects in Python?
A mutable object can be changed in place (such as list, dict and set), while an immutable object cannot change after creation (such as int, str, tuple and frozenset); any "modification" of it creates a new object.
Read the full explanation - Junior
When should you choose a list, tuple, set or dict?
Use a list for an ordered, changeable sequence, a tuple for a fixed record, a set for fast membership checks and de-duplication, and a dict to map keys to values with fast lookup.
Read the full explanation - Junior
What are list and dict comprehensions for, and when do they hurt readability?
They are a compact syntax for building a new collection from an iterable with a simple transformation or filter, and they hurt readability when loops nest, conditions pile up, or they contain side effects.
Read the full explanation - Junior
What is the difference between is and == in Python?
== compares values through __eq__, while is checks whether two names refer to the same object in memory (the same identity, id).
Read the full explanation - Junior
How does Python decide whether a value is truthy or falsy?
An object is falsy if it defines __bool__ returning False or __len__ returning zero; the built-in falsy values are None, False, 0, 0.0, the empty string and empty collections. Everything else is truthy.
Read the full explanation - Junior
What are unpacking and starred expressions good for?
Unpacking distributes the items of an iterable over several names in one line, the star * captures a variable number of items into a list, and * and ** also expand values in calls or merge collections.
Read the full explanation - Junior
What does slicing do on a list or str, and does it produce a copy?
On list, str and tuple, slicing creates a new object holding the requested part, but it is a shallow copy; mutable inner elements remain shared.
Read the full explanation - Mid-level
What is the difference between a module and a package, and how does import work?
A module is a single Python file and a package is a directory that groups modules (usually with __init__.py). On import Python searches sys.path, executes the module once, then stores it in sys.modules and reuses it for every later import.
Read the full explanation - Junior
Why do we use if __name__ == "__main__"?
To tell running a file directly apart from importing it as a module: when run directly __name__ is "__main__", and when imported it is the module's name.
Read the full explanation