Nicole Simonek

View Original

Python Interview Questions and Answers

Show off your knowledge of basic Python foundations with these answers!

What are Python’s built-in data types?

  • Number (integers, floats, and complex)

  • String (sequence of characters)

  • List (mutable ordered collection of items)

  • Tuple (immutable ordered collection of items)

  • Dictionary (unordered collection of key-value pairs)

  • Set (unordered collection of unique items)

What is the difference between a ‘module’ and ‘package’?

A module is a single file of Python code, while a package is a collection of modules organized in a directory.

What is the difference between a ‘class’, ‘object’, and ‘instance’?

Classes are the template to create your objects, while an object is the thing being built based on the class template/prototype. In instance is a virtual copy of an object.

What is the difference between a ‘list’ and a ‘tuple’?

Lists are mutable (can be changed after being created) while tuples are immutable (cannot be changed after being created). The syntax for lists is [], while the syntax for tuples is (). With tuples being immutable, they have better performance.

What are the ‘mutable’ versus ‘immutable’ data types?

Mutable:

  • List

  • Dictionary

  • Set

Immutable:

  • Tuple

  • String

  • Number

What is the difference between a Pandas ‘series’ and ‘dataframe’?

A series is a one-dimensional array with a defined index. A dataframe is a two-dimensional array with rows and columns where the index is optional (though a dataframe can be two-dimensional with one column).

What is the difference between a ‘matrix’ and ‘array’?

A matrix is exclusively two-dimensional, while an array can be multi-dimensional (greater than or equal to one dimension). An array within an array (nested array) could create a matrix.

What is the difference between ‘==’ and ‘is’?

The ‘==’ (equality operator) checks if the values themselves are actually equal, while ‘is’ (identity operator) checks if the objects are the same.

How do you install a Python package?

You would install a package by typing “pip install package_name”; ‘pip’ stands for Python Installer Package.

What are some popular Python packages used for data visualization?

  • Matplotlib

  • Seaborn

  • Plotly

  • Altair

  • Bokeh

What are some popular Python packages used for machine learning?

  • Numpy

  • Scikit-learn/sklearn

  • Pytorch

  • Tensorflow

  • Scipy

  • Keras

What are some popular Python packages used for statistical analysis?

  • Numpy

  • Pandas

  • Matplotlib

  • Seaborn

  • Statsmodels

  • Scipy

What is the difference between a normal function and a ‘lambda’ function?

A normal function is named, while a lambda function is anonymous. A normal function can be reused, while a lambda function uses the ‘lambda’ keyword and can only be a single expression.

What is a Python ‘list comprehension’?

A list comprehension creates a list from an already existing list. They can also be used to map elements from the existing list to new values.

What is the Python string ‘split’ function?

The split function, split(), implies its definition as it splits a string into a list based on a given delimiter. You can also set the number of max splits (ex: words.split(“,”,5) or you could just use a delimiter — words.split(“,”).

What is a Python ‘with’ statement used for?

The with statement is used for exception handling, and also ensures that you do not leave resources open and running. It is often used with opening and writing to files, and ensures that they get closed after processing is completed.

What is the Python ‘append’ statement used for?

The append statement allows you to add an individual element to the end of a list, rather than having to alter the entire list.

What is ‘indexing’ in Python?

Indexing returns a specific element within a sequence by using what position it is or (as the name alludes) index number. If we were to count the index numbers in a given list, it would start at 0 then go 1, 2, 3, and so on (don’t forget about 0!).

Let’s say we have the following list:

list = [1, 2, 3, 4, 5]

And we want to get the index of ‘4’:

number = 4

We would write the following code:

list.index(number)

And it would return the index:

3

Why 3 and not 4? Because the index starts at 0, so 1 = 0 index, 2 = 1 index, 3 = 2 index, 4 = 3 index! You can also include a start and stop parameter, where the code could look like list.index(number, 2, 4).

You can also index by using brackets, such as list[0] (specifying the index as 0), where it would return 1 (1 in the list has an index of 0).

Python also allows you to do negative indexing, which would read starting from the end of the list. For example, list[-1] would return 4.


Interested in working with me? Schedule here!


If you enjoyed this blog post, I would greatly appreciate you taking a moment to browse my other blog posts (I write on lifestyle, beauty, travel, restaurants, working in tech, and cocktails + wine), subscribe, and/or make a donation. Donation proceeds go toward monthly Squarespace fees, PO box fees, website enhancements, ad campaigns, SEO tools, and time investment in addition to my full-time job. Thank you for your readership from the bottom of my heart! xx Nicole

See this form in the original post