Python basics interview questions
The questions junior Python candidates hear most, with answers you can say in under a minute.
A list is mutable and a tuple is not. Tuples are slightly faster, can be dict keys, and signal that the data should not change. Use a list when you will add or remove items.
It makes b another name for the same list. Changes through b show in a. To copy, use a.copy(), list(a) or a[:].
try/except around the risky line, catching the specific exception, such as ValueError for int('abc'). Avoid a bare except, which hides bugs.
A mapping from unique, hashable keys to values with fast lookup. Use it when you look things up by name or id rather than by position.
A compact way to build a list from an iterable with an optional condition: [n * n for n in range(5) if n % 2 == 0]. Prefer a plain loop when there are side effects.
*args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dict. They let a function accept a flexible number of arguments.
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