Fundamentals 11 min read

9 Common Python Interview Questions and Answers

This article reviews nine frequently asked Python interview questions, covering lists, tuples, dictionaries, string reversal, memory management, sessions, cookies, tokens, GET vs POST, decorators, concurrency concepts, comprehensions, and generators, providing concise explanations and code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
9 Common Python Interview Questions and Answers

As a programmer, you may have encountered technical interview questions; this article summarizes nine typical Python interview questions for reference.

1: Difference between Python list, tuple, and dict

Lists are mutable dynamic arrays with O(1) random access and O(n) insertion/deletion; tuples are immutable arrays; dictionaries are mutable hash tables using open addressing for collision resolution.

2: Reverse a string or number

String reversal can be done with slicing:

<code>str1 = "长风几万里,吹度玉门关"
print(str1[::-1])</code>

Numbers can be reversed similarly:

<code># Positive integer
number = 10002
new_number = int(str(number)[::-1])
# Negative integer
number = -10002
new_number = int('-{0}'.format(str(abs(number))[::-1]))</code>

3: Python memory management

Python uses a garbage collector with reference counting; macros Py_INCREF(op) and Py_DECREF(op) adjust the reference count, and when it reaches zero, the object's __del__ destructor frees the memory.

4: Session, Cookie, Token

Session stores data on the server; Cookie is a client‑side implementation that often holds the session ID; Token (commonly used in REST APIs) carries user identity information and is sent in request headers.

5: Difference and purpose of GET and POST

GET and POST have no intrinsic protocol limits, but servers and browsers impose length constraints; GET is idempotent and safe for data retrieval, while POST is used for data submission and is not idempotent.

Idempotence means multiple identical requests produce the same effect and result.

6: Python decorators

Decorators are functions that wrap other functions to add behavior without modifying the original code. Example:

<code>def log(func):
    def wrapper(*args, **kwargs):
        print(f"Calling {func.__name__}")
        result = func(*args, **kwargs)
        print(f"Finished {func.__name__}")
        return result
    return wrapper</code>

Decorators are useful for logging, performance testing, transaction handling, permission checks, and caching.

7: GIL, processes, threads, coroutines

The Global Interpreter Lock ensures only one thread executes Python bytecode at a time; threads are suitable for I/O‑bound tasks, while CPU‑bound work benefits from multiprocessing. Coroutines (async/await) provide lightweight concurrency without thread switching overhead.

8: List, dict, and set comprehensions

<code># List comprehension
list1 = [i for i in range(5)]
# Dict comprehension
dict1 = {k: v for k, v in some_dict.items()}
# Set comprehension
set1 = {x*2 for x in [1, 2]}
</code>

These comprehensions create new collections concisely and improve code readability.

9: Generators

<code># Generator expression
x = (i for i in range(10))
print(x)  # <generator object <genexpr> at 0x...>
</code>

Generators produce items lazily, saving memory for large sequences and enabling iteration without materializing the entire collection.

In summary, mastering these topics equips candidates to answer Python interview questions effectively.

memory managementPythonConcurrencyinterviewdata structuresdecoratorsGenerators
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.