Python interview questions and quick answers
Lists, dictionaries, generators, decorators and the Python questions asked in service-company and data roles.
Review 1,016 questions and short answers on Python. Start with real examples below, then search, practise and follow links to full explanations.
Python: questions worth revising first
These answers come from AI Tapri's existing question bank and published interview guides. Open each answer and read the linked in-depth guide for examples and context.
01Write a function to check whether two strings are anagrams of each other.
Two strings are anagrams if they contain the same characters with the same counts. Compare Counter(a) with Counter(b) in O(n) time, or compare sorted(a) with sorted(b) in O(n log n) time.
Read the full explanation02What is the difference between append(), extend() and insert() on a list?
append(x) adds x as one item at the end. extend(iterable) adds each element of the iterable at the end. insert(i, x) puts x at position i and shifts later items right, which costs O(n).
Read the full explanation03What are *args and **kwargs in Python?
In a function definition, *args collects any extra positional arguments into a tuple, and **kwargs collects any extra keyword arguments into a dict. They let a function accept a variable number of inputs. The same * and ** symbols also unpack a list or dict when calling a function.
Read the full explanation04What does the assert statement do, and when should you not use it?
assert condition, message raises AssertionError if the condition is false. It is for internal sanity checks during development. Do not use it to validate user input, because assertions are removed when Python runs with the -O flag.
Read the full explanation05Why is a bare "except:" considered bad practice in Python?
A bare except catches everything, including KeyboardInterrupt and SystemExit, so Ctrl+C stops working and real bugs get hidden. Catch specific exceptions, or at most "except Exception", and log or re-raise what you cannot handle.
Read the full explanation06What are the main built-in data types in Python?
Numbers (int, float, complex, bool), text (str), sequences (list, tuple, range), mappings (dict), sets (set, frozenset), bytes types and NoneType. Lists, dicts and sets are mutable; the rest are immutable.
Read the full explanation07How would you explain "Write a function to check whether two strings are anagrams of each other" in an interview?
"First check the lengths if spaces aren't ignored, since different lengths can't be anagrams. Then compare character counts with Counter. Sorting both strings also works and is easy to explain, but it is slower asymptotically."
Read the full explanation08How would you explain "Write a function to check whether two strings are anagrams of each other" to a beginner?
"listen" and "silent" use the same letters, just in a different order. So order does not matter; counts do.
Read the full explanationFind your next answer.
Pick a topic, search a keyword, or open a question to reveal the answer.
Python quick answers
Free to practise. No account needed. Made for quick revision, not rote memorisation.
Keep preparing for Python
Quick answers help with recall; detailed guides explain the reasoning, examples and common mistakes behind each answer.