Python Interview Questions & Tips
What Makes Python Unique?
Python is an interpreted, object-oriented general-purpose programming language. It is beloved by many developers and one of the most popular languages in the world, ranking second to JavaScript in the 2022 Stack Overflow Developer survey.
While few aspects of Python are unique to the language, a variety of attributes that developers love and a deep toolbox of features make it an exceptional choice for a wide range of applications and projects:
Readability: Python is one of the most readable programming languages, frequently using English or understandable keywords (continue
, pass
, def
, local
, finally
, etc). It also requires consistent spacing in order to run without errors and largely avoids the usage of symbols, which both improve readability.
A robust standard library: From json
to datetime
to io
, Python has a deep collection of standard library modules that enables the development of a variety of application types out of the box.
An even-deeper ecosystem of non-standard modules: NumPy (used for scientific computing and mathematics) and Pandas (used for data analysis and manipulation) lead an incredibly deep set of non-standard-library modules, followed by other libraries such as collections
, Flask, Django and TensorFlow. All of these libraries contribute to Python’s versatility, a major aspect of its popularity.
Excellent documentation: The official Python documentation, as well as many top libraries, have comprehensive and easy-to-understand documentation, which is appreciated by Python engineers of all levels.
List comprehensions and dict comprehensions: Python also has an easy, approachable and compact syntax for iterating and mapping over lists. For example, in another language you may have to write something like this in order to iterate over a list of integers and multiply them by 3:
nums = [2, 7, 12, 32]
newlist = []
for x in nums:
newlist.append(x * 3)
print(newlist) # [6, 21, 36, 96)
1nums = [2, 7, 12, 32]
2newlist = []
3
4for x in nums:
5 newlist.append(x * 3)
6
7print(newlist) # [6, 21, 36, 96)
8
However, in Python this code can be done in one line using a list comprehension:
```
nums = [2, 7, 12, 32]
newlist = [x*3 for x in nums]
print(newlist) # [6, 21, 36, 96)
```
1```
2nums = [2, 7, 12, 32]
3newlist = [x*3 for x in nums]
4print(newlist) # [6, 21, 36, 96)
5```
6
You can even nest list comprehensions and add if
statements, though doing so can make the code slightly more difficult to read and quickly comprehend:
capital_tuples = [['Colorado', 'Denver'], ['Minnesota', 'St. Paul'], ['Massachusetts', 'Boston']]
cities_and_states_starting_with_m = [
city_or_state
for city_state_tuple in capital_tuples
for city_or_state in city_state_tuple
if city_or_state[0] == 'M'
]
print(cities_and_states_starting_with_m) # ['Minnesota', 'Massachusetts']
1capital_tuples = [['Colorado', 'Denver'], ['Minnesota', 'St. Paul'], ['Massachusetts', 'Boston']]
2cities_and_states_starting_with_m = [
3 city_or_state
4 for city_state_tuple in capital_tuples
5 for city_or_state in city_state_tuple
6 if city_or_state[0] == 'M'
7]
8print(cities_and_states_starting_with_m) # ['Minnesota', 'Massachusetts']
9
Similarly, you can use construct dictionaries using dict comprehensions:
capital_tuples = [["Colorado", "Denver"], ['Minnesota', 'St. Paul'], ['Massachusetts', 'Boston']]
capital_dict = {state: capital for state, capital in capital_tuples}
print(capital_dict) # { 'Colorado': 'Denver', 'Minnesota': 'St. Paul', 'Massachusetts': ‘Boston' }
1capital_tuples = [["Colorado", "Denver"], ['Minnesota', 'St. Paul'], ['Massachusetts', 'Boston']]
2capital_dict = {state: capital for state, capital in capital_tuples}
3print(capital_dict) # { 'Colorado': 'Denver', 'Minnesota': 'St. Paul', 'Massachusetts': ‘Boston' }
4
Lambda expressions / lambda functions: Lambda expressions are anonymous, simple functions in Python often used within high-order functions such as filter
or map
. They are syntactically restricted to a single expression and semantically they are simply syntactic sugar for a normal function definition.
nums = [2, 7, 12, 17, 23]
odd_nums = list(filter(lambda x: x % 2 != 0, nums))
print(odd_nums)
nums_squared = list(map(lambda x: x**2, nums))
print(nums_squared)
1nums = [2, 7, 12, 17, 23]
2odd_nums = list(filter(lambda x: x % 2 != 0, nums))
3print(odd_nums)
4
5nums_squared = list(map(lambda x: x**2, nums))
6print(nums_squared)
7
Python Interview Stats
We've hosted over 100k interviews on our platform. Python was the language of choice in those interviews 40% of the time, and engineers who interviewed in Python passed their interviews 56% of the time.
Below is a distribution of programming languages and their popularity in technical interviews as well as success rates in interviews, by language.
Common Python Interview Mistakes
Though Python is known for being easy to understand, debug and reason about, there are a handful of quirks that may throw off newer developers or developers coming from other languages:
Spacing matters: While strict spacing rules makes Python code more readable, it also won’t run properly without consistent spacing. For example, many languages use brackets ({
and }
) to denote the beginning and ending of code blocks or functions, but Python relies on indentation and colons.
Scope and closures: In many languages you can automatically reference variables in the enclosing scope (or other outer scopes), but in Python you can only reference local variables. In order to access variables in an outer scope the nonlocal
keyword is required, and in order to reference variables in the global scope the (you guessed it) global
keyword is required.
# Global variable
global_var = 10
def outer_function():
# Outer function variable
outer_var = 20
def inner_function():
# Local variable
inner_var = 30
print("Local variable:", inner_var) # 30
nonlocal outer_var
outer_var = 40
print("Nonlocal variable:", outer_var) # 40
global global_var
global_var = 50
print("Global variable:", global_var) # 50
print("Outer variable:", outer_var) # 20
inner_function()
outer_function()
1# Global variable
2global_var = 10
3
4def outer_function():
5 # Outer function variable
6 outer_var = 20
7
8 def inner_function():
9 # Local variable
10 inner_var = 30
11
12 print("Local variable:", inner_var) # 30
13 nonlocal outer_var
14 outer_var = 40
15 print("Nonlocal variable:", outer_var) # 40
16 global global_var
17 global_var = 50
18 print("Global variable:", global_var) # 50
19
20 print("Outer variable:", outer_var) # 20
21 inner_function()
22
23outer_function()
24
Interpreted: Python is an interpreted language, meaning errors surface at run-time; as opposed to compiled languages such as Java, where errors surface at compile time.
Python Interview Replays
Below you can find replays of mock interviews conducted on our platform in Python. The questions asked in these interviews tend to be language-agnostic (rather than asking about language-specific details and idiosyncrasies), but in these cases, the interviewee chose Python as the language they would work in.

About interviewing.io
interviewing.io is a mock interview practice platform. We've hosted over 100K mock interviews, conducted by senior engineers from FAANG & other top companies. We've drawn on data from these interviews to bring you the best interview prep resource on the web.