50 Python Interview Questions (Junior to Senior)
Python is consistently one of the most in-demand programming languages, powering everything from web development and data science to artificial intelligence. Whether you are aiming for your first junior developer position or a staff engineering role, the Python interview will test both your knowledge of the syntax and your grasp of deeper architectural concepts.
We've compiled 50 essential Python interview questions, categorized by experience level, to help you prepare.

🟢 Junior Level: Foundations & Basics
At the junior level, interviewers want to ensure you know your way around Python's basic syntax, data types, and core mechanics.
- What are the key features of Python? Answer: Python is interpreted, dynamically typed, object-oriented, and has a massive standard library. It emphasizes readability and cleaner code.
- What is the difference between lists and tuples?
Answer: Lists are mutable (can be changed) and use square brackets
[]. Tuples are immutable and use parentheses(). - How does Python handle memory management? Answer: Python uses an automatic garbage collection system, primarily based on reference counting and a cyclic garbage collector.
- Explain the difference between
==andis. Answer:==checks for value equality, whileischecks for object identity (whether they point to the exact same memory location). - What is a lambda function?
Answer: An anonymous, inline function defined using the
lambdakeyword, typically used for short, simple operations. - What are decorators? (Basic understanding) Answer: A decorator is a function that takes another function and extends its behavior without explicitly modifying it.
- How do you handle exceptions in Python?
Answer: Using
try,except,else, andfinallyblocks to catch errors gracefully and ensure cleanup code runs. - What does the
__init__method do? Answer: It's the constructor method in object-oriented Python, called automatically when a new instance of a class is created. - Explain list comprehensions with an example.
Answer: A visually concise way to create lists. Example:
[x*2 for x in range(10)]. - What is the purpose of
selfin Python classes? Answer:selfrepresents the instance of the class and is used to access variables and methods associated with that specific object. - How do you open and read a file safely?
Answer: Using the
with open('file.txt', 'r') as f:context manager, which ensures the file is closed automatically. - What are the basic built-in data types in Python? Answer: Integers, floats, strings, booleans, lists, tuples, sets, and dictionaries.
- How do you copy an object in Python?
Answer: Using
copy.copy()for a shallow copy (copies references) andcopy.deepcopy()for a deep copy (recursively copies all nested objects). - What is PEP 8? Answer: The official style guide for writing Python code, focusing on readability and consistency (e.g., using 4 spaces for indentation).
- What does the
passstatement do? Answer: It's a null operation used as a placeholder when a statement is syntactically required but you have no code to execute yet.
🔵 Mid-Level: Data Structures & Core Mechanics
Once you have a few years of experience, interviews shift towards how effectively you can use Python's built-in data structures and handle more complex scenarios.

- Explain the concept of Duck Typing in Python. Answer: "If it walks like a duck and quacks like a duck, it's a duck." Python focuses on an object's behavior (its methods and properties) rather than its explicit class type.
- How are dictionaries implemented internally? Answer: Dictionaries in Python are implemented as hash tables. They provide O(1) average time complexity for lookups, insertions, and deletions.
- What are generators and how are they different from normal functions?
Answer: Generators use the
yieldkeyword instead ofreturnto produce a sequence of values lazily over time, saving memory compared to returning a full list. - What is the Global Interpreter Lock (GIL)? Answer: A mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes at once in standard CPython.
- Explain how
*argsand**kwargswork. Answer: They allow functions to accept an arbitrary number of positional arguments (as a tuple) and keyword arguments (as a dictionary), respectively. - What is monkey patching? Answer: Dynamically modifying a class or module at runtime, often used for hot-fixing bugs or testing (mocking).
- Explain the difference between
@staticmethodand@classmethod. Answer:@classmethodtakescls(the class) as its first argument and can access class state.@staticmethodtakes no implicit first argument and acts like a regular function housed inside a class namespace. - What are Magic Methods (Dunder Methods)?
Answer: Methods surrounded by double underscores (e.g.,
__str__,__add__) that allow you to define custom behavior for built-in operations (like printing or adding objects). - How do you merge two dictionaries in modern Python (3.9+)?
Answer: Use the union operator
|(e.g.,dict1 | dict2) or the in-place update operator|=. - What is the time complexity of searching an element in a Set vs a List? Answer: O(1) average for a Set (due to hash table implementation) versus O(N) for a List.
- What does the
__slots__attribute do? Answer: It tells Python to use a static, memory-efficient structure for class instances instead of a dynamic dictionary, reducing memory overhead significantly. - Explain context managers and how to build your own.
Answer: Objects that handle setup and teardown logic using the
withstatement. You build them by defining__enter__and__exit__methods or using the@contextlib.contextmanagerdecorator. - How does sorting work in Python? Answer: Python uses Timsort, a hybrid sorting algorithm derived from merge sort and insertion sort, optimized for real-world data with existing ordered runs.
- What is the difference between a module and a package?
Answer: A module is a single
.pyfile. A package is a directory containing multiple modules and an__init__.pyfile (though__init__.pyis optional in newer Python versions). - How do you manage dependencies and virtual environments?
Answer: Using tools like
venv,virtualenv, orPipenvto isolate project packages, and relying on arequirements.txtorpyproject.tomlfile. - Explain the
zip()function and its common use cases. Answer: It takes iterables and aggregates them in a tuple-by-tuple fashion. Commonly used for parallel iteration or creating dictionaries from two lists. - What is method resolution order (MRO) and
super()? Answer: MRO determines the order in which Python searches for base classes during multiple inheritance (using the C3 linearization algorithm).super()delegates method calls to parent or sibling classes. - Can you explain closures in Python? Answer: A function object that remembers values in its enclosing scope even if they are not present in memory. Used extensively in decorators.
- What is negative indexing and when would you use it?
Answer: Using negative numbers to access elements from the end of a sequence (e.g.,
list[-1]accesses the last element). - Why are default arguments evaluated only once?
Answer: Default argument values are evaluated when the function definition is executed, meaning mutable defaults (like
[]or{}) are shared across all calls unless handled carefully (e.g., setting the default toNone).
🟣 Senior Level: Advanced Concepts & Architecture
Senior interviews focus on system architecture, concurrency, optimization, and advanced metaprogramming features.

