Python Junior interview questions and answers
75 questions for a Python (Junior) interview - with correct answers, explanations and links to the docs.
- 1.Which of these built-in types is immutable?
- 2.What does this code print?
- 3.What is the difference between
==andis? - 4.How should you check that a variable is
None? - 5.What does
7 // 2return? - 6.What does
-7 // 2return? - 7.What does
print(0.1 + 0.2 == 0.3)print? - 8.Which values are falsy in Python?
- 9.What does
[1, 2, 3, 4, 5][1:4]return? - 10.What does
s[::-1]do with a strings? - 11.What happens when you run this code?
- 12.What is the idiomatic way to join a list of strings with commas?
- 13.What does this f-string print?
- 14.What is the difference between
list.append(x)andlist.extend(x)? - 15.What does
list.sort()return? - 16.How do you sort a list of words by length?
- 17.What is the idiomatic way to loop over a list with indexes?
- 18.What does
zip([1, 2, 3], ["a", "b"])produce when turned into a list? - 19.What does
range(2, 10, 3)produce? - 20.When does the
elseblock of aforloop run? - 21.What does this list comprehension produce?
- 22.How do you build a dict that maps each word to its length?
- 23.What does
d.get("missing")return when the key is absent? - 24.In what order does iterating over a dict return its keys?
- 25.Why can't a list be used as a dict key?
- 26.What is the fastest way to remove duplicates from a list of hashable items if order doesn't matter?
- 27.What does
{1, 2, 3} & {2, 3, 4}return? - 28.How do you create an empty set?
- 29.How do you create a tuple with one element?
- 30.What is the Pythonic way to swap two variables?
- 31.What does
first, *rest = [1, 2, 3, 4]assign torest? - 32.What does this code print?
- 33.What do
*argsand**kwargscollect in a function signature? - 34.What does a function without a
returnstatement return? - 35.In which order does Python look up a name inside a function?
- 36.What happens when you run this code?
- 37.What is
nonlocalfor? - 38.What is a limitation of
lambdain Python? - 39.What does
if __name__ == "__main__":do? - 40.What does
import mathdo the second time it runs in the same process? - 41.Why is
from module import *discouraged? - 42.What is a virtual environment for?
- 43.Why run
python -m pip install ...instead of justpip install ...? - 44.What does PEP 8 recommend for indentation?
- 45.Which naming style does PEP 8 use for functions and classes?
- 46.What is
selfin a method? - 47.What does
__init__do? - 48.What does this code print?
- 49.What is the difference between
__str__and__repr__? - 50.What does
super().__init__()do in a subclass? - 51.What does
isinstance(True, int)return? - 52.Why is
isinstance(x, Base)usually better thantype(x) == Base? - 53.What does "duck typing" mean in Python?
- 54.What does the
elseblock oftrydo? - 55.When does the
finallyblock run? - 56.Why is a bare
except:a bad idea? - 57.How do you raise an error when a function gets an invalid argument?
- 58.Which exception does
[1, 2, 3][5]raise? - 59.Why open files with
with open(...) as f:? - 60.What should you pass to
open()when reading a text file to avoid platform-dependent surprises? - 61.Which module gives an object-oriented way to work with file paths?
- 62.What does
json.loadsdo? - 63.What does
print(2 ** 100)print? - 64.What does
round(2.5)return? - 65.What does
1 < x < 10mean in Python? - 66.What does
"" or "default"evaluate to? - 67.What is Python's conditional expression syntax?
- 68.What does
grid = [[0] * 3] * 3create? - 69.What does
copy.copy(nested_list)copy? - 70.What is the average time complexity of
x in some_set? - 71.What is
passused for? - 72.What does a docstring do?
- 73.What does
matchdo in Python 3.10 and later? - 74.How does pytest know that a function is a test?
- 75.How do you check in pytest that code raises an exception?