Python Q&A
Python Q&A
It is very open, and there is no perfect answer. This question is principally used to
demonstrate your ability to summarize high-level concepts in a few seconds. Here’s a
possible short answer:
Python’s built-in functions are always available for use. These functions are available in
the official Python documentation.
Importing an external library is something you’ll do multiple times a day! Let’s see an
example using the pandas library:
import pandas as pd
dataframe = pd.DataFrame(dataset)
This will import the entire pandas library, allowing you to use all its functions and
classes, here, you use the DataFrame function. The alias pd to make it easier to
reference this dataframe in the code.
In Python, errors and exceptions are easily handled using the try-except block. This
allows you to catch and handle exceptions that occur during the execution of your code,
preventing them from causing your program to terminate unexpectedly.
except ZeroDivisionError:
except:
finally:
# Cleanup code
In the example above, the try block encloses code that may raise an exception. The
except block catches and handles exceptions raised within the try block. The type of
exception we want to catch can be specified; here, it’s ZeroDivisionError.
A generic except block can be used to catch any exception. The finally block is optional
and is used to execute cleanup code (such as closing files or releasing resources)
regardless of whether an exception occurred. The code in the finally block is always
executed, even if an exception is raised and not caught.
The mean is undoubtedly one of the most-used functions in data analysis. Also known
as the average, the mean is calculated by summing up all the values in a dataset and
dividing the sum by the total number of values.
If you aspire to a data analyst job, you should know how to calculate the mean in
Python. Python’s built-in functions allow us to do it in a very simple way. We can see
this in the following example:
data = [2, 3, 4, 5, 6]
Thanks to the built-in functions sum() and len(), we can calculate the sum of the values
of the dataset and the total number of values. Another great way to do it is by using the
function mean() from the module statistics:
import statistics
data = [2, 3, 4, 5, 6]
mean = statistics.mean(data)
The data analysis process involves several steps aimed at understanding, interpreting,
and deriving insights from data. The general data analysis process typically includes the
following main steps:
In data analysis, various file types are used to store and manipulate data. Your answer
to this question will indicate to the interviewer how much experience you have in
manipulating data. Some common file types include:
● CSV (Comma-Separated Values): CSV is a plain text format where each line
represents a row of data and columns are separated by commas (or another
character, such as a semi-colon). CSV files are widely used for storing tabular
data and are compatible with many software tools.
● Excel spreadsheets (.xlsx, .xls): Microsoft Excel files are commonly used for
storing tabular data, performing calculations, and creating visualizations.
● JSON (JavaScript Object Notation): JSON is a lightweight data-interchange
format that’s easy for humans to read and write and easy for machines to parse
and generate. JSON files store data in key-value pairs and are commonly used
for web APIs and configuration files.
String Questions
Question 8: How Do You Print a Variable in a String in Python?
There are many ways to print a variable in a Python string. The more experience with
Python you have, the more ways you will share with the interviewer.
There are three ways to print a variable: concatenation, string formatting, and f-strings.
language = “Python”
The syntax in the example above is called string concatenation: two strings are merged
into a single string using the + operator.
In this example, string formatting is used. The format() method is called on a string that
contains replacement fields delimited by braces {}. The replacement fields will be
replaced by the parameters passed through the format() function – here, language.
String slicing uses three optional parameters: start, end, and step. The syntax is as
follows:
String[start:end:step]
● start (optional): The index from which the slicing begins. It indicates the position
of the first character to be included in the slice. If omitted, slicing starts from the
beginning of the string (index 0).
● end (optional): The index up to which slicing occurs; however, it excludes the
character at this position. If omitted, slicing extends to the end of the string.
● step (optional): The step or increment value for selecting characters within the
specified range. If omitted, the default value is 1.
s = "hello world"
Pandas Question
Question 10: How Do You Read a CSV File with pandas?
This question seems very basic, but it demonstrates the candidate’s knowledge of the
pandas library.
import pandas as pd
df = pd.read_csv('dataset.csv')
With pandas, a CSV file can be easily loaded using the function read_csv(). The path of
the CSV file is indicated as a parameter.
Manipulating data in Python implies that you can choose the right data structure to
correctly store and perform operations on your data. If you have Python programming
experience, you should know the “fantastic four” data structures: list, tuple, dictionary,
and set. Name them and provide a short explanation of each one in your answer.
● Lists are widely used during data collection and data cleaning. They offer a
combination of flexibility, efficiency, and ease of use that makes them suitable for
storing and manipulating data.
● Tuples are lightweight data structures. Using tuples instead of lists for storing
data that will never be changed is a good practice; in terms of performance,
tuples are more efficient than lists.
TIP : Knowing the theory of data structures is excellent, but it’s not enough; you must
also be able to provide practical examples of how to use each data structure in a
real-life scenario.
This question will test your ability to write optimized code. Suppose you have a
dictionary named my_dict, and you want to retrieve the value associated with the
specific key “the_key”. If the dictionary does not contain the key, it will generate a fatal
error during execution. Therefore, you need to be sure that the key exists when you
access it.
A better way to answer the question is using the dictionary get() method, which allows
us to retrieve a value for a specific key in a dictionary. It accepts a default value in case
the key does not exist:
This is a trick question! Most candidates will answer int or string because those are the
types commonly used as a key dictionary, but this is only partially correct. To answer
this question correctly, you must understand the concept of hashable.
list_name.sort()
my_list = [9, 3, 5, 1]
my_list.sort()
print(my_list) #Print 1, 3, 5, 9
my_list.sort(reverse=True)
print(my_list) #Print 9, 5, 3, 1
List comprehension is a concise and powerful way to create lists in Python. It provides a
compact syntax for generating lists and applying operations or conditions to each
element.
… where:
numbers = [1, 2, 3, 4, 5]
Good luck with your interviews, and may your Python skills help you unlock
exciting opportunities in the world of data!