- How would you bypass the GIL for CPU-bound tasks?
Answer: Use the
multiprocessingmodule instead ofthreading. Each process gets its own Python interpreter and memory space, bypassing the GIL entirely. - Explain the mechanics of
asyncioand the event loop. Answer:asynciouses an event loop to cooperatively schedule and run asynchronous tasks. It's ideal for I/O bound operations (like fetching web pages or querying databases) within a single thread. - What are metaclasses and when would you use them? Answer: Metaclasses are the "classes of classes." They define how a class behaves. You use them when you need to intercept class creation, modify class dictionaries, or enforce patterns (like Singletons or ORMs).
- How would you optimize a Python application that uses too much memory?
Answer: Common strategies include using generators instead of lists, employing
__slots__in classes to remove the__dict__overhead, or utilizing libraries like NumPy for large datasets. - Explain the descriptor protocol in Python.
Answer: Descriptors are objects with
__get__,__set__, or__delete__methods. They are the mechanism behind properties, methods, static methods, and super. - Compare Threading vs Multiprocessing vs Asyncio. When to use which?
Answer: Use
threadingfor blocking I/O (like file reads),asynciofor scalable non-blocking I/O (like network requests), andmultiprocessingfor CPU-heavy tasks. - How does Python's execution model (bytecodes and PVM) work?
Answer: Python source code is compiled down to lower-level, platform-independent bytecode (
.pycfiles), which is then interpreted by the Python Virtual Machine (PVM) line by line. - What is the difference between
__new__and__init__? Answer:__new__is responsible for creating and returning a new instance (called first), while__init__initializes the existing instance. - How would you handle memory leaks in a long-running Python process?
Answer: Identify cyclically referenced objects, utilize weak references where appropriate, profile with tools like
tracemalloc, and ensure large data structures are properly cleared. - What are weak references (
weakref) and why are they useful? Answer: A reference to an object that does not increase its reference count. This allows the object to be garbage collected, preventing memory leaks in caching or observer patterns. - How can you write C extensions for Python?
Answer: You can use the standard C-API, Cython, or
ctypes/cffito write or interface with C/C++ code, typically to drastically improve CPU execution speed. - What is the difference between bounded and unbounded methods?
Answer: A bounded method is bound to an instance (passing
selfautomatically), whereas an unbounded method is just a function sitting in a class namespace that needs an instance explicitly passed if called. - How do abstract base classes (ABCs) work in Python?
Answer: Using the
abcmodule, they provide a blueprint by defining abstract methods that must be strictly implemented by any concrete subclass, enforcing structural consistency. - Explain how Python imports and caches modules internally (
sys.modules). Answer: Python checks thesys.modulesdictionary first. If a module is found, it uses the cached version. If not, it loads, compiles, and executes the module, then caches it for future imports. - How would you design a scalable microservice architecture in Python? Answer: Use asynchronous frameworks (e.g., FastAPI), decouple services via message brokers (RabbitMQ or Kafka), implement robust containerization (Docker/Kubernetes), and establish strong API contracts.
Final Thoughts
The goal of your preparation shouldn't just be memorizing these answers, but understanding the core why behind them. When answering these in an interview, try to tie the concept back to a real-world problem you've solved in past projects.
Good luck with your Python interview! 🐍