Top 50 Interview Questions for Python

 

Here is a list of 50 Python interview questions (and answers), the first 25 are beginner or fresher level questions and the rest 25 are advanced level Python questions for Data Science specifically, that we feel are the most popular and cover all the basic applications of Python. Without wasting anymore time lets get directly to the questions.

Before going through the questions, here’s a quick video to help you refresh your memory on Python.

 

1) What are Tuples and Lists in Python?

Ans:

Tuples and Lists are known as ‘sequence data types’ in Python. They consists of a number of elements separated by commas, the main difference being, Tuples are enclosed within parentheses whereas Lists are enclosed within squared brackets.

Eg for Tuple :

Animals  = (‘cat’,’dog’,’cow’)

Eg for Lists :

Animals =  [‘cat’,’dog’,’cow’]

2) What are the differences between Tuples and Lists?

Ans:

TUPLES LISTS
They cannot be edited (immutable) They can be edited (mutable)
They are faster They are slower
Syntax – animals_tup = (‘cat’,’dog’,’cow’) Syntax – animals_list = [‘cat’,’dog’,’cow’]

3) Name some important features on Python.

Ans:

  • Python is an interpreted language and does not require to be compiled before it is run.
  • It supports structured programming as well as Object Oriented Programming
  • It does not require stating the type of variable while declaring them, it can be done at any point during the program without any error.
  • In Python, functions can be assigned to variables, returned from other functions and passed onto other functions.
  • It can also be used as a scripting language and can be integrated with C, C++ or Java.
  • It supports automatic garbage collection

 

4) What are the most popular data types that Python supports?

Ans:

  1. Numbers
  2. Strings
  3. Arrays
  4. Lists
  5. Tuples
  6. Dictionary

5) What does dictionary mean in Python?

Ans:

Dictionary is a built in Data type in Python, it establishes or defines a direct relationship between keys and values. It consists of a keys and their corresponding values. \

Dictionary are indexed by keys

Syntax : variable name = { ‘key1’ : ’value1’ , ’key2’ : ’value2’  ,…}

Example:

The following examples contains keys with corresponding values for countries and capitals.

Keys – India, Canada, China

Values – Delhi, Ottawa, Beijing

 

 

 

 

 

 

 

6) What is the output of print flavour [1:4] if flavour = (‘vanilla’ , ‘strawberry’ , ‘chocolate’ , ‘blueberry’ , ‘apple’)

ans:

 

 

 

7) What is the output of print numbers + alphabets if numbers = (21,43,54,21) and alphabets = (‘ab’,’cd’,’ef’,’er’)

Ans:

 

 

 

 

8) How do you convert a string to an integer in Python ?

Ans:

Using the function int ( x [,base]), where x is the string and the base is the corresponding position of x in the string

9) How do you convert a string to a float in Python?

Ans:

Using the functions float(x)

10) How can you randomize items of a list in Pythons?

Ans:

Consider the example

 

 

 

11) Write a sorting algorithm for a numerical dataset in Python.

Ans:

 

 

 

 

 

12) What is the difference between range and xrange ?

Ans:

Range and xrange basically perform the same function, that is they both provide a list of integers for you to use. The difference being range returns a python list object whereas xrange returns a xrange object.  

13) Explain split(), sub(), subn() methods or “re” module in Python?

Ans:

To modify strings, Pythons “re” module is used in 3 ways,

  1. split() : uses a regex pattern to split a string
  2. sub() : finds all substrings where the regex pattern matches and replaces it with a different string
  3. subn() : it is similar to sub() and returns new string along with number of replacements

 

14) What is the output of L [1:] is L = [4,7,2,7]

Ans:

 

 

 

 

Multiple choice questions :

 

15) Which of the following is an invalid statement?

  1. abc = 4,000,000
  2. a b c = 300 400 700
  3. a,b,c = 300, 400, 500
  4. a_b_c = 7,000,005

 

Ans : 2

16) What is the output of the below code?

  1. 1
  2. 7
  3. 9
  4. 3

 

Ans : 3

17) What will be the output of x in the following code

x.append (“R”)

Options

  1. [ ‘R’, [‘Data’ , ‘Science’, 4 , 5 , 6]]
  2. [‘Data’ , ‘Science’, ‘4’ , ‘5’ , ‘6’]
  3. ([‘Data’ , ‘Science’ , 4 , 5 , 6])
  4. None of the above

 

Ans: 1

18) What will be the syntax to multiply each element of the following list with 2 (choose all answers that are right)

alist = [1,2,3]

Options

a) print(alist*2)

b) for i in alist :

     print i*2

c) [i*2 for i a list]

d) map( lambda x:x*2, alist)

 

Ans: b,c,d

19) Write a code to find the sum of first 100 natural numbers using lambda function

