Python Junior interview questions and answers

75 questions for a Python (Junior) interview - with correct answers, explanations and links to the docs.

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