Skip to content
CompanyBenchCompanyBench

Python interview questions and answers

Python interview questions with short answers on data types, OOP, decorators, generators, memory management, copying and method resolution order.

Intermediate and advanced Python questions on data structures, OOP, decorators, memory management and scripting, with short answers.

Related: Python developer salaries and contract Python developers.

Questions and answers

What are Python's key features?

Python is interpreted, dynamically typed and object-oriented. It has high-level built-in data structures and a large library ecosystem, which makes development fast.

Explain classes and objects in Python.

A class is a blueprint. An object is an instance of that class. Python's object model supports inheritance, encapsulation and polymorphism.

What are decorators and how do they work?

A decorator is a higher-order function that wraps another function or method and changes its behaviour without editing its code.

How is memory managed in Python?

Python uses reference counting plus a cyclic garbage collector to free memory that is no longer used. The gc module lets you inspect and control garbage collection.

How would you automate renaming files in a folder?

Use the os module: list the files in the directory, loop over them and rename each one.

What are mutable and immutable types?

Mutable types can change after creation: list, dict and set. Immutable types cannot: int, float, str and tuple.

Explain list comprehension with an example.

A list comprehension builds a list in one expression: brackets containing an expression followed by a for clause, for example [x * x for x in numbers].

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

A shallow copy creates a new outer object but keeps references to the original nested objects. A deep copy copies every nested object recursively.

What are generators and how do they differ from iterators?

A generator is a function that uses yield to produce values one at a time, and calling it returns an iterator. Generators are memory-efficient for large sequences because they never hold the whole sequence in memory.

What is method resolution order (MRO)?

MRO is the order in which Python searches base classes for a method. It follows C3 linearisation, and you can inspect it with ClassName.__mro__.