Interview Q&A / Python

INTERVIEW Q&A · 81 QUESTIONS

Python interview questions

Lists, dictionaries, generators, decorators and the Python questions asked in service-company and data roles.

  1. What are *args and **kwargs in Python?

    In a function definition, *args collects any extra positional arguments into a tuple, and **kwargs collects any extra keyword arguments into a dict. They let a function accept a variable number of inputs. The same * and ** symbols also unpack a list or dict when calling a function.

    BeginnerIn-depth3 min read
  2. What are mutable and immutable objects in Python, and why is a mutable default argument a bug?

    Mutable objects such as lists, dicts and sets can change in place; immutable ones such as int, str and tuple cannot. Default arguments are evaluated once, when the function is defined, so a list used as a default is shared across every call. Use None as the default and create the list inside the function.

    BeginnerIn-depth3 min read
  3. What is the difference between a list and a tuple in Python?

    A list is mutable, so you can add, remove or change items after creating it. A tuple is immutable, so its contents are fixed once created. Tuples are slightly lighter and can be used as dictionary keys if all their items are hashable; lists cannot.

    BeginnerIn-depth3 min read
  4. What is the difference between a shallow copy and a deep copy in Python?

    A shallow copy creates a new outer container but keeps references to the same inner objects, so changing a nested list affects both. A deep copy recursively copies everything inside, so the two are fully independent. Use copy.copy() for shallow and copy.deepcopy() for deep.

    BeginnerIn-depth3 min read