Python Interview Questions - Advanced
Master advanced Python interview questions covering Iterators, Generators, GIL, Asyncio, and more.
Master advanced Python interview questions covering Iterators, Generators, GIL, Asyncio, and more. This interview-focused guide covers essential python interview questions - advanced concepts for technical interviews.
Python Interview Questions – Level 4: Advanced Python
Take your Python knowledge to the next level. This section covers memory management, concurrency, asynchronous programming, and internal implementation details that distinguish expert Python developers.
161. What is iterator?
An iterator is an object that contains a countable number of values and can be iterated upon. It implements the iterator protocol: __iter__() and __next__().
nums = [1, 2, 3]
it = iter(nums)
print(next(it)) # 1
print(next(it)) # 2
162. What is iterable?
An iterable is an object capable of returning its members one at a time, such as list, str, tuple, or dict. You can get an iterator from an iterable using iter().
# Strings are iterable
for char in "Hi":
print(char)
163. What is generator?
A generator is a function that returns an iterator object using the yield keyword. It is memory-efficient because it generates values lazily.
def my_gen():
yield 1
yield 2
g = my_gen()
print(list(g)) # [1, 2]
164. Difference between yield and return?
- return: Exits the function and returns a final value.
- yield: Pauses the function, yields a value, and saves state to be resumed later.
def count_up_to(n):
count = 1
while count <= n:
yield count
count += 1
165. What is generator expression?
A compact, memory-efficient way to create a generator using parentheses: (x**2 for x in range(10)).
# List comprehension (creates full list)
squares_list = [x**2 for x in range(5)]
# Generator expression (lazy evaluation)
squares_gen = (x**2 for x in range(5))
print(next(squares_gen)) # 0
166. What is comprehension?
A concise syntax for creating collections (lists, sets, dicts) from existing iterables.
evens = [x for x in range(10) if x % 2 == 0]
print(evens) # [0, 2, 4, 6, 8]
167. Types of comprehensions?
- List:
[x for x in r] - Set:
{x for x in r} - Dict:
{k: v for k, v in r}
# Dict comprehension example
names = ["Alice", "Bob"]
name_lengths = {name: len(name) for name in names}
# {'Alice': 5, 'Bob': 3}
168. What is memory-efficient programming?
Techniques to minimize memory usage, such as using generators, __slots__, and avoid loading large datasets into memory at once.
import sys
# Generator Size vs List Size
gen = (i for i in range(10000))
lst = [i for i in range(10000)]
print(sys.getsizeof(gen)) # ~100 bytes
print(sys.getsizeof(lst)) # ~85000 bytes
169. What is deepcopy vs shallow copy?
- Shallow Copy: New object with references to original nested objects.
- Deep Copy: New object with recursive copies of all nested objects.
import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)
original[0][0] = 99
print(shallow[0][0]) # 99 (Affected)
print(deep[0][0]) # 1 (Unaffected)
170. What is copy module?
Provides copy.copy() (shallow) and copy.deepcopy() (deep) functions.
import copy
a = [1, 2, 3]
b = copy.copy(a)
171. What is threading?
Running multiple threads within a single process. Best for I/O-bound tasks.
Example:
import threading
import time
def print_numbers():
for i in range(5):
time.sleep(1)
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
print("Main thread continues...")
172. What is multiprocessing?
Running multiple separate processes, each with its own GIL. Best for CPU-bound tasks.
Example:
from multiprocessing import Process
def square_numbers():
for i in range(5):
print(i*i)
if __name__ == "__main__":
process = Process(target=square_numbers)
process.start()
process.join()
173. What is GIL?
The Global Interpreter Lock (GIL) is a mutex that prevents multiple native threads from executing Python bytecodes at once, ensuring thread safety for the interpreter.
Example:
# Even with threads, CPU-bound tasks don't speed up much because of GIL
import threading
import time
def count(n):
while n > 0:
n -= 1
start = time.time()
t1 = threading.Thread(target=count, args=(10000000,))
t2 = threading.Thread(target=count, args=(10000000,))
t1.start()
t2.start()
t1.join()
t2.join()
end = time.time()
print(f"Time: {end - start:.2f}s")
174. Difference between process and thread?
- Thread: Shares memory, affected by GIL, lightweight.
- Process: Isolated memory, bypasses GIL, heavier.
Example:
import multiprocessing
import threading
# Shared vs isolated memory demo (conceptual)
shared_list = []
def add_to_list_worker(name):
shared_list.append(name)
# Threads share memory
t1 = threading.Thread(target=add_to_list_worker, args=("Thread 1",))
t2 = threading.Thread(target=add_to_list_worker, args=("Thread 2",))
t1.start()
t2.start()
t1.join()
t2.join()
print("From threads:", shared_list) # Likely has both entries
175. What is async programming?
Non-blocking execution style, allowing a single thread to handle multiple tasks by switching between them during I/O waits.
Example:
import asyncio
async def greet(name):
await asyncio.sleep(1)
print(f"Hello {name}!")
async def main():
await asyncio.gather(greet("Alice"), greet("Bob"))
asyncio.run(main())
176. What is asyncio?
The standard library for writing concurrent code using async/await.
Example:
import asyncio
async def count():
print("One")
await asyncio.sleep(1)
print("Two")
async def main():
await asyncio.gather(count(), count(), count())
asyncio.run(main())
177. What is await?
A keyword used to yield control back to the event loop while waiting for an asynchronous task to finish.
Example:
import asyncio
async def slow_task():
await asyncio.sleep(2)
return "Done!"
async def main():
result = await slow_task() # Wait here
print(result)
asyncio.run(main())
178. What is event loop?
The engine that runs asyncio tasks, managing their execution and I/O events.
Example:
import asyncio
async def hello():
print("Hello")
await asyncio.sleep(1)
print("World")
loop = asyncio.get_event_loop() # Get the loop
loop.run_until_complete(hello()) # Run task
179. What is context manager?
An object that implements __enter__ and __exit__, usually used with the with statement for resource management.
Example:
class MyContext:
def __enter__(self):
print("Entering")
return "Resource"
def __exit__(self, exc_type, exc_val, exc_tb):
print("Exiting")
with MyContext() as res:
print(f"Using {res}")
180. What is with statement?
Simplifies exception handling by encapsulating common preparation and cleanup tasks (e.g., closing a file).
Example:
# Safe file handling
with open("test.txt", "w") as f:
f.write("Hello World!")
# File is automatically closed!
181. What is file handling?
Reading from and writing to files using open() and related methods.
Example:
# Writing to file
with open("example.txt", "w") as f:
f.write("Line 1\nLine 2\n")
# Reading from file
with open("example.txt", "r") as f:
content = f.read()
print(content)
182. Modes of file opening?
'r' (read), 'w' (write), 'a' (append), 'b' (binary), 'r+' (read/write).
Example:
# Append
with open("log.txt", "a") as f:
f.write("New log entry\n")
# Read binary
with open("image.jpg", "rb") as f:
image_data = f.read()
183. What is pickle?
A module for serializing and deserializing Python objects into byte streams.
Example:
import pickle
data = {"name": "Alice", "age": 30}
# Serialize (dump)
with open("data.pkl", "wb") as f:
pickle.dump(data, f)
# Deserialize (load)
with open("data.pkl", "rb") as f:
loaded_data = pickle.load(f)
print(loaded_data)
184. What is serialization?
Converting an object into a storable/transmittable format (e.g., JSON, Pickle).
Example:
import json
data = {"key": "value"}
json_str = json.dumps(data) # Serialize to JSON string
print(json_str)
185. What is deserialization?
Reconstructing an object from its serialized format.
Example:
import json
json_str = '{"name": "Bob", "age": 25}'
data = json.loads(json_str) # Deserialize from JSON string
print(data["name"])
186. What is regular expression?
A sequence of characters that forms a search pattern (Regex), used with the re module.
Example:
import re
text = "My email is user@example.com"
pattern = r'\w+@\w+\.\w+'
match = re.search(pattern, text)
if match:
print("Email found:", match.group())
187. What is re module?
The Python module for working with Regular Expressions.
Example:
import re
# Find all numbers
text = "There are 3 apples and 5 oranges"
numbers = re.findall(r'\d+', text)
print(numbers) # ['3', '5']
188. What is pattern matching?
Checking if a string matches a pattern. Python 3.10+ also supports Structural Pattern Matching (match/case).
Example:
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case _:
return "Other error"
print(http_error(404))
189. What is unit testing?
Testing individual units of code to ensure correctness.
Example:
def add(a, b):
return a + b
# Simple test
assert add(2, 3) == 5
assert add(-1, 1) == 0
print("All tests passed!")
190. What is unittest module?
The built-in testing framework for Python.
Example:
import unittest
def add(a, b):
return a + b
class TestMath(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
if __name__ == "__main__":
unittest.main()
191. What is pytest?
A powerful third-party testing framework known for its simplicity and extensive plugin support.
Example:
# test_math.py
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
# Run with: pytest test_math.py
192. What is logging?
A flexible system for tracking events during program execution, preferred over print().
Example:
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Program started")
logger.warning("Low disk space warning")
193. What is virtualenv?
A tool for creating isolated Python environments to manage project-specific dependencies.
Example:
# Create virtual env (command line)
# virtualenv my_env
# Activate on Windows: my_env\Scripts\activate
# Activate on Mac/Linux: source my_env/bin/activate
194. What is poetry?
A modern tool for dependency management and packaging that uses pyproject.toml.
Example:
# pyproject.toml (conceptual)
[tool.poetry]
name = "my-project"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.25.1"
195. What is pip freeze?
Outputs all installed packages and their versions, often piped to requirements.txt.
Example:
# Command line
# pip freeze > requirements.txt
# Then install from requirements: pip install -r requirements.txt
196. What is wheel?
The standard binary distribution format for Python packages.
Example:
# Command line to build a wheel
# python setup.py bdist_wheel
197. What is CPython?
The reference implementation of Python, written in C and Python.
Example:
# Check implementation
import platform
print(platform.python_implementation()) # 'CPython'
198. What is PyPy?
An alternative implementation using JIT compilation for high performance.
Example:
# PyPy runs loops much faster! (conceptual)
def loop():
total = 0
for i in range(1000000):
total += i
return total
199. What is C-extension?
High-performance modules written in C/C++ and used within Python.
Example:
// Simple C extension (conceptual)
#include <Python.h>
static PyObject* hello(PyObject* self) {
return PyUnicode_FromString("Hello from C!");
}
200. What is Python used for in AI & ML?
Python is the industry standard for AI/ML due to libraries like NumPy, Pandas, Scikit-learn, TensorFlow, and PyTorch.
Example:
import numpy as np
# Simple numpy example
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]