DEV Community

Cover image for 50 Python Interview Questions and Answers

50 Python Interview Questions and Answers

Amanda Fawcett on May 27, 2020

This post was written by Cameron Wilson, and was originally published at Educative.io This post will cover interview questions on the Python prog...
Collapse
 
therenegadecoder profile image
Jeremy Grifski

Can you expand a bit on question 2? If I were to convert a list into a tuple, I would probably throw the list right at the tuple constructor:

>>> my_list = [1, 2, 3]
>>> my_tuple = tuple(my_list)
>>> my_tuple
(1, 2, 3)
Enter fullscreen mode Exit fullscreen mode

The answer provided seems to be answering a different question.

Collapse
 
muhimen123 profile image
Muhimen

I agree with that. I run the code, and this is the result.

>>> print(my_tuple)
(50, 'Eighty', 9)

But the real answer should be something like this:

(50, 'Twenty', 110, 'Fifty', 'Ten', 20, 10, 80, 'Eighty')
Collapse
 
musale profile image
Musale Martin

This is a really informative article. On question 13, is it possible to add a note that the behavior of range and xrange that is explained is based on Python 2. In Python 3, xrange is deprecated and the default behavior it had is moved to range.

Collapse
 
dwd profile image
Dave Cridland

There are so many things in here which are flat-out incorrect or warped beyond recognition it's really quite concerning.

People have already pointe dout Question 2 and Question 13, but there's plenty of other examples.

Question 11 manages to omit the key property of inheritance (that of substitution allowing specialisation). Question 12 makes me cry, especially following on from Question 11, which is literally about one form of polymorphism that it somehow fails to mention.

It's a particular shame because Python is rich in polymorphic techniques, from Row Polymorphism (aka Duck Typing) to Inclusion Polymorphism (aka subtyping and inheritance) to Polytypism (where we don't care about the type at all for much of the code).

If an interviewer accepts some of these answers as correct for anything but an entry-level Junior position, I'd worry about accepting the job.

Collapse
 
tenerhades profile image
Tener

A list consists of mutable objects. (Objects which can be changed after creation)

Not quite. The list itself is mutable, but objects referenced in that list can be either mutable or immutable, and each element of said list is considered a reference (and possibly garbage collected thereafter).

Collapse
 
hottabxp profile image
Sergey

Thanks! That's the best I've seen yet.