Programming.

8 Important Python Interview Questions and Answers

Looking for a Python job? This article explains Python interview questions and answers them for you. It explains both some Python coding interview questions but also some non-technical questions. This article also contains Python interview questions and answers for testers which can form a source of inspiration.

Slightly related is this article which explains the difference between software engineers and software developers.

Question 1: Why Python?

Why would you use Python at all? There are several arguments to use Python over other programming languages. This really depends on the use case. If you are facing a Data Science related job, then you would really argue for using Python. Here are some reasons in favor of using Python as a programming language:

  • It is dynamically typed. In a dynamically typed programming language, you do not need to specify the types of your variables. Because of that, you are programming on a higher level then when using programming languages like Java. In Java you need to declare the types of your variables like in the following code snippet: int myInteger = 12;. In Python, you can just do myInteger = 12.
  • In machine learning projects, a lot of support is available for Python. There are large communities and libraries written in Python just for the sake of machine learning. One key ingredient why communities and libraries are depending heavily on Python, is the fact that it supports list comprehensions.

Question 2: What is the difference between List, Set and Dict?

The Python language has support for so called Python list comprehensions (read more about Python list comprehensions here). This allows you to transform mathematics into code in an elegant way. But first, let me explain the difference between lists, sets and dictionaries:

  • list is a data type used for storing items, in an ordered way. It is often denoted using rectangular brackets like [ and ]. An example of a list is [“hello”, “world”]. Since the ordering of the items is important, this is different from [“world”, “hello”]. Items may appear multiple times in a list. Therefore, [“hello”, “hello”] is a list containing the “hello” item twice.
  • set is just like a list a data type used for storing items. The difference is that a set is used for storing items in an unordered way. In Python, you can define a set using the set() command. Fun fact: you need a list to create a set. Since it is unordered, the sets set([“hello”, “world”]) and set([“world”, “hello”]) are the same! Items are unique in a set, so set([“hello”, “hello”]) is the same as set([“hello”]).
  • dictionary is a key-value datatype. Keys are unique. An example of a dictionary is the following: {“hello”: “world”}. The keys are unordered unless you use an Ordered Dictionary. Keys are looked up by a hash function to ensure fast lookup of the items.

Question 3: How does Python handle Compile-time and Run-time code checking?

Python supports compile-time code checking up to some amount. Most checks like checks for variable types are postponed until run-time code checking. Even if you use a custom function which is not defined, you will pass the compile-time checking. During run-time, Python raises exceptions if something is not right.

Question 4: Name a few well-known Python packages

There are a few extremely popular Python packages in the wild:

  • Pandas: A package providing flexible data structures for working with relational or labeled data.
  • NumPyA package which allows you to work with numerical based data structures like tensors.
  • Matplotlib: A 2D rendering engine written for Python. More information about Matplotlib can be found in this post.
  • Tensorflow: A package used for constructing computational graphs. Neural network and many machine learning models depend on these computational graphs. For example, you can build a GRU using Tensorflow. The implementation can be found here.

Question 5: How is memory managed in Python?

Python has build-in garbage collection, which recycles and free unused memory and gives it back to its private heap space.

All Python objects and data structures are allocated to resourced using a private heap in Python. The Python interpreter takes care of this private heap.

Question 6: Give an overview of the Data Types used in Python

Python has immutable and mutable data types. Immutable data types are data types that cannot be modified during runtime. Mutable data types can be modified during runtime. Python has the following build-in data types:

  • Lists (mutable)
  • Sets (mutable)
  • Dictionaries (mutable)
  • Tuples (immutable)
  • String (immutable)
  • Number (float, int) (immutable)

Question 7: What are lambda functions?

Lambda functions are functions without a name. One can for example define and use a lambda function as follows:

f = lambda x: x*2
y = f(2)
print(y) #2*2 = 4

Question 8: What is meant by *args and **kwargs

*args are ordered arguments used in a function. **kwargs are unordered arguments used in a function. Take a look at the following function:

def rectangle_area(width=1, height=1):
    return width * height

The arguments width and height are **kwargs, since these arguments are optional and can be swapped:

rectangle_area(height=2, width=3)
rectangle_area(width=3, height=2)

Sometimes, arguments are not optional. These arguments belong to *args:

import math

def circle_area(radius):
    return 2 * math.pi * radius

circle_area(10)

Here, the “radius” argument belongs to *args.

Bonus Questions

What Python IDE should you use?

See this great article which gives an overview of recent Python IDEs.

Conclusion (TL;DR)

There are many questions that can be asked about Python. It is explained how Python coding interview questions but also some non-technical questions should be answered. Even some advanced Python interview questions are highlighted. If you still have some unanswered questions, please let me know. In that case, I will add your question to the list. If you liked this blog post, then make sure to share it on social media!

Kevin Jacobs

Kevin Jacobs

Kevin Jacobs is a certified Data Scientist and blog writer for Data Blogger. He is passionate about any project that involves large amounts of data and statistical data analysis. Kevin can be reached using Twitter (@kmjjacobs), LinkedIn or via e-mail: mail@kevinjacobs.nl.