Python interview questions

Data types, mutability, functions, decorators, generators and pandas basics.

1. What is the difference between a list and a tuple?

A list is mutable (you can add, remove and change items) and written with []. A tuple is immutable and written with (). Tuples are slightly faster, can be dictionary keys and suit fixed data such as coordinates.

2. Which Python types are mutable and immutable?

Mutable: list, dict, set and most user-defined objects. Immutable: int, float, bool, str, tuple and frozenset. Changing an immutable value actually creates a new object.

3. How does a dictionary work?

It stores key-value pairs in a hash table, giving average O(1) lookup by key. Keys must be hashable (immutable). Since Python 3.7 dictionaries keep insertion order.

4. What are *args and **kwargs?

*args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dictionary. They let a function accept a variable number of arguments.

5. What is the difference between is and ==?

== compares values; is checks whether both names refer to the same object in memory. Use is for None checks (x is None).

6. What is a list comprehension?

A compact way to build a list: [x * x for x in range(10) if x % 2 == 0] gives the squares of even numbers. There are also set and dictionary comprehensions.

7. What is a decorator?

A function that takes another function and returns a new function that adds behaviour around it (logging, timing, access checks). Applied with @decorator_name above the function definition.

8. What is a generator?

A function that uses yield to produce values one at a time, keeping its state between calls. It saves memory for large sequences because values are created only when needed.

9. What is the difference between a shallow copy and a deep copy?

A shallow copy (copy.copy, list.copy()) creates a new outer object but shares the inner objects. A deep copy (copy.deepcopy) copies everything recursively, so changes to nested items do not affect the original.

10. What are self and __init__?

__init__ is the constructor method that runs when an object is created and sets up its attributes. self is the first parameter of instance methods and refers to the object itself.

11. What is a lambda function?

A small anonymous function written in one expression: square = lambda x: x * x. Often used with sorted, map and filter, for example sorted(items, key=lambda p: p[1]).

12. How does exception handling work in Python?

Code that may fail goes in try; except catches specific errors; else runs if no error happened; finally always runs (for cleanup). raise throws an exception yourself.

13. What is the GIL?

The Global Interpreter Lock in CPython lets only one thread run Python bytecode at a time. Threads still help for waiting on I/O, but for CPU-heavy work you use multiprocessing.

14. What are pandas Series and DataFrame?

pandas is a data-analysis library. A Series is a one-dimensional labelled column; a DataFrame is a table of rows and columns. Common operations: read_csv, head, groupby, merge, fillna and filtering rows with conditions such as df[df.marks > 60].

Asked in: Deloitte

All interview preparation