Options

a) import numpy as np

          print np.sum(range(100))

b) import numpy as np

          print np.sum[range(100)]

c) import numpy as np

          print reduce(lambda x,y: x+y, range(100))

d) import numpy as np

          print reduce[lambda x,y: x+y, range(100)]

Ans: a,c

 

20) Let list1 = [23,34,54,56,1,32], what will be the output for list1[-2] ?

Options

  1. 54
  2. 56
  3. 1
  4. error

Ans: c

21) Consider bio = {‘Age’: 7, ‘Name’: ‘Manni’, ‘address’: ‘Downtown’}

     Write a code to print the values of the keys only.

Options

a) for i in dict:

   print(dict[i])

b) for val in dict.itervalues():

   print val

c)for val in dict.itervalues();

   print val

d) none of the above

Ans: a,b

22) What is the maximum possible length of an identifier

Options

  1. 33
  2. 45
  3. 90
  4. None of the above

Ans: 4

23) What will be be the output of the following code

     list1=[1,2,3]

     list2=[“A”,”B”,”C”,”D”]

     dic1=zip(list1,list2)

     print dic1

Options:

  1. [(1, ‘A’), (2, ‘B’), (3, ‘C’)]
  2. ((1, ‘A’), (2, ‘B’), (3, ‘C’))
  3. [(1, A), (2, B), (3, C)]
  4. error

Ans: 1

24) What will be the output of the following code

     i=5

     while i<10:

     print i #inside first loop

     if(i<8):

     print i #inside second loop

     i=i+1

Options :

  1. 5 6 7 8 9
  2. 5 5 6 6 7 7 8 8 9 9
  3. 5 5 6 6 7 7 8 8 9
  4. 5 6 7 8

Ans: 3

 

25) What is the output of len([2,5,6,9])

Options :

  1. 3
  2. 4
  3. 2
  4. Error

ans: 1

 

Python for Data Science questions:

a) Data Manipulation

1) What is Lambda and give examples of its use in different functions.

Answer:

Lambda is small anonymous function which is mainly used in combination with the functions filter() and map()


2) How do we fill a particular missing value in a column with median of columns?

Answer:  df = df.fillna(df.median())

              df

 

3)What does groupby method do and perform an example to show the mean for a particular column?

Answer: The groupby method allows you to group rows of data together and call aggregate functions

4) How do we apply multiple functions to columns in groups using group by method?

Answer:

5) How to make a set from a list? 

Answer:

6) How to concatenate row and column wise?

Answer:

7) Given that df1 and df2 is defined, perform some merging operations

Answer:

   

   8) How to store and load dataframes, Give examples.

Answer:

 

9) How to select a rows and columns in a dataframe?

Answer:

 

10) How to create a series from a list?

Answer:

b) Data Visualization

11) How to plot a simple matplotlib with necessary packages?

Answer:

12) Plot the distribution of a univariate set of observations for the sample file tips.csv (click here for tips.csv)

Answer:

     

13) What is bar plot? Plot a barplot for sex and total_bill as x and y respectively

Answer:

Barplot is a general plot that allows you to aggregate the categorical data based off some    function, by default the mean

14) Why do we pass only the x-value in count plot?

Answer:

It is same as barplot except the estimator is explicitly counting the number of occurrences  which is why we only pass the x value in count plot.

 

15) What is jointplot? Plot a scatter jointplot for the above sample file(tips.csv)

Answer:

c) Numpy and Pandas

16) Create an identity 2d-array or matrix (with ones across the diagonal).

Answer:

17) Create a 2d-array or matrix of order 3×3 with values = 9,8,7,6,5,4,3,2,1 arranged in the same order.

Answer:

18) Imagine this is the top 10 & bottom 10 samples from the dataframe. How to Set the index of the dataframe to be the first column?

Answer:

19) How to find the range of all columns in the above dataframe(Refer: Q18) ?

Answer:

20) For the above dataframe(Refer: Q18), take only first 3 columns and store it in a dataframe named “IrisSubset”

Answer:

IrisSubset = pd.DataFrame(ds.iloc[0:10,0:3]

 

21) How to print all the rows from 3rd column to 10th columns?

Answer:

df = pd.read_csv(“filepath”)

df.iloc[:,2:9]

 

22) How to set the index of the dataframe to be the first column?

Answer:

df.index = ds[‘Sepal Length (in cm)’]

 

23) Write the iloc function to print all the rows of the 3rd column

Answer:

df.iloc[:,2]

 

24) Using iloc function print sepal length and sepal width for first 10 rows

Answer:

df.iloc[1:10,0:1]

 

25) Write a function that accepts two numbers as input and prints them pass the sepal length and sepal width of 5th row and print the output

Answer: