Basic Python questions
1. What is the difference between a list and a tuple?
Lists are mutable and use square brackets; tuples are immutable and use parentheses. Tuples are hashable when their items are, so they can be dictionary keys, and they are slightly faster and safer for fixed records.
2. What are mutable and immutable types in Python?
Immutable objects cannot change after creation: int, float, str, tuple, frozenset, bytes. Mutable objects can: list, dict, set, bytearray and most user-defined classes. This matters for default arguments and dictionary keys.
3. Why should you not use a mutable default argument?
Default values are evaluated once when the function is defined, so a default list or dict is shared across calls and keeps growing. Use None as the default and create the list inside the function.
Intermediate Python questions
4. What is a decorator?
A decorator is a function that takes a function and returns a new function that wraps it, applied with @name above a definition. It is used for logging, caching (functools.lru_cache), authentication and timing. functools.wraps keeps the original name and docstring.
5. What is the difference between a generator and a list?
A list stores all items in memory. A generator, created with yield or a generator expression, produces items lazily one at a time, so it can handle very large or infinite sequences with constant memory. It can only be iterated once.
6. Explain *args and **kwargs.
*args collects extra positional arguments into a tuple and **kwargs collects extra keyword arguments into a dict. They are used to write flexible functions and to forward arguments to another function.
7. What is the GIL?
The Global Interpreter Lock in CPython allows only one thread to execute Python bytecode at a time. Threads still help for I/O-bound work; for CPU-bound work use multiprocessing, native extensions or async I/O for concurrency.
8. Shallow copy vs deep copy?
A shallow copy (copy.copy, list slicing) creates a new outer container but shares the nested objects. A deep copy (copy.deepcopy) recursively copies nested objects, so changes to inner lists do not affect the original.
Advanced Python questions
9. How does async/await work in Python?
async def defines a coroutine. await pauses it until an awaitable finishes, letting the asyncio event loop run other coroutines meanwhile. It is ideal for many concurrent network calls (FastAPI, aiohttp) but does not speed up CPU-bound code.
10. Django vs FastAPI: which would you choose?
Django is batteries-included: ORM, admin, auth and templates, great for full web apps. FastAPI is a lightweight async framework with automatic validation through Pydantic and OpenAPI docs, ideal for APIs and ML model serving. The choice depends on whether you need the full stack or a fast API layer.
Want to practise these with a trainer? APEX live batches include mock interviews, real projects and placement support. See upcoming batches.
