Python Interview Questions

To succeed in IT, not only do you have to master the art of undergoing interviews, but also the art of giving them. As a competent Python developer, you must be able to answer all interview questions with authority and lucidity. As a recruiter, you must be able to formulate a comprehensive list of questions, pertinent to the job description. Don’t expect a fresh grad to paint you a holistic picture of your prospective system’s architecture, and don’t gauge the wisdom of a system architect by asking them about syntax.

Python is a beautiful and sleek language, with stable, scalable, and renowned frameworks like Django, Flask, and Pyramid, used by a constantly growing community. Let’s take a look at some of the questions that will help you touch all of its (or the most important) bases during a recruiter interview:

1 – Briefly explain the execution process of a Python file?


Python is an interpreted language. The Python interpreter, behind the scenes, converts your source code into bytecode (.pyc files), and then executes them for you.

Red Flags:

  1. Not sure about the difference between compilation and interpretation.
  2. No knowledge of .pyc files and/or their difference from .py files.

2 – We know that Python is an object-oriented language, but does it have access specifiers?


No. Python is a modern object-oriented language which considers the use of access specifiers (like private and public in C++) as primitive and redundant.

Red Flags:

  1. Don’t know what access specifiers are.

3 – How does the ternary operator work in Python? Can you assign a value to a variable within a ternary operator?


The order of arguments in Python’s ternary operator is different from that in most languages, where you write the condition first, followed by the two expressions. In Python, you have to write it like: A if condition else B.

The execution is short-circuited in a way that only A is evaluated if the condition is true, and only B is executed, in case it’s false.

For the second part: No, assignments or statements (e.g. pass) can’t be used within a ternary operator.

Red Flags:

  1. “Yes” to the second part of the question.

4 – Can you explain how an exception can be caught in a Python program?


By surrounding your code between try and except keywords. You can either use Python’s built-in exceptions or define a new exception class. You can also raise an exception in a specific case using the raise keyword.

Red Flags:

  1. No mention of the try and except keywords.

5 – How would you test Python code?


Python has some cool modules to write testcases like unittest and Doctest which come bundled up with the Python standard library. You can also use tools like py.test, Hypothesis, mock, tox, and Unittest2.

Red Flags:

  1. Failure to mention at-least 2-3 names from the aforementioned.

6 – What are Python blocks and how do you define them?


Python source code is organized by defining blocks via indentation. Blocks can have blocks within them and can be part of bigger blocks themselves.

Red Flags:

  1. No emphasis on the significance of indentation.

7 – If you have to choose between a list, set, and a dictionary to store 10 million integers, what will you use? Bear in mind that you would later like to query the frequency of a number within the dataset.  


Since we have to cater for multiple occurrences of a number, we can’t choose a set, since it doesn’t store duplicates by design.

To find the frequency of a number within a list, you will have to iterate it completely, resulting in an O(n) complexity for searching, which is inefficient.

A dictionary, however, lets you store key-value pairs. You can store a number as the key and the amount of times it has been stored, as the value. This way, whenever you want to query the frequency, you can get the result in O(1). This makes a dictionary the best choice in this particular scenario.

Red Flags:

  1. Chooses set.
  2. Doesn’t explain the basic storing and querying processes associated with each datatype.

8 – Please list a few differences between lists and tuples in Python.


Python lists are mutable, whereas Tuples can’t be edited.

Tuples can be defined with a list of comma-separated integers with or without parenthesis. On the other hand, lists are defined via square brackets or using the list constructor.

Red Flags:

  1. No mention of the mutability difference.

9 – When should you use shallow copy instead of deep copy, and vice versa?


When you make a shallow copy, a new object gets created and it keeps the reference pointers of the original object. As the shallow copy process is not recursive, copies of the original child objects are not created. So basically, what you have is two objects that share the same set of elements. This means that any changes made in the copied object instantly get reflected in the original object.

In case of a deep copy however, a recursive copy process is carried out, and copies of all child objects are created. So, if you make a change in the copied object, they won’t get reflected in the original object.

When and why you should choose one over the other depends on the use-case, but it’s paramount that you understand the internal working of the two processes.

