Tags Projects About License

Python interview questions. Part II. Middle

Python interview questions. Part II. Middle

I'm starting a series of posts about my own database of interview questions. Of course, it's not complete and there are no answers, and some questions may seem strange or even silly, but I think they're good for finding the right Python candidate for the job.


Q: What is the output of -12 % 10?

Q: What is the output of -12 // 10?

Q: What is the sequence of call operators in the expression a * b * c?

Q: Why shouldn't you make the default arguments an empty list?

Q: What id() function in Python is for?

Q: What is the yield keyword used for in Python?

Q: What is an iterator in Python? Can you write an example?

Q: What is a generator in Python? How are they different from iterators?

Q: What is the difference between __iter__ and __next__?

Q: How do you create a dictionary which can preserve the order of pairs?

Q: What is a context manager? How are they different from try ... finally?

Q: Which functions must be overridden in a class in order for its instances to implement the context manager protocol?

Q: What is the synchronous code? What is asynchronous code? How to write asynchronous code?

Q: What is unittest module in Python? How to write tests in Python?

Q: What is type checking? Why Python is a strongly typed language? Do we have types in Python?

Q: How can you copy an object in Python? How to make a deep copy?

Q: How memory is managed in Python? Why garbage collector exists in Python?

Q: How the garbage collector works in Python? Describe Python's garbage collection mechanism in brief.

Q: How can you share global variables across modules? Is it a good idea to do that?

Q: What is the __slots__ attribute used in a class for?

Q: What are metaclasses in Python?

Q: How to create a class without a class statement?

Code involving questions

Q: How to check that one tuple contains all elements of another tuple?

Q: What will be the output of the following code?

>>> list = ['a', 'b', 'c', 'd', 'e']
>>> print(list[10:]) # []

Q: How can I reload a previously imported module? (we assume that the module is a file module.py)

Q: What will be the output of the following code?

>>> a = [[]]*3
>>> a[1].append(1)
>>> print(a)  # [[1], [1], [1]]

Q: What's wrong with the following code?

def foo():
    from .module import *

Q: The file is located in /usr/lib/python/person.py. The program is run with python /usr/lib/python/person.py. What will the output be?

class Person:
    def __init__(self, name):
        __name__ = name

    def getAge(self):

p = Person("John")

Q: Write a timeit decorator to measure the time of function execution.

Q: Write a decorator that will catch errors and repeat the function a maximum of 3 times (configurable).

Q: What's the output of the following code?

class parent:
    def __init__(self, param):
        self.v1 = param

class child:
    def __init__(self, param):
        self.v2 = param

obj = child(11)
print(obj.v1 + " " + obj.v2)

Q: Fix the following code to make it work.

class Repeater:
class RepeaterIterator:

repeater = Repeater("Hello")
for i in repeater:
    print(i)  # hello

Q: Write code to get unique values from a list of complex types (custom classes). Example: [A(1, "ab"), A(2, "ab"), A(2, "aa"), A(1, "ab)] -> [A(1, "ab"), A(2, "ab"), A(2, "aa")].

Q: We have the following code with the unknown function f(). In f(), we do not want to use a return, instead, we may want to use a generator.

for x in f(5):

The output looks like this:

0 1 8 27 64

Write a function f() so that we can have the output above.

Q: What's the output of the following code?

x = [[0], [1]]
print(len(' '.join(list(map(str, x)))))

Recommended books

Previous post
Buy me a coffee
Next post

More? Well, there you go:

Python interview questions. Part III. Senior

Python interview questions. Part I. Junior

Best blogs/podcasts to follow for Python developers