3 Most Asked Python Interview Questions for Data Scientists

Top python interview questions for data scientists


Alexandros Zenonos

2 years ago | 3 min read

Many blogs and websites have several questions and advice with regards to data science interviews. However, most data science interviews want to test candidates software engineering skills as well.

Here we present the top 3 most frequently asked questions in a data science interview that are about Python Programming.

What is the difference between a list and a tuple?

This question is really about understanding data structures in Python. When would you use a list and when a tuple? This is closely related to a question that didn’t make the top 3 about what is an immutable and mutable object. We will get straight into it.

A tuple is an immutable object contrary to a list that is not. In plain English, a mutable object can be changed, while an immutable one cannot. So, a list has a variable size, i.e., can be extended and we can replace items to it while a tuple is immutable and thus we cannot.

This explanation is most of the times sufficient to pass the interview question but in this post, we are going to give you a few more details about it, to understand it a bit better.

Tuples and lists are both data structures used to store a collection of items and those items may be of any data type. Both tuples and lists access those items using an index. And this is where similarities end.

The syntax of tuples is different than lists, where tuples are created with ‘()’ and lists ‘[]’.

As mentioned tuples are immutable meaning if you have a tuple like this: my_tuple=('A','B','C') and you try overwriting the element at index ‘0’, e.g. my_tuple[0]='D', this would throw a type error: TypeError: ‘tuple’ object does not support item assignment. The same won’t happen if you are modifying a list.

In terms of memory efficiency, since tuples are immutable, bigger chunks of memory can be allocated to them, while smaller chunks are required for lists to accommodate the variability. Not to be confused here.

Bigger chunks in memory actually mean lower memory footprint as the overhead is low. So, tuples are more memory efficient but not a good idea to use if they are expected to be changing.

What is a list comprehension?

Anyone programmed with python is likely to have come across this notation, where you can do for loops inline like this [letters for letters in ‘Example’]. The will results in [‘E’, ‘x’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’].

List comprehension is an elegant way to create and define lists without writing too much code. Also, you can add conditions to list comprehensions like this: [ x for x in range(20) if x % 2 == 0] which returns only numbers that are divisible by 2 (including 0).

A key point here is that every list comprehension can be re-written using a normal for-loop. However, not every for-loop can be re-written in a list comprehension format. However, typically, list comprehensions are more efficient. For more analysis on this visit this link.

Sometimes a list comprehension is compared with a lambda function (not be confused with AWS Lambda) which is another question the frequently comes up in interviews.

Lambda function is really about doing a function operation as you would normally do but rather do it in one line.

For instance,

def my_func(x):

can be turned to this lambda function my_func=lambda x: print(x+1), where this can be called like a normal function e.g. my_func(2)

What is a decorator?

A decorator in Python is any callable Python object that is used to modify a function or a class without changing its underlying structure. Using a decorator is easy, writing one can be complicated to an inexperienced programmer.

Let’s see how we can create one.

Say we want to create a decorator to convert a string to lower case.

def lowercase_decorator(function): def wrapper():
func = function()
make_lowercase = func.lower()
return make_lowercase return wrapper

We defined a function that takes another function as an argument and performs the operation of lower case to the function passed.

def say_hello():
return ‘HELLO WORLD’decorate = lowercase_decorator(say_hello)

The output would be “hello world”.

In practice, however, we use the ‘@’ symbol as a syntactic sugar of what we did above, to reduce the code required to alter the behaviour of a function.

What we could have instead, after defining our decorator, is the following:

def say_hello():
return ‘HELLO WORLD’say_hello()

We expect the output to be the same i.e., “hello world”.


Created by

Alexandros Zenonos

Data Scientist







Related Articles