A engineer by profession with experience in managing operations and technology. I love partying and ... View More
About Me
Movies
Action, Comedy
Interests
Football, Reading
Music
Rock, Classic , House
Friends
Adrian Cole
posted a blog.
Over the years, Python has become the most popular & widely adopted programing language for data science applications. Its user-friendly nature and lightweight implementation make it a great choice for data scientists. It can be easily used for data analysis where we need to integrate the results into web apps or to add mathematical models or codes for production.
So in order to succeed in interviews for data science roles it is important to have a clear idea about the kind of questions to expect. To help you breeze past your interview I have compiled a list of Python Data Science questions along with their model answers that you are most likely to face in your interview. Do let us know if it helped you in your interview by providing your feedback at the comment section below.
1. Write the output for the given Python code –
def multipliers ():
return [lambda x: i * x for i in range (4)]
print [m (2) for m in multipliers ()]
The output for the above code will be [6,6,6,6]. Due to late binding, the value of the variable ‘i’ is looked up when any of the functions returned by multipliers are called.
2. Can the lambda forms in Python contain statements?
No, the lambda forms in Python cannot contain statements as their syntax is restricted to single expressions and they are used for creating function objects which are returned at runtime.
3.Which library is used for plotting in Python language?
The answer to this question varies based on the requirements for plotting data.
Matplotlib is the library used for plotting in Python language, but it needs lot of fine-tuning to ensure that the plots look shiny. Data scientists prefer Seaborn to create statistically and aesthetically appealing meaningful plots.
4. How can you check if a data set or time series is Random?
We make use of the lag plot. If the lag plot for the given dataset does not show any structure then it is random.
5. What is pylab?
Pylab is a package that combines NumPy, SciPy and Matplotlib into a single namespace.
6. State the difference between tuples and lists in Python.
Tuples can be used a key in a dictionary to store notes on locations whereas a list can be used to store multiple locations. Lists are mutable whereas tuples are immutable which means they cannot be edited.
7. Name a few libraries in Python used for Data Analysis and Scientific computations.
NumPy, SciPy, Seaborn, Pandas, Matplotlib, SciKit are a few libraries in Python used for Data Analysis and Scientific computations.
8. Write the code to sort an array in NumPy by the (n-1)th column?
This can be achieved using argsort () function. Let us take an array X then to sort the (n-1)th column the code will be x[x [: n-2].argsort ()]
9. If you are to give the first and last names of employees, which data type in Python will you use to store them?
You can use a list that has first name and last name included in an element or use Dictionary.
10. Explain the usage of decorators.
A decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it. They are used to modify the code in classes and functions. With the help of decorators a piece of code can be executed before or after the execution of the original code.
11. What will be the output of the below code:
def foo (i= []):
i.append (1)
return i
foo ()
foo ()
The output for the above code will be-
[1]
[1, 1]
Argument to the function foo is evaluated only once when the function is defined. However, since it is a list, at every step the entire list is modified by appending a 1 to it.
12. Which tool in Python would you use to find bugs?
The tools to find bugs in Python are Pylint and Pychecker. Pylint is used to verify if a module satisfies all the coding standards. Pychecker is a static analysis tool that helps to find out bugs in the source code.
13. What is the difference between range () and xrange () functions in Python?
The range () function returns a list whereas the xrange () function returns an object that works like an iterator for generating numbers on demand.
14. How can you randomize the items of a list in place in Python?
We can use the Shuffle (lst) function for randomizing the items of a list in Python.
15. What is PEP8?
PEP8 is a set of coding guidelines in Python language that programmers can use to write readable code which makes it easy to use for other users.
16. What is monkey patching in Python?
Programmers in Python can modify or extend other codes during runtime by using the monkey patching technique. It comes in handy while testing but it is not a good practice to use it in production environment as debugging the code could become difficult.
17. What do you mean by list comprehension?
List comprehension is the process of creating a list while performing some operation on the data so that it can be accessed using an iterator.
18. Suppose the data is stored in HDFS format and you want to find how the data is structured. Which command would you use to find out the names of HDFS keys?
In this case we could use the following command
hf.keys()
Note: HDFS file has been loaded by h5py as hf.
19. What will be the output of the below code
word = ‘aeioubcdfg’
print word [:3] + word [3:]
The output for the above code will be: ‘aeioubcdfg’.
In string slicing when the indices of both the slices collide and a “+†operator is applied on the string it concatenates them.
20. How can you check whether a pandas dataframe is empty or not?
The attribute df.empty is used to check whether a pandas dataframe is empty or not.fun fact data
21. Which python library is used for Machine Learning?
SciKit-Learn is the Python library that can be used for Machine Learning.
22. You are given a list of N numbers. Create a single list comprehension in Python to create a new list that contains only those values which have even numbers from elements of the list at even indices. For instance if list[4] has an even value then it has to be included in the new output list because it has an even index but if list[5] has an even value it should not be included in the list because it is not at an even index.
[x for x in list [1::2] if x%2 == 0]
The above code will take all the numbers present at even indices and then discard the odd numbers.
23. What will be the output of the given code?
list= [‘a’,’e’,’i’,’o’,’u’]
print list [8:]
The output for the above code will be an empty list []. Most of the people might confuse the answer with an index error because the code is attempting to access a member in the list whose index exceeds the total number of members in the list. The reason being the code is trying to access the slice of a list at a starting index which is greater than the number of members in the list.
24. How would you import a decision tree classifier in sklearn?
from sklearn.tree import DecisionTreeClassifier
25. You want to read a website which has url as “www.abcd.orgâ€. How would you perform this task?
urllib2.urlopen(www.abcd.org) OR
requests.get(www.abcd.org)
I hope these questions & model answers will help you in your interview prep. Do let me know how your interview goes in the comments below!
If you’re looking to master the Python programming language, there is no substitute for an instructor-led classroom training course. Check out our portfolio of Python courses here.
This post first appeared here.
Be the first person to like this.