Python practical and code-reading questions
Short code snippets interviewers show on screen, with what to say about each.
3, 1 and 3.5. Floor division, remainder, true division.
It catches everything, including KeyboardInterrupt and real bugs, and makes failures silent. Catch the specific exception, or at least Exception, and log it.
with open(path) as f: for line in f: ... The with statement closes the file; iterating the file object reads lazily, so large files are fine.
sorted returns a new list and works on any iterable; list.sort() sorts in place and returns None. Returning the result of .sort() is a classic bug.
A per-project Python install with its own packages. It stops version conflicts between projects and makes requirements reproducible.
Split on whitespace, count with collections.Counter, return counter.most_common(1)[0][0]. Mention lowercasing and stripping punctuation if the interviewer asks about edge cases.
Practise out loud. Say each answer once as written, then again in your own words with your own number or story.
Have a number ready. "My Python score is at benchmark on a timed test" is a stronger answer than "I am good at it". Take the test and quote the result.
More in Programming