DEV Community

Cover image for 100+ Python Interview Questions
Mohit Chaprana
Mohit Chaprana

Posted on • Updated on • Originally published at inquisite.liveup.xyz

100+ Python Interview Questions

Python has turned the 3rd most in-demand programming language sought after by employers. Hence, we brought 100 essential Python interview questions to acquaint you with the skills and knowledge required to succeed in a job interview.
Let’s begin answering the fundamental-level Python interview questions.

Q-1: What is Python, what are the benefits of using it, and what do you understand of PEP 8?
Python is one of the most successful interpreted languages. When you write a Python script, it doesn’t need to get compiled before execution. Few other interpreted languages are PHP and Javascript.

Benefits of Python Programming
Python is a dynamic-typed language. It means that you don’t need to mention the data type of variables during their declaration. It allows us to set variables like var1=101 and var2 =” You are an engineer.” without any error.
Python supports object-orientated programming as you can define classes along with the composition and inheritance. It doesn’t use access specifiers like public or private).
Functions in Python are like first-class objects. It suggests you can assign them to variables, return from other methods and pass as arguments.
Developing using Python is quick but running it is often slower than compiled languages. Luckily, Python enables to include the “C” language extensions so you can optimize your scripts.
Python has several usages like web-based applications, test automation, data modeling, big data analytics and much more. Alternatively, you can utilize it as a “glue” layer to work with other languages.
PEP 8.
PEP 8 is the latest Python coding standard, a set of coding recommendations. It guides to deliver more readable Python code.

Q-2: What is the output of the following Python code fragment? Justify your answer.
def extendList(val, list=[]):
list.append(val)
return list

list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')

print "list1 = %s" % list1
print "list2 = %s" % list2
print "list3 = %s" % list3
The result of the above Python code snippet is:

list1 = [10, 'a']
list2 = [123]
list3 = [10, 'a']
You may erroneously expect list1 to be equal to [10] and list3 to match with [‘a’], thinking that the list argument will initialize to its default value of [] every time there is a call to the extendList.

However, the flow is like that a new list gets created once after the function is defined. And the same get used whenever someone calls the extendList method without a list argument. It works like this because the calculation of expressions (in default arguments) occurs at the time of function definition, not during its invocation.

The list1 and list3 are hence operating on the same default list, whereas list2 is running on a separate object that it has created on its own (bypassing an empty list as the value of the list parameter).

The definition of the extendList function can get changed in the following manner.

def extendList(val, list=None):
if list is None:
list = []
list.append(val)
return list
With this revised implementation, the output would be:

list1 = [10]
list2 = [123]
list3 = ['a']

For More questions like this, please visit this link:- https://myinquisitor.in/python-interview-questions/

Top comments (0)