Top 15 Python Interview Questions for Right Preparation

Here is the compilation of the most important Python Interview Questions for various technical interviews. Please go through these questions as a part of revision before a python interview. These questions cover a variety of topics in python programming that can be most likely asked in a python interview. 

1. What is __init__ in python?

The _init_ method or initialiser method in python is a type of constructor used to initialize the attributes of a class. This is the most commonly asked question among Python Interview Questions. Without the  __init__ method, no argument can be passed when the class is called in any python program. Here is an example to illustrate the syntax for using the __init__ method in python.

  def __init__(self, x, y):
        self.x = x
        self.y = y

2. What is the difference between Tuple, List and array?

Tuple

Tuples gives us the easiest way to group some items that you are returning from a function or getting from a list etc. Tuples are immutable. 

List

List is another inbuilt data structure in Python. Lists are ordered and have a definite count. The elements in a list are indexed according to the sequence in which they were entered and the index always starts with 0. List is mutable. List may contain elements of different data types.

Array

Arrays are one of the basic linear data structures that are used to store and retrieve data sequentially. Arrays are similar to lists and python has many inbuilt functions such as append, pop, copy, count, insert, sort etc. for arrays. Arrays contain elements of a single data type.

3. What is the latest version of python and what are the new features?

To get the latest version for python, click here

4. What do you mean by Dictionary in Python?

Another frequently asked question among Python Interview Questions is Dictionary. A dictionary is used to store key-value pairs of data. ‘:’  is used to give a value to its key. Python provides the built-in function dict() method which is also used to create a dictionary. A dictionary is mostly unordered, changeable and indexed.

The following example will give you a better explanation of the same. 

Employee = {"Country": "Canada", "City": "Saskatoon", "Population":25000}    

Dict = dict([(1, 'Monday'), (2, 'Tuesday'),(3,'Wednesday')])   
#Dictionaries can also be created explicitly as in the next example:
thisdict = {
  "Airline": "Air France",
  "Hub": "Paris",
  "code": 'CDG'
}

5. What do you mean by Lambda function in python?

Lambda functions are very powerful functions that can have any number of arguments within a single expression. The expression is evaluated and then returned with the desired result by carrying out any number of calculations etc. as mentioned while declaring it. Lambda functions can be used wherever function objects are existent for any purpose in python programming. This can be better understood with the following example.

answer = lambda a: a * 10
print(answer(5))

6. What do you mean by pickling?

Pickling in Python is used for object serialisation so that it can be reused in future over a different network or a domain. Pickling is the process of converting a Python object hierarchy into a byte stream. It is also known as serialisation or simply flattening. The reverse operation is known as unpicking. In Python, predefined library ‘pickle’ can be imported to implement these operations.  

7. What is the use of range() and xrange() in Python?

range() and xrange() are methods used to get the specific amount of elements to store or perform some operation further on as required. To differentiate,

  • range() creates a list maximum with 9999999 elements. It is an expensive operation in python programming.
  • xrange() is a sequence object that evaluates slowly. It is much more optimised, it will only compute the next value when the need arises and does not create a list of all values in the list as range() does.

The advantage of the range() type is that it is faster if iterating over the same sequence multiple times while xrange() has to again create the integer object every time upon iteration.

8. What are modules in Python? List some inbuilt modules in python.

Modules in Python are simply Python files with a .py extension. The name of the module will be the name of the file. A Python module can have a set of functions, classes or variables defined and implemented. Some examples of inbuilt modules in python:

  • os
  • sys
  • math
  • random
  • data time
  • JSON

9. What are some of the data structures inbuilt in Python?

Some data structures inbuilt in Python are as follows:

10. What is the use of ‘self’ in Python?

Some major uses of self in python are indicated below:

  • self refers to the name variable of the entire class which is explicitly used every time we define a method.
  • self is used to represent the instance of any python class.

11. What is the difference between del and remove?

  • remove() in Python is used to remove the first matching value from the list/array by providing the value itself.
list=[10,20,30,40]
list.remove(30)
print(list)
  • del() in python is used to delete any variable by providing its index in a list of values from an array.
list = [1,2,3,4,5]
del(list[1])
print(list)

12. What is the difference between extend() and append()?

The basic difference between extend() and append() is:

  • extend() in Python is used to add elements to an existing array/list. 
  • append() is used to add an attribute to a list as a single element at the end of the existing list.

13. Explain Split() function in Python.

Syntax: string.split(separator, max)

Split() function is an inbuilt python function for string used in data manipulation which separates a given input string when it encounters a separator which has been specified before in the function as mentioned above. The separated texts are returned as a list. Example is given below for reference. Note that split() can also be used without its attributes.

message = "Welcome to CSVeda"
x = message.split()
print(x)

14. What is the use of __name__ variable in Python?

__name__ is a built-in variable in Python that stores the name of the current module/script being executed. If the current module is executing then the __name__ variable is assigned the value __main__ otherwise it simply contains the name of the module or script.

So, when a Python script is executing then its __name__ variable will always have the value __main__ and if that Python file is imported in any other Python script then the __name__ variable will have the name of the module. Let’s take an example to understand. Create a Python script with name test.py an add the following code in it:

def func1():
    print('Value of __name__:', __name__)
  
func1()

When we executed the test.py script, the value for the __name__ variable is set as __main__. This can be called in other Python script after importing the above script.

15. How can we get index of an element in array in Python?

With this operation, you can search for an item in an array based on its value. This method accepts only one argument, value. It is a non-destructive method, which means it does not affect the array values.

The syntax is

arrayName.index(value) 

Be First to Comment

Leave a Reply

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