Machine Learning Fundamentals with Python

Discover the machine learning fundamentals and explore how machine learning is changing the world. Join the ML revolution today! If you’re new to the discipline, this is an ideal place to start. You’ll cover the machine learning basics with Python, starting with supervised learning with the scikit-learn library. You’ll also learn how to cluster, transform, visualize, and extra insights from data using unsupervised learning and scipy. As you progress, you’ll explore linear classifiers for machine learning in Python, including logistics regression and support vector machines. You’ll finish the track by covering the fundamentals of neural networks and deep learning models using Keras. By the time you’re finished, you’ll understand the essential machine learning concepts and be able to apply the fundamentals of machine learning with Python.

Top Python Interview Questions & Answers

Essential Python interview questions with examples for job seekers, final-year students, and data professionals.
Nov 2022 · 22 min read

Python Interview Questions

Python is the most popular language in the tech industry. During the interview, you will be asked to solve the challenge using Python and explain complex Python functionality. To pass the technical and coding stage, you need a guide or mock questions to practice.

In this post, we have outlined the most common question asked during technical interviews. Practicing these questions can help data professionals, developers, and software engineers successfully pass the interview stage.

Basic Python Interview Questions

These are some of the questions you might encounter during an entry-level Python interview.

1. Can you differentiate between a List and a Tuple?

Lists and tuples are Python data structures. The list is dynamic and whereas the tuple has static characteristics. They both have various advantages and use cases.


The list is the mutable data type, consumes more memory, and it is better for element insertion and deletion. Furthermore, it has several building functions, and the implication of iterations is slower compared to Tuple.


a_list = ["Data", "Camp", "Tutorial"]


The Tuple is an immutable data type, and it is generally used for accessing the elements. It is faster and consumes less memory, but it lacks built-in methods.


a_tuple = ("Data", "Camp", "Tutorial")

Learn more in our Python Lists tutorial.

2. What is __init__() in Python?

It is known as a constructor in OOP terminology. It is used to initiate a state when you create a new object. For example, you can assign values to object properties or run the operations that are necessary when the object is created.

The __init__() method is reserved for Python classes, and it is called automatically when you create a new object.


We have created a `book_shop` class and added the constructor and `book()` function. The constructor will store the book title name and the `book()` function will print the book name.

To test our code we have initialized the `b` object with “Sandman” and executed the `book()` function.

class book_shop:

# constructor
def __init__(self, title):
self.title = title

# Sample method
def book(self):
print(‘The tile of the book is’, self.title)

b = book_shop(‘Sandman’)
# The tile of the book is Sandman

3. What is the difference between a mutable data type and an immutable data type?

The mutable Python data types can be modified, and they can change at runtime, for example, a List, Dictionary, and Set.

The immutable Python data types can not be changed or modified, and they remain unchanged during runtime, for example, a Numeric, String, and Tuple.

4. Explain List, Dictionary, and Tuple comprehension with an example.


List comprehension offers one-liner syntax to create a new list based on the values of the existing list. You can use a `for loop` to replicate the same thing, but it will require you to write multiple lines, and sometimes it can get complex.

List comprehension eases the creation of the list based on existing iterable.

my_list = [i for i in range(1, 10)]
# [1, 2, 3, 4, 5, 6, 7, 8, 9]


Similar to a List comprehension, you can create a dictionary based on an existing table with a single line of code. You need to enclose the operation with curly brackets `{}`.

my_dict = {i for i in range(1, 10)}
# {1, 2, 3, 4, 5, 6, 7, 8, 9}


It is a bit different for Tuples. You can create Tuple comprehension using round brackets `()`, but it will return a generator object, not a tuple comprehension.

You can run the loop to extract the elements or convert them to a list.

my_tuple = (i for i in range(1, 10))
# <generator object <genexpr> at 0x7fb91b151430>

You can learn more in our Python Tuples tutorial.

String segmentation

5. What is monkey patching in Python?

Monkey patching in Python is a dynamic technique that can change the behavior of the code at run-time. In short, you can modify a class or module at run-time.


Let’s learn monkey patching with an example.

  1. We have created a class `monkey` with a `patch()` function. We have also created a `monk_p` function outside the class.
  2. We will now replace the `patch` with the `monk_p` function by assigning `monkey.patch` to `monk_p`.
  3. In the end, we will test the modification by creating the object using the `monkey` class and running the `patch()` function.

Instead of displaying “patch() is being called”, it has displayed “monk_p() is being called”.

class monkey:
def patch(self):
print ("patch() is being called")
def monk_p(self):
print (“monk_p() is being called”)# replacing address of “patch” with “monk_p”
monkey.patch = monk_pobj = monkey()obj.patch()
# monk_p() is being called

6. What is the Python “with” statement designed for?

The `with` statement is used for exception handling to make code cleaner and simpler. It is generally used for the management of common resources like creating, editing, and saving a file.


Instead of writing multiple lines of open, try, finally, and close, you can create and write a text file using the `with` statement. It is simple.

