Python Interview Questions

Interviewing for Python can be quite intimidating.  If you are appearing for a technical round of interview for Python, here’s a list of 50 interview questions with answers to help you prepare. The first set of questions and answers are curated for freshers while the second set is designed for advanced users. These questions cover all the basic applications of Python and will showcase your expertise in the subject.

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?
Tuples and Lists are known as ‘sequence data types’ in Python. They consist of a number of elements separated by commas, the main difference being, Tuples are enclosed within parentheses whereas Lists are enclosed within square brackets.
Eg for Tuple :
Animals  = (‘cat’,’dog’,’cow’)
Eg for Lists :
Animals = 
2) What are the differences between Tuples and Lists?

They cannot be edited (immutable)They can be edited (mutable)
They are fasterThey are slower
Syntax – animals_tup = (‘cat’,’dog’,’cow’)Syntax – animals_list =

3) Name some important features on Python.

  • 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?

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

5) What does dictionary mean in Python?
Dictionary is a built in Data type in Python, it establishes or defines a direct relationship between keys and values. It consists of keys and their corresponding values. 
Dictionary are indexed by keys
Syntax : variable name = { ‘key1’ : ’value1’ , ’key2’ : ’value2’  ,…}
The following examples contain keys with corresponding values for countries and capitals.
Keys – India, Canada, China
Values – Delhi, Ottawa, Beijing

6) What is the output of print flavour if flavour = (‘vanilla’ , ‘strawberry’ , ‘chocolate’ , ‘blueberry’ , ‘apple’)
7) What is the output of print numbers + alphabets if numbers = (21,43,54,21) and alphabets = (‘ab’,’cd’,’ef’,’er’)

8) How do you convert a string to an integer in Python ?
It can be done by using the function int ( x ), 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?
String can be converted to a float in Python by Using the functions float(x).
10) How can you randomize items of a list in Pythons?
Consider the example
11) Write a sorting algorithm for a numerical dataset in Python.
12) What is the difference between range and xrange ?
Range and xrange basically perform the same function, i.e., they both provide a list of integers for you to use. The difference being range returns a python list object whereas xrange returns an xrange object.  
13) Explain split(), sub(), subn() methods or “re” module in Python?
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 a new string along with number of replacements.

14) What is the output of L is L =

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”)


  1. ]
  2. ()
  3. 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. a) print(alist*2)
  2. b) for i in alist :

     print i*2

  1. c)
  2. 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

  1. a) import numpy as np

          print np.sum(range(100))

  1. b) import numpy as np

          print np.sum

  1. c) import numpy as np

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

  1. d) import numpy as np

          print reduce
Ans: a,c
20) Let list1 = , what will be the output for list1 ?

  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.

  1. a) for i in dict:


  1. b) for val in dict.itervalues():

   print val
c)for val in dict.itervalues();
   print val

  1. d) none of the above

Ans: a,b
22) What is the maximum possible length of an identifier

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

Ans: 4
23) What will be the output of the following code
     print dic1

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

Ans: 1
24) What will be the output of the following code
     while i<10:
     print i #inside first loop
     print i #inside second loop
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()
Options :

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

ans: 1
Python for Data Science questions:

a) Data Manipulation

26) What is Lambda? Give examples of its use in different functions.
Lambda is small anonymous function which is mainly used in combination with the functions filter() and map().

27) How do we fill a particular missing value in a column with median of columns?
Answer:  df = df.fillna(df.median())
28)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.

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

30) How to make a set from a list? 

31) How to concatenate row and column wise?

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

33) How to store and load dataframes, Give examples.
34) How to select rows and columns in a dataframe?

35) How to create a series from a list?

b) Data Visualization

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

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

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

Barplot is a general plot that allows you to aggregate the categorical data based off some function, by default the mean
39) Why do we pass only the x-value in count plot?
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.
40) What is jointplot? Plot a scatter jointplot for the above sample file(tips.csv)
c) Numpy and Pandas

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

42) 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.

43) 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?

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

45) For the above dataframe(Refer: Q18), take only the first 3 columns and store it in a dataframe named “IrisSubset”
IrisSubset = pd.DataFrame(ds.iloc
46) How to print all the rows from 3rd column to 10th columns?
df = pd.read_csv(“filepath”)
47) How to set the index of the dataframe to be the first column?
df.index = ds
48) Write the iloc function to print all the rows of the 3rd column
49) Using iloc function print sepal length and sepal width for first 10 rows
50) 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




Please enter your comment!
Please enter your name here