9 Common Python Interview Questions and Answers
This article presents nine frequently asked Python interview questions covering lists, tuples, dictionaries, string reversal, memory management, session/cookie/token concepts, HTTP methods, decorators, concurrency, comprehensions, and generators, each explained with clear descriptions and code examples for developers preparing for technical interviews.
Python interviewers often ask about basic data structures; this article explains the differences between lists, tuples, and dictionaries, highlighting that lists are mutable dynamic arrays (O(1) access, O(n) insert/delete), tuples are immutable sequences, and dictionaries are hash tables with copy considerations.
To reverse a string or number, Python slicing can be used:
str1 = "长风几万里,吹度玉门关"
print(str1[::-1])For numbers:
# Positive integer
number = 10002
new_number = int(str(number)[::-1])
# Negative integer
number = -10002
new_number = int('-{0}'.format(str(abs(number))[::-1]))Python's memory management relies on reference counting and a cyclic garbage collector; macros Py_INCREF(op) and Py_DECREF(op) adjust the reference count, and when it reaches zero the object's __del__ method frees the memory.
Session, Cookie, and Token are authentication mechanisms: Session data lives on the server, Cookie stores the session ID (or other non‑sensitive data) on the client, and Token (often used in REST APIs) is a signed credential kept in headers, localStorage, or cookies.
GET and POST differ mainly in semantics: GET is idempotent and used for data retrieval, while POST is non‑idempotent and used for data submission; both transmit data in the request body, and limits are imposed by browsers or server configurations.
Python decorators are higher‑order functions that wrap other functions to add behavior such as logging, performance testing, transaction handling, permission checks, or caching, similar to AOP in Java.
Concurrency concepts: the Global Interpreter Lock (GIL) restricts true parallel thread execution, making multiprocessing (via the multiprocessing module) preferable for CPU‑bound tasks, while async/await and coroutines provide lightweight concurrency for I/O‑bound workloads.
List, dictionary, and set comprehensions allow concise creation of new collections from existing iterables, e.g.:
list1 = [i for i in range(5)]
dict1 = {k: v for k, v in some_dict.items()}
set1 = {x*2 for x in [1,2]}Generators produce lazy sequences without allocating the entire list in memory:
x = (i for i in range(10))
# x is a generator object that yields values on demandThese nine topics provide a solid foundation for Python interview preparation, though each can be explored in much greater depth.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.