Python interview questions & answers
Python is a high-level, dynamically typed, interpreted language known for readable syntax and a vast ecosystem spanning web, data science, automation, and AI. It emphasizes readability, uses indentation for blocks, and manages memory automatically with reference counting plus a cyclic garbage collector. Interviews test data structures, mutability, scoping, and idiomatic features.
Updated 2026-06-18 · 15 real, commonly-asked questions with answers.
Key takeaways
- Python is a high-level, dynamically typed, interpreted language known for readable syntax and a vast ecosystem spanning web, data science, automation, and AI.
- Core areas to revise for Python: Data structures (list, dict, set, tuple), Mutability & copying, Comprehensions & generators, Decorators & closures, Scope (LEGB).
- This guide answers 15 of the most-asked Python interview questions — rehearse them in OnJob's free AI mock interview.
Top 15 Python interview questions
Q1.What is the difference between a list and a tuple?
A list is mutable, so you can add, remove, or change elements after creation, and it uses square brackets. A tuple is immutable, fixed after creation, and uses parentheses, which makes it hashable and usable as a dictionary key. Tuples are also slightly faster and signal that the data should not change.
Q2.Explain mutable versus immutable types in Python.
Mutable types like lists, dicts, and sets can be changed in place without creating a new object. Immutable types like int, float, str, tuple, and frozenset cannot be altered; any operation produces a new object. This matters for function arguments, since passing a mutable object lets a function modify the caller's data.
Q3.What is a dictionary and how is it implemented?
A dictionary is a collection of key-value pairs with unique, hashable keys, written with curly braces. It is implemented as a hash table, giving average O(1) lookup, insertion, and deletion. Since Python 3.7, dictionaries also preserve insertion order as a language guarantee.
Q4.What are list comprehensions?
List comprehensions are a concise syntax to build a list from an iterable in a single expression, such as squaring each value in a range. They can include conditions to filter elements. They are usually faster and more readable than an equivalent for loop with append.
Q5.What is the difference between == and is?
The == operator compares values for equality, checking whether two objects hold equivalent data. The is operator compares identity, checking whether two names refer to the exact same object in memory. Two distinct lists with the same contents are equal by == but not by is.
Q6.What are args and kwargs?
The single-star args collects extra positional arguments into a tuple, letting a function accept any number of positional inputs. The double-star kwargs collects extra keyword arguments into a dictionary. Together they let functions accept a flexible, variable number of arguments.
Q7.What is a decorator?
A decorator is a function that takes another function and returns a modified version, used to add behavior without changing the original code. It is applied with the at-symbol syntax above a function definition. Common uses include logging, timing, caching, and access control.
Q8.Explain the difference between deep copy and shallow copy.
A shallow copy duplicates the outer object but shares references to nested objects, so changing a nested element affects both. A deep copy recursively duplicates everything, producing a fully independent object. Use deep copy when nested mutable structures must not be shared.
Q9.What is the Global Interpreter Lock (GIL)?
The GIL is a mutex in CPython that allows only one thread to execute Python bytecode at a time. It simplifies memory management but prevents true parallel CPU-bound execution with threads. For CPU-bound work use multiprocessing; threads still help I/O-bound tasks because the GIL is released during I/O.
Q10.What are generators and how do they differ from lists?
Generators produce items lazily one at a time using the yield keyword, computing values on demand instead of storing them all. This makes them memory-efficient for large or infinite sequences. Unlike a list, a generator can be iterated only once and does not support indexing.
Q11.How is memory managed in Python?
Python uses automatic memory management based primarily on reference counting: each object tracks how many references point to it, and it is freed when that count reaches zero. A supplementary cyclic garbage collector detects and frees reference cycles that counting alone cannot. Memory is allocated from private heaps managed by the interpreter.
Q12.Explain Python's scope resolution (LEGB rule).
Python resolves names by searching scopes in order: Local (inside the current function), Enclosing (any outer function), Global (module level), and Built-in (Python's predefined names). The first match wins. The global and nonlocal keywords let you rebind names in outer scopes.
Q13.What is the difference between append() and extend() on a list?
append() adds its single argument as one element at the end, so appending a list nests it. extend() iterates over its argument and adds each element individually, flattening one level. So appending [2,3] to [1] gives [1,[2,3]] while extending gives [1,2,3].
Q14.What are lambda functions?
A lambda is a small anonymous function defined inline with the lambda keyword, limited to a single expression. For example, a lambda that returns its input plus one. They are handy as short throwaway callbacks for functions like sorted, map, and filter.
Q15.How do you handle exceptions in Python?
Use a try block for code that might fail, one or more except blocks to catch specific exception types, an optional else block that runs if no exception occurred, and a finally block that always runs for cleanup. You raise exceptions with the raise statement. Catching specific exceptions rather than a bare except is the recommended practice.
More interview topics
Practise & prepare
Practise Python out loud
Reading answers is step one. Rehearse them in OnJob's free AI mock interview, get instant feedback, then apply to AI-matched jobs in one click.