17 Dsinterviewquestions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

17 Python Interview

Questions for Data Science


This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

1. What experience do you have


working with Python?

This is another question your future boss will


surely ask you. Be prepared to explain how you
learned Python and how long you’ve been
writing Python code.

Describe examples of your projects. If you’ve


created an application, show it and describe how
you built it.

Also, be prepared to define your expectations


and what you value in your job environment.
Know what your plans are and how you want to
develop as a Python professional.

To see more examples of Python questions and


answers, check out this list of JOB INTERVIEW
QUESTIONS.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

2. What is a Python dictionary?


A dictionary is one of the built-in data types in Python. It
defines an unordered mapping of unique keys to values.

Dictionaries are indexed by keys, and the values can be any


valid Python data type (even a user-defined class).
Notably, dictionaries are mutable, which means they can
be modified.

A dictionary is created with curly braces and indexed using


the square bracket notation. Here's an example:

my_dict = {'name': 'Hugh Jackman', 'age': 50,


'films': ['Logan', 'Deadpool 2', 'The Front Runner']}
my_dict['age']

Here, the keys include name, age, and films. As you can
see, the corresponding values can be of different data
types, including numbers, strings, and lists. Notice how the
value 50 is accessed via the corresponding key age.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

3. What is a Python library? Name


some common Python libraries.
A Python library is a collection of related modules, or
precompiled code that can be deployed in many different
programs.

Libraries make Python easier to use because you don’t


need to write the same code multiple times. Some of the
most popular Python libraries include:

TensorFlow - used in machine learning and neural


networks.
NumPy - functions and tools for working with arrays.
SciPy, a library that is based on NumPy.
Pandas - used in data manipulation and analysis.
Matplotlib - best for making data visualizations.
Keras, used for implementing neural networks.
SciKit-Learn, another data analysis library.
PyTorch, a machine learning framework.

To learn more, check out the article THE MOST POPULAR


PYTHON LIBRARIES.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

4. What is a function argument?


Give some examples.
An argument is a value that is sent to the function
when it is called. The function performs some action
on data and returns the result. In Python, there are 4
types of arguments:

Optional arguments (which may or may not be


explicitly defined) and default arguments, which
are used if an optional argument is not provided.

Keyword arguments (also called named


arguments), which follow an identifier (e.g. flavor
= “vanilla”).

Positional arguments, which must be included in


a specific order.

Arbitrary arguments (*args and **kwargs), used


when you have to pass an unknown number of
arguments into a function.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

5. Explain how the if statement


works in Python.
An if statement is used for decision-making
operations. It contains a block of code which will be
followed only when the "if" condition is true.

When the condition is false, either another optional


statement runs (the else statement) or the block of
code inside the "if" is skipped. For example:

def main():
SELECT
x,y =2,8id,
first_name,
if(x < y):
last_name,
st=”x is less than y”
FROM customer
print(st)
ORDER BY last_name DESC;
if __name__ == ”__main__”:
main()

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

6. Write a Python program that


checks how many capital letters are
there in a file.

with open("text.txt") as file:


SELECT id,
count = 0
first_name,
text = file.read()
last_name,
for i in text:
FROM customer
if i.isupper():
ORDER BY count
last_name
+= 1 DESC;
print(count)

Sometimes you will need to write this type of program


as a one-liner, or a shortened piece of code.

Check out these 10 PYTHON ONE-LINERS for some


inspiration

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

7. What are the data types used in


Python?
Python has the following built-in data types:

Number (float, integer)


String
Tuple
List
Set
Dictionary

Numbers, strings, and tuples are immutable


data types, meaning they cannot be modified
during runtime.

Lists, sets, and dictionaries are mutable,


which means they can be modified during
runtime.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

8. Explain the difference between


lists and tuples.

Both lists and tuples are made up of elements,


which are values of any Python data type.
However, these data types have a number of
differences:

Lists are mutable, while tuples are


immutable.

Lists are created with square brackets


(e.g., my_list = [a, b, c]), while tuples are
enclosed in parentheses (e.g., my_tuple
= (a, b, c)).

Lists are slower than tuples.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

9. What are lambda functions?


Lambda functions are anonymous functions in
Python. They're very helpful when you need to define
a function that's very short and consists of only one
expression. So, instead of formally defining the small
function with a specific name, body, and return
statement, you can write everything in one short line
of code using a lambda function.

Here's an example of how lambda functions are


