CodeSubmit Interview Series

15 Best Python Interview Questions and Answers (2023)

Want to hire a Python developer? You need the right questions to identify the best developer for your team. Here are the best Python interview questions for your technical recruitment processes.

Python interviews

Python is a popular language that developers enjoy thanks to its ease of use. But what interview questions should you ask during your interviews? 

We put together these questions to help you identify the best Python developers for your team. You can use them as they are or use them as inspiration for your own questions.

Want to use Python coding challenges in your interviews together with these interview questions? Read more about our Python coding tests

Beginner Python interview questions

These beginner Python questions help you understand how well a candidate knows the basics of this language. They’re ideal for more junior candidates.

1. What is Python? 

Answer:

Let’s start from the beginning. This question is really about the fundamentals of Python. There’s no textbook answer, so focus on listening to candidates’ answers to identify why they prefer Python and what their experience using it is. 

Python is a high-level, portable programming language. It has inbuilt automatic memory management, string, and threads and it’s object-based. It works well for Rapid Application Development and as a scripting or glue language to connect existing components together. 

Python is a simple language that’s relatively easy to learn. As a highly readable language, the cost of program maintenance is reduced. And because the language supports modules and packages, it encourages program modularity and code reuse. 

Many developers like the fact that editing, testing, and debugging is a fast process. Debugging Python is easy because a bug or bad input won’t cause a segmentation fault, but instead, the program raises an exception. 

2. Is Python a dynamically-typed language or statically-typed? 

Answer:

Python is a dynamically-typed language. In a weakly typed language, variables can be implicitly coerced to unrelated types, whereas in a strongly typed language they cannot, and an explicit conversion is required.

3. What are the data types of Python? 

Answer:

Python has six standard data types. Data types are the classification of data items and represent the kind of value that shows what operations can be performed on a particular data. Because everything is an object in Python, they’re actually classes. Variables are instances of the classes. 

The data types in Python are: 

  • Numbers 
  • String 
  • List 
  • Tuple
  • Set 
  • Dictionary

Advanced Python interview questions

These advanced Python interview questions are all about understanding candidates’ in-depth knowledge of Python. The questions are less about what Python is and more about how it works. 

4. How do I access a module written in Python from C?

Answer:

Understanding how Python works is a given for any Python developer. However, there’s no point in trying to get a word-for-word answer. Instead, look out for personal stories on how candidates have used specific features. 

You can get a pointer to the module object as follows:

module = PyImport_ImportModule("");

Note that it doesn't enter the module into any namespace -- it only ensures it has been initialized and is stored in sys. modules.

5. How do you reverse a list in Python? When is it useful? 

Answer:

You can reverse a list in Python with the reverse() or reversed() methods. They reverse the list, but don’t create a new list. 

For example, the method can be used to arrange lists. If there’s a list of names in reverse alphabetical order, the list would have to be reversed to be arranged in alphabetical order and that’s what can be done with the reverse functionality.

6. What does break and continue do in Python?

Answer:

This is another question to see how well candidates understand Python’s functionalities. 

Break and continue help control loops in Python. “Break” breaks the current loop from execution and transfers control to the next block. “Continue” makes a jump to the next iteration of the loop without exhausting it.

This can be helpful when you want to terminate the current iteration or the entire loop. Loops iterate over a block of code until the test expression is false, so break and continue make it possible to skip this. 

7. Can break and continue be used together?

Answer:

Follow-up question to the previous question. Break and continue can be used together. “Break” will stop the current loop from execution, while “continue” will cause the next iteration of the loop to run immediately.  

8. What will be the output of the code below? 

list = [‘alfa’, ‘bravo’, ‘charlie’, ‘delta’. ‘echo’]

print list[10:]

Answer:

By asking candidates to take a look at snippets of code, you get a feel for how they approach their work. Most things are googleable, so the point is not to get perfect answers, but rather to see how candidates reason. Remember, though, not to use “brainteasers” or “gotcha” questions, as they mostly just waste everyone’s time. 

The output will be []; it will not result in an IndexError. The code is trying to access the 10th object in the list which does not exist, so it will output [].

9. Explain generators vs iterators

Answer:

An iterator has the whole sequence in memory before it returns the first result. An iterator uses the "return". A generator calculates each result at the moment it is called for. The next result is unknown. A generator uses "yield".

So, in short:

  • You'd use a generator when processing a stream, or when memory consumption is important.
  • Generators are iterators, but iterators are not generators.

10. Does Python need to be compiled before it is run? 

Answer:

Compiled languages are based on code that can be executed directly on a computer’s processor. An interpreted language, on the other hand, isn’t in “machine code” before runtime. The translation happens at the same time as the program is executed. 

Python is an interpreted language so it does not need to be compiled. 

(Note: Candidates might not give a definition of compiled and interpreted languages. If you want to see how well they understand these concepts, follow up with a question about them.) 

11. What are *args, **kwargs?

This is a classic! Make sure to listen to the candidate explain the advantages and shortcomings of using them, especially when it comes to code maintainability.

Answer:

In cases when we don't know how many arguments will be passed to a function, like when we want to pass a list or a tuple of values, we use *args.

**kwargs takes keyword arguments when we don't know how many there will be:

The words args and kwargs are a convention, and we can use anything in their place.

The recommendation is to rely on **kwargs only for methods that have a reduced scope. Private methods (and by private in a context of Python. Methods starting by _) which are used in few places in the class are good candidates, for example. On the other hand, methods that are used by dozens of classes all over the code base are very bad candidates.

12. Is Python Call by Value or Call by Reference? How are arguments passed by value or by reference?

Answer:

Everything in Python is an object and all variables hold references to the objects. The reference values are according to the functions; as a result, you cannot change the value of the references. However, you can change the objects if it is mutable. A good follow-up question is #15 to dig deeper into immutability.

13. What is the GIL in Python?

Answer:

Python is not thread-safe when it comes to memory management. So, if you're running multiple threads, the GIL is a bottleneck; it only allows one thread to access memory at a time. If everything is happening in one thread, then you're fine. But if multi-threading then, when one thread accesses memory, the GIL blocks all the other threads.

This is a problem for multi-threaded python programs. It is not a problem for multi-processing python since each process has its own memory.

See if your candidate mentions "bottleneck", "multi-threading" and "memory management".

Solutions are to use multi-processing, use extensions written in C, or use other Python implementations like IronPython, or Cython.

14. What is the lambda function?

Answer:

A lambda function is an anonymous function that’s used when an anonymous function is required for a short period of time. This function can only have one statement, but it can have any number of parameters. Example: a = lambda x,y : x+y print(a(5,6.

15. Explain mutable vs immutable types?

Answer:

An immutable type cannot be changed. Examples are integers, floats, strings, tuples. A mutable type can change. Examples are lists, dictionaries, sets, classes.

For the follow-up, you want the candidate to explain how immutable types point to actual values, whereas immutable types are pointers to locations in memory so are subject to change.

16. BONUS: Tabs vs spaces

Answer:

Use this question to relax the candidate after an intensive interview session. There is no right answer here, both are syntactically completely fine. The most popular way of indenting is with spaces only and also part of the PEP-8 standard.

Over to you!

There you have it! Now you know what Python interview questions to ask on your next interview. 

Hiring a Flask devloper, specifically? Check out this list of the The Best Flask Interview Questions!

Ready to hold successful technical interviews? 

Try CodeSubmit for free (no credit card required).