# using with statement
with open('myfile.txt', 'w') as file:
file.write('DataCamp Black Friday Sale!!!')

7. Why use else in try/except construct in Python?

`try:` and `except:` are commonly known for exceptional handling in Python, so where does `else:` come in handy? `else:` will be triggered when no exception is raised.


Let’s learn more about `else:` with a couple of examples.

  1. On the first try, we entered 2 as the numerator and “d” as the denominator. Which is incorrect, and `except:` was triggered with “Invalid input!”.
  2. On the second try, we entered 2 as the numerator and 1 as the denominator and got the result 2. No exception was raised, so it triggered the `else:` printing the message “Division is successful.”
num1 = int(input('Enter Numerator: '))
num2 = int(input('Enter Denominator: '))
division = num1/num2
print(f'Result is: {division}')
print('Invalid input!')
print('Division is successful.')
## Try 1 ##
# Enter Numerator: 2
# Enter Denominator: d
# Invalid input!## Try 2 ##
# Enter Numerator: 2
# Enter Denominator: 1
# Result is: 2.0
# Division is successful.

Take the Python Fundamentals skill track to gain the foundational skills you need to become a Python programmer.

Python Data Science Interview Questions

For those focused more on data science applications of Python, these are some questions you may encounter.

8. What are the advantages of NumPy over regular Python lists?


Numpy arrays consume less memory.

For example, if you create a list and a Numpy array of a thousand elements. The list will consume 48K bytes, and the Numpy array will consume 8k bytes of memory.


Numpy arrays take less time to perform the operations on arrays than lists.

For example, if we are multiplying two lists and two Numpy arrays of 1 million elements together. It took 0.15 seconds for the list and 0.0059 seconds for the array to operate.


Numpy arrays are convenient to use as they offer simple array multiple, addition, and a lot more built-in functionality. Whereas Python lists are incapable of running basic operations.

9. What is the difference between merge, join and concatenate?


Merge two DataFrames named series objects using the unique column identifier.

It requires two DataFrame, a common column in both DataFrame, and “how” you want to join them together. You can left, right, outer, inner, and cross join two data DataFrames. By default, it is an inner join.

pd.merge(df1, df2, how='outer', on='Id')


Join the DataFrames using the unique index. It requires an optional `on` argument that can be a column or multiple column names. By default, the join function performs a left join.



Concatenate joins two or multiple DataFrames along a particular axis (rows or columns). It doesn’t require an `on` argument.

  • join(): it combines two DataFrames by index.
  • merge(): it combines two DataFrames by the column or columns you specify.
  • concat(): it combines two or more DataFrames vertically or horizontally.

10. How do you identify and deal with missing values?

Identifying missing values

We can identify missing values in the DataFrame by using the `isnull()` function and then applying `sum()`. `Isnull()` will return boolean values, and the sum will give you the number of missing values in each column.

In the example, we have created a dictionary of lists and converted it into a pandas DataFrame. After that, we used isnull().sum() to get the number of missing values in each column.

import pandas as pd
import numpy as np
# dictionary of lists
dict = {‘id’:[1, 4, np.nan, 9],
‘Age’: [30, 45, np.nan, np.nan],
‘Score’:[np.nan, 140, 180, 198]}# creating a DataFrame
df = pd.DataFrame(dict)df.isnull().sum()
# id 1
# Age 2
# Score 1

Dealing with missing values

There are various ways of dealing with missing values.

  1. Drop the entire row or the columns if it consists of missing values using `dropna()`. This method is not recommended, as you will lose important information.
  2. Fill the missing values with the constant, average, backward fill, and forward fill using the `fillna()` function.
  3. Replace missing values with a constant String, Integer, or Float using the `replace()` function.
  4. Fill in the missing values using an interpolation method.

Note: make sure you are working with a larger dataset while using the `dropna()` function.

# drop missing values
df.dropna(axis = 0, how ='any')
df.fillna(method =‘bfill’)#replace null values with -999
df.replace(to_replace = np.nan, value = 999)# Interpolate
df.interpolate(method =‘linear’, limit_direction =‘forward’)

pandas interpolate

Become a professional data scientist by taking the Data Scientist with Python career track. It includes 25 courses and six projects to help you learn all the fundamentals of data science with the help of Python libraries.

11. Which all Python libraries have you used for visualization?

Data visualization is the most important part of data analysis. You get to see your data in action, and it helps you find hidden patterns.

The most popular Python data visualization libraries are:

  1. Matplotlib
  2. Seaborn
  3. Plotly
  4. Bokeh

In Python, we generally use Matplotlib and seaborn to display all types of data visualization. With a few lines of code, you can use it to display scatter plot, line plot, box plot, bar chart, and many more.

For interactive and more complex applications, we use Plotly. You can use it to create colorful interactive graphs with a few lines of code. You can zoom, apply animation, and even add control functions. Plotly provides more than 40 unique types of charts, and we can even use them to create a web application or dashboard.

Bokeh is used for detailed graphics with a high level of interactivity across large datasets.