defined and used:

(lambda x, y, z: (x+y) ** z)(3,2,2)

25

Here, an anonymous function takes the sum of the


arguments (x and y) to the power of the argument (z).
As you can see, the syntax of a lambda function is
much more concise than that of a standard function.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

10. Explain list comprehensions and


how they're used in Python.
List comprehensions provide a concise way to create
lists. A list is traditionally created using square brackets.
But with a list comprehension, these brackets contain an
expression followed by a for clause and then if clauses,
when necessary.

Evaluating the given expression in the context of these


for and if clauses produces a list. It's best explained with
an example:

old_list = [1, 0, -2, 4, -3]


new_list = [x**2 for x in old_list if x > 0]
print(new_list)

[1,16]

Here, we're creating a new list by taking the elements of


the old list to the power of 2, but only for the elements
that are strictly positive. The list comprehension allows
us to solve this task in just one line of code.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

11. What is a negative index, and


how is it used in Python?
A negative index is used in Python to index a list,
string, or any other container class in reverse order
(from the end). Thus, [-1] refers to the last element,
[-2] refers to the second-to-last element, and so on.

Here are two examples:

list = [2, 5, 4, 7, 5, 6, 9]
print SELECT
(list[-1]) id,
first_name,
9
last_name,
FROM customer
text = "I love data science"
print ORDER BY last_name DESC;
(text[-3])

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

12. What is pandas?

Pandas is a Python open-source library that


provides high-performance and flexible data
structures and data analysis tools that make
working with relational or labeled data both
easy and intuitive.

It has functions for analyzing, cleaning,


exploring, and manipulating data. For
example, it allows us to:

analyze big data and make conclusions


based on statistical theories.
clean messy data sets, and make them
readable and relevant.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

13. What is the default missing value


marker in pandas, and how can you
detect all missing values in a DataFrame?
In pandas, the default missing value marker is NaN.

You can detect all missing values in a DataFrame by


using the isna() function from the pandas library:

employees.isna()
employees.isna()

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

To explore the composition of a column, including


how many missing values it has, use the info()
function;

it shows the number of non-null entries in each


column together with the total number of entries in
that column (the difference, of course, is the number
of missing values!).

employees.info()
SELECT id,
RangeIndex: 100 entries, 0 to 99
first_name,
Data columns (totas 3 columns):
last_name,
ID 100 non-null int64
FROM customer
Department 92 non-null object
Age ORDER BY last_name
94 non-null float64 DESC;
dtypes: float64(1), int64(1), object(1)
memory usage: 2.4+ KB

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

14. Write Python code to select the


Department and Age columns from the
employees DataFrame.
You can select multiple columns from a DataFrame
by passing in their names to double square brackets:

employees[['Department', 'Age']]

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

15. Add a new column named Active to the


employees DataFrame with all 1s to indicate
each employee's active employment status.
You can create a new column with a particular value
for all entries by simply assigning this value to the
whole column:

employees['Active']
employees.isna() = 1

employees.head()

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

16. Write Python code to plot the


distribution of employees by age.
To plot the distribution of employees by age, you'll
simply need to create a histogram from the Age
column of the employees DataFrame. This can be
done by calling the hist() function on the selected
column:

employees[['Department', 'Age']]

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

17. What Python IDEs are the most


popular in Data Science?

Granted, you won't be asked this exact


question, but it's likely that your prospective
employer will still want to get a better sense
of how you work with Python and what
exposure you have to the language.

In that case, it's good to know that Jupyter


Notebook, PyCharm, and Spyder are all good
Python IDEs, but Jupyter Notebook is
arguably the best for data science.

Interested in exploring some other Python


IDEs for data science? Check out THIS
ARTICLE.

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
This material is prepared by LearnPython.com as a part of the
Python training program. Check out our Python courses.

Here are some additional helpful


articles:

Why Should Every Data Scientist Know


Python?

How to Practice Python Before a Job


Interview

Who Are Data Scientists and What Do


They Use Python For?

Do You Need a Master’s Degree to


Become a Data Scientist?

The Best Python Books for Data Science

Where Can I Find Sample Data Science


Projects to Practice Python?

LearnPython.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA
If you found the material worthy to be shared, please tag
LearnPython.com as a gesture of appreciation

Learn it all at LearnPython.com

LearnSQL.com is owned by Vertabelo SA


vertabelo.com | CC BY-NC-ND Vertabelo SA

You might also like