Red Flags:

  1. Can’t coherently explain how the memory for both the copies is managed.

10 – What are list comprehensions and why do we use them, if at all?


List comprehensions provide a great way to define lists where every element actually results from an operation being performed on every member of another list or a sequence. For example, if you want to store a list of squares of the first 10 prime numbers, you can use list comprehension to do so. However, list comprehensions shouldn’t be used for trivial iterations like printing values etc. Only use them if you want to build a resultant list which contains the updated values.

Red Flags:

  1. Failure to point out when and when not to use list comprehensions.

11 – What do you know about the global interpreter lock?


The Global Interpreter Lock, aka GIL, is a mutex that prevents multiple thread to execute Python bytecode simultaneously. This lock is necessary because the memory management in CPython isn’t thread-safe. This may prevent multi-threaded applications to use multiple CPUs and is therefore a bottleneck. However, not all the operations are concerned by the GIL, such as the IO operations or some computation done within libraries (e.g. numpy).

Red Flags:

  1. Never heard of it.

12 – Is None or == None, what should you use, and why? Is there a difference between == and is operators?


Yes, there is a difference between == and is. The former checks for equivalence and the latter checks whether the two specified objects are actually the same.

However, with that said, in the case of None, there is no difference between the two. Why? Because there is only one None object.

As a rule of thumb, using Is None is the way to go.

Red Flags:

  1. Fails to mention any of the three pointers above.

13 – What would the output of this code be?

def f(a,list=[]):
    for i in range(a):

[0, 1, 4] [1,2,3,0,1] [0, 1, 4, 0,1]

The key thing to remember here is that the list object is only created once, when the function is defined. In the first and third call, the list object is the same. However, during the second call, we used another object for the list argument.

Red Flags:

  1. Didn’t know that the default value of the argument “list” was created only once.

14 – In a function, what is the meaning of *args and **kwargs?


The single asterisk *args is used in a function definition when you are not sure how many arguments you might have to pass to a certain function.

The double asterisk **kwargs allows you to send keyworded arguments to a function, which can contain as many arguments as you please.

Red Flags:

  1. Doesn’t mention the difference between the two.

15 – What would the output of this code be?

