Python Interview Questions – Part 2

I have covered data science interview questions (Part 1 and Part 2) in my previous articles. In this article, I will cover important python interview questions. Let’s start then.

Q.1 Which sorting technique does sort() and sorted() functions of python use?

The Tim Sort algorithm is used in Python for sorting. Tim Sort is a stable sorting, and the worst case for the algorithm is O(N log N). It is derived from merge sort and insertion sort, hence known as a hybrid sorting algorithm. It performs well on various kinds of real-world data.

Q.2 Explain if python is a compiled language or an interpreted language?

Well, Python is not even a wholly compiled language and not ultimately interpreted language. When we execute our code, then compilation is done, and next, a byte code is generated. This byte code gets converted by the python virtual machine according to the basis of the underlying platform. 

.Q.3 What are functions in Python?

A function is simply a block of code that we write once and can use multiple times whenever needed in the program. A function is a self-contained statement that has a logical name, parameters list, and body. Functions are quite handy and make programming modular to do modular tasks. Python has many built-in functions to perform some tasks. Users can also define their function ser to create new functions as well.

Here are two types of functions in Python:

copy(), len(), count() etc. are the built-in functions present in Python.

A user defines User-defined Functions according to their needs and requirements. ,

Q.4 What is List Comprehension? Explain with an example.

List comprehension is a fantastic functionality provided in Python. It helps us to create a new list from an existing list in Python. A list comprehension comprises of an expression followed by for loop within square brackets. 

For Example:

 list_12= [i for i in range(1, 100)]

Here , list_ 12 is a new list created using a for loop . 

Q.5 What are Generators in Python?

The generator is a method that helps in implementing iterators in Python. It is a special function that yields expression in the function. It does not use __itr__ and next() function and reduces other overheads.

If any function has at least one yield statement, it is said to be a generator. The yield keyword stops the current execution of the code, save its state, and then continues from the same when needed.

Q.6 Python supports multiple Inheritance or not?

Yes, Python supports multiple inheritances, like C++. According to multiple inheritances, a class can be derived from more than one base class.

Q7. What is a self keyword in Python?

Ans: Self is defined as an instance of a class. It is explicitly included as the first parameter in Python. It helps to distinguish between the methods and attributes of a class having local variables.

The self variable present in the __init__ method points to the newly created object. 

Q.8 What is Pickling and Unpickling?

Pickle is a module in Python. It accepts any python’s object and converts it into a string representation. This string representation is then dumped into a file using the dump function; this process is known as pickling. 

On the opposite, the process of retrieving actual Python objects from the dumped string representation is known as unpickling. 

Q9. In python, how do we capitalize the first letter of a given string?

Ans: We use the capitalize() method in python to capitalize on a string’s first letter. It returns the original string if the string already consists of a capital letter at the starting. 

Q10. What does the zip() function do in Python.?

Ans. It takes multiple lists like list1, list2, etc., as input and converts them into a single list of tuples. It takes the corresponding elements of the lists that are passed as parameters to the zip function.

For example:

list1 = [‘E’, ‘B’,’D’] and list2 = [11,20,30]. 

zip(list1, list2) # It will result in a list of tuples say [(‘E’,11),(‘B’,20),(‘D’,30)]

Note: Zip stops generating tuples when the first list ends( it happens when the given lists are of different lengths). 

Q.11 Why do we use the help() and dir() function in Python?

Ans: The Python interpreter can access both Help() and dir() functions. And help in viewing a consolidated dump of built-in functions. 

Help() : The help() function calls the built-in Python help system.It is used to display the documentation string and helps in seeing the related modules, keywords, attributes, etc. 

Dir(): This function returns all properties and methods of the considered object without providing values.

Q12. Explain about __init__() in Python?

__intit__() function is equivalent to constructors. It is a reserve method in Python’s classes. This method is called automatically as soon as our class is instantiated. It allocates memory to the new object as soon as it is created. 

We use this method to initialize variables.

I hope you learned some important questions from this article.  

Thanks for reading.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked * Protection Status