Skills & Technologies

Python Interview Questions & Answers for All Levels

18 min readUpdated May 17, 2025
Pythonprogramming languageOOP
Python is the most popular language for data science, backend development, and scripting — making Python interview questions among the most frequently searched topics by job seekers. Whether you're applying for a data scientist, backend engineer, or DevOps role, you'll face Python-specific questions. This guide covers the full spectrum: from core language fundamentals that trip up even experienced developers to advanced topics like concurrency, metaclasses, and performance optimization.

Core Python Concepts

These questions test your understanding of Python's design philosophy and language mechanics. You should be comfortable with: • Data types and mutability — knowing which types are mutable vs immutable • Memory management — how Python handles references, garbage collection, and the GIL • Functional features — decorators, generators, closures, and higher-order functions

Q1.What is the difference between a list and a tuple in Python? When would you use each?

beginner
Lists are mutable (can be modified after creation) while tuples are immutable (cannot be changed). Tuples are hashable (can be used as dictionary keys or set elements) if all their elements are hashable. When to use tuples: • Fixed collections of heterogeneous data (like a database row) • Dictionary keys or set elements • Function return values with multiple elements When to use lists: • Collections that need to grow, shrink, or be sorted • Homogeneous sequences you plan to iterate and modify Performance considerations: • Tuples are slightly faster to create and iterate due to immutability optimizations • Tuples use less memory — Python can cache small tuples and reuse them • In practice, the choice signals intent: a tuple says 'this data won't change,' which helps readers understand your code

Q2.Explain Python's GIL (Global Interpreter Lock). How does it affect multithreading?

advanced
The GIL is a mutex in CPython that allows only one thread to execute Python bytecode at a time. This means CPU-bound multithreaded Python code doesn't achieve true parallelism — threads take turns rather than running simultaneously. When the GIL doesn't matter: • The GIL is released during I/O operations (network requests, file reads, database queries) • I/O-bound multithreading is still effective and widely used Workarounds for CPU-bound parallelism: • Use the multiprocessing module — separate processes, each with its own GIL • Use libraries like NumPy that release the GIL during C-level computations • Use concurrent.futures.ProcessPoolExecutor for a higher-level API Recent developments: • Python 3.13 introduced an experimental free-threaded mode (--disable-gil) that removes the GIL entirely, though it's not yet the default

Q3.What are decorators in Python? Write a simple example.

intermediate
A decorator is a function that takes another function as input and extends its behavior without modifying its source code. It's syntactic sugar for higher-order functions. Simple example: def timer(func): def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) print(f'{func.__name__} took {time.time()-start:.2f}s') return result return wrapper Usage: @timer above a function definition. Under the hood, @timer on def my_func is equivalent to my_func = timer(my_func). Common real-world decorators: • @functools.lru_cache — memoization • @property — computed attributes • @staticmethod — static methods • Framework decorators like Flask's @app.route Best practice: Always use @functools.wraps(func) in your wrapper to preserve the original function's metadata.

Data Structures & Algorithms in Python

Python-specific implementation questions for common data structure operations. Interviewers expect you to know: • Built-in data structures — dict, set, list, deque, and their time complexities • The collections module — Counter, defaultdict, OrderedDict, namedtuple • Pythonic patterns — list comprehensions, generator expressions, and itertools

Q4.How does Python's dictionary work internally? What is its time complexity?

intermediate
Python's dict uses a hash table with open addressing (specifically, a probe sequence using perturbation). How insertion works: 1. Python computes the key's hash 2. Maps it to a table index 3. Stores the key-value pair there 4. If two keys hash to the same index (collision), Python probes the next available slot using a perturbation-based sequence Time complexity: • Average-case: O(1) for get, set, and delete • Worst-case (all keys collide): O(n), but Python's hash function makes this extremely unlikely Key details: • Since Python 3.7, dictionaries maintain insertion order (implementation detail in 3.6, guaranteed in 3.7+) • Python over-allocates the hash table (resizing at 2/3 capacity) to keep collisions low, trading memory for speed • Keys must be hashable — implementing __hash__ and __eq__ is required for custom objects used as keys

Python OOP & Design Patterns

Object-oriented programming questions that test your ability to write maintainable Python code. Key areas include: • Class mechanics — __init__, __new__, __repr__, and the MRO (Method Resolution Order) • Inheritance patterns — mixins, abstract base classes, composition vs inheritance • Design patterns — Singleton, Factory, Observer, and Strategy patterns in Pythonic style

Q5.Explain the difference between __init__ and __new__ in Python.

advanced
Core distinction: • __new__ is the class constructor — it creates and returns a new instance of the class • __init__ is the initializer — it sets up the instance after it's been created Call sequence: 1. obj = MyClass.__new__(MyClass) 2. MyClass.__init__(obj) When to override __new__: In practice, you rarely override __new__. The main use cases are: • Implementing singletons (return the same instance every time) • Subclassing immutable types (str, int, tuple) — you must set the value in __new__ since the object is immutable by the time __init__ runs • Metaclass programming Important caveat: If __new__ doesn't return an instance of the class, __init__ won't be called.

Frequently Asked Questions

Which Python version should I use in interviews?+

Use Python 3.10+ features confidently — match statements, walrus operator (:=), f-strings, and type hints are all fair game and show modern Python knowledge. Avoid Python 2 syntax. If the interviewer's environment uses an older version, they'll tell you.

How important is knowing Python internals for interviews?+

For data science and general SWE roles, understanding the GIL, dict internals, and memory model gives you an edge but isn't required. For senior backend or infrastructure roles, deep Python internals knowledge (garbage collection, C extensions, asyncio event loop) is expected.

Should I use built-in functions or implement from scratch in interviews?+

Ask the interviewer. Generally, use built-in functions for the overall solution (sorted(), collections.Counter) but be prepared to explain how they work internally. If asked to implement a data structure, write it from scratch. Showing you know the standard library is a positive signal.

Ready to land your dream job?

CareerUplift gives you AI-powered mock interviews, an ATS-optimized resume builder, and personalized coaching — everything you need to get hired faster.

Related Articles