list = ['1', ‘2', '3', '4', '5']
print (list[12:])

Answer:  The output would be [] and not an index error. This is because we are trying to retrieve the slice and only specifying the starting index.

Red Flags:

  1. Any errors.

16 – Please point out a few differences between Python 2.x and 3.x?


There are more changes/updates in Python 3 than can be mentioned, but here are the most commonly known ones:

  1. Change in the printing syntax.
  2. Change in the way integer division behaves. E.g. 3/2 = 1 in Python 2 as opposed to 1.5 in Python 3.
  3. Introduction of Unicode and two byte classes.
  4. A new method “__contains__” for range object, which helps fasten the lookup process.
  5. Enforcement of the new syntax to raise exceptions; i.e. you will get an error if you don’t surround the exception argument with parentheses.
  6. Deprecation of the .next() method. Only the next function remains.
  7. A TypeError is now rightly raised whenever two unorderable types are compared.

Red Flags:

  1. Failure to identify at-least 3,4 differences between the two versions (especially if you are interviewing for an experienced resource).

17 – What are some reasons to dislike Python?


This question is important because it makes the applicant think. There is no unanimity regarding the answers, but a cognizant, unbiased and experienced Python developer will always have something to say. If Python has ever really made a person pull their hair out, they would be able to vehemently explain what they don’t like (or understand that some people may not like) about the language to you.

Here are a few examples of topics that could be raised by the applicant:

  1. Having to deal with Python 2 compatibility whereas version 3 have been released more than 10 years ago.
  2. The usage of whitespace to define blocks.
  3. GIL.
  4. Dynamic typing.

Red Flags:

  1. “Nothing that comes to mind”.


Final Word

This list included a collection of easy, tricky, and difficult questions. Hence, it should allow you to assess the experience level of most candidates astutely. Again, don’t make the final call based on a person’s lack of familiarity with trivial stuff like certain language limitations or syntax, as they are not hard to learn (Python is easier than PHP!). What you need to figure out is whether or not they are capable of knowing right from wrong, when writing (or designing) code for your application. Don’t forget that one of the best ways to evaluate the candidate’s skills for your project is to perform a hands-on programming assessment.

How to Face an Interview as a Junior Software Engineer

Do phrases like “I don’t remember that”, “I’m not good at selling myself”, “the hardest thing for me is to talk about salary” sound familiar to you?

After many interviews with software developers, and especially the junior ones, I always keep the idea that with a few guidelines they could improve a lot in their interviews. And with that, either get the job they are looking for, or get in a better position to negotiate the salary.

It comes down to knowing how to tell your story, being able to talk for 10 seconds about everything you put on your CV, reading something about the company you’re going to and being clear about the salary you want (gross!).

And that’s exactly what I’m going to tell you in more detail below. A kind of guide based on many hours sitting across the table.

What does an interview look like?
The normal thing is that in the interviews they first tell you a little about the company, what it does, maybe its history, projects, products. Here you simply attend and keep what they tell you, but you don’t have to say anything to look interesting, because the most normal thing is that they are obvious :-P. I will tell you below: visit the company’s website before the interview, take an interest in what they do, their products, their projects, their sector… and this way you can effectively get into the conversation and show that you’ve done your homework (without being repellent, of course XD).

After the introduction, the normal thing is that they ask you to explain your experience, your trajectory, to talk about yourself.

Then comes the more complicated part, in which you are either asked questions about your CV (to explain something concrete) or general technical questions. In some companies they even do tests, programming tests, a whole exam. Although it’s the complicated part, it’s the one we’re most prepared for “in series”, so it’s almost the one I’m least worried about.

Finally, the economic aspect is dealt with, either because they ask you openly, or because they explain the levels and career plan of the company. And if they don’t address it, ask it, don’t leave the interview without knowing it.

Each company has its own process, some are very professional and have human resources people for this, they do aptitude tests, things like that. Then there are famous tests like Google/Facebook/Silycon-Valley in which you do a very complicated test, and there are even books to prepare it. I don’t share those techniques and they even seem like a lack of respect for the interviewee, but that’s another story. We at Codice go more to a personal interview, that the candidate tells us, and some technical question, and then if we want something more, we send an exercise to be solved at home, avoiding the pressure and stress of putting you on live programming.

The best thing: do several interviews, start with a company that doesn’t convince you much, maybe you’ll get a surprise, but in any case you’ll see what the process is about.

Prepare your story well… and tell it!
Now comes the part with the most room for improvement. After the introduction and telling you why the company is a world leader in its sector (all companies believe it in one way or another), they say “OK, now tell us a little about yourself”.

And here, with the recent graduates, there are anti-patrons who repeat themselves a lot: “no, well, I don’t have experience because I just finished the degree”, “buf! well, I don’t know very well, I’m with the project and…”, “I did an internship once that… but I don’t remember very well”.

In my opinion, try to avoid all this. The person interviewing you already knows you’re a rookie, that’s why you’re there, but he expects you to tell him something. Or better said: he probably doesn’t have very high expectations that you’re going to tell him something interesting, so you have a great opportunity to do it better than the other candidates.

The key? Prepare it. In the shower, while you’re walking somewhere, anywhere. But be prepared for what you’re going to say. Not to recite it, but to have some key points on how to explain your own story.

What if you don’t have work experience? As I was saying, the interviewer already knows that, so what he hopes is to see what you’ve done in your years of study: some interesting practice, or end-of-degree/career/what-you-call-now project? So count it.

Example: in second operating systems I made an application that was used to turn off all the computers in the classroom. It was a Linux daemon made in C and I learned the basics of sockets + daemons + running processes. I liked it very much because it was used for years in the lab.

I’m sure you have something similar.

Have you contributed to an open source project? Count it.

Have you done any work on your own or with a colleague for a company? Internships? Or your aunt’s/neighbor’s/friend’s company website? Then count it, but approach it professionally.


Ready to see CodinGame

Assessment in action?