Python Interview Questions

Python Interview Questions - Advanced

Master advanced Python interview questions covering Iterators, Generators, GIL, Asyncio, and more.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

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.

172. What is multiprocessing?

Running multiple separate processes, each with its own GIL. Best for CPU-bound tasks.

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.

174. Difference between process and thread?

  • Thread: Shares memory, affected by GIL, lightweight.
  • Process: Isolated memory, bypasses GIL, heavier.

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.

176. What is asyncio?

The standard library for writing concurrent code using async/await.

177. What is await?

A keyword used to yield control back to the event loop while waiting for an asynchronous task to finish.

178. What is event loop?

The engine that runs asyncio tasks, managing their execution and I/O events.

179. What is context manager?

An object that implements __enter__ and __exit__, usually used with the with statement for resource management.

180. What is with statement?

Simplifies exception handling by encapsulating common preparation and cleanup tasks (e.g., closing a file).

181. What is file handling?

Reading from and writing to files using open() and related methods.

182. Modes of file opening?

'r' (read), 'w' (write), 'a' (append), 'b' (binary), 'r+' (read/write).

183. What is pickle?

A module for serializing and deserializing Python objects into byte streams.

184. What is serialization?

Converting an object into a storable/transmittable format (e.g., JSON, Pickle).

185. What is deserialization?

Reconstructing an object from its serialized format.

186. What is regular expression?

A sequence of characters that forms a search pattern (Regex), used with the re module.

187. What is re module?

The Python module for working with Regular Expressions.

188. What is pattern matching?

Checking if a string matches a pattern. Python 3.10+ also supports Structural Pattern Matching (match/case).

189. What is unit testing?

Testing individual units of code to ensure correctness.

190. What is unittest module?

The built-in testing framework for Python.

191. What is pytest?

A powerful third-party testing framework known for its simplicity and extensive plugin support.

192. What is logging?

A flexible system for tracking events during program execution, preferred over print().

193. What is virtualenv?

A tool for creating isolated Python environments to manage project-specific dependencies.

194. What is poetry?

A modern tool for dependency management and packaging that uses pyproject.toml.

195. What is pip freeze?

Outputs all installed packages and their versions, often piped to requirements.txt.

196. What is wheel?

The standard binary distribution format for Python packages.

197. What is CPython?

The reference implementation of Python, written in C and Python.

198. What is PyPy?

An alternative implementation using JIT compilation for high performance.

199. What is C-extension?

High-performance modules written in C/C++ and used within Python.

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.