67% found this document useful (3 votes)
811 views

120 Python Interview Questions and Answers in 2021 - You Should Know

This document discusses 120 Python interview questions and answers that are important to know in 2021. It begins with an introduction explaining that the questions are divided into categories for beginners and advanced users, and cover basic Python applications. The questions then cover topics like Python features, keywords, literals, tuples, functions, NumPy arrays, Pandas, dataframes, Series, and groupby functions. Sample questions include how to concatenate tuples, initialize a NumPy array with zeros, what Pandas is used for, how to create a dataframe from lists or a dictionary. Finally, it mentions that an accompanying video provides more detailed explanations of the questions and answers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
67% found this document useful (3 votes)
811 views

120 Python Interview Questions and Answers in 2021 - You Should Know

This document discusses 120 Python interview questions and answers that are important to know in 2021. It begins with an introduction explaining that the questions are divided into categories for beginners and advanced users, and cover basic Python applications. The questions then cover topics like Python features, keywords, literals, tuples, functions, NumPy arrays, Pandas, dataframes, Series, and groupby functions. Sample questions include how to concatenate tuples, initialize a NumPy array with zeros, what Pandas is used for, how to create a dataframe from lists or a dictionary. Finally, it mentions that an accompanying video provides more detailed explanations of the questions and answers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Python Interview
EXPLORE PROGRAMS 
Questions and Answers
ARTIFICIAL INTELLIGENCE  DATA SCIENCE 
in BUSINESS
2021ANALYTICS 
By Great Learning Team - Mar 5, 2021

CLOUD COMPUTING 


MORE 

Interviewing for Python can be quite intimidating.  If you are appearing for a technical round
of interview for Python, here’s a list of the top 101 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. The questions are divided into groups such as
basic, intermediate, and advanced questions. 

Do check out our Free Course on Python Interview Questions on Great Learning Academy.
It covers all the basic and advanced level questions.

Python Interview Questions in 2021

S.No- Questions
1. #What are the key features of Python?
2. #What are Keywords in Python?
3. #What are Literals in Python and explain about different Literals?
4. #How can you concatenate two tuples?
5. #What are functions in Python?
6. #How can you initialize a 5*5 numpy array with only zeroes?
7. #What is Pandas?
8. #What are dataframes?
9 #What is a Pandas Series?
10. #What is Pandas groupby?

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 1/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Here is a video which talks about the Python Interview Questions and Answers in detail
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

1. What are the key features of Python?


CLOUD COMPUTING 

Python is one of the most popular programming languages used by data scientists and AIML 
MORE 
professionals. This popularity is due to the following key features of Python:

Python is easy to learn due to its clear syntax and readability


Python is easy to interpret, making debugging easy
Python is free and Open-source
It can be used across different languages
It is an object-oriented language which supports concepts of classes
It can be easily integrated with other languages like C++, Java and more

2. What are Keywords in Python?

Keywords in Python are reserved words which are used as identifiers, function name or
variable name. They help define the structure and syntax of the language. 

There are a total of 33 keywords in Python 3.7 which can change in the next version, i.e.,
Python 3.8. A list of all the keywords is provided below:

Keywords in Python

False class finally is return


None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass

break except

3. What are Literals in Python and explain about different Literals?

Literals in Python refer to the data that is given in a variable or constant. Python has various
kinds of literals including:

1. String Literals: It is a sequence of characters enclosed in codes. There can be single,


double and triple strings based on the number of quotes used. Character literals are single
characters surrounded by single or double-quotes. 
2. Numeric Literals: These are unchangeable kind and belong to three different types –
integer, float and complex.
3. Boolean Literals: They can have either of the two values- True or False which represents
‘1’ and ‘0’ respectively. 
4. Special Literals: Special literals are sued to classify fields that are not created. It is
represented by the value ‘none’.

4. How can you concatenate two tuples?


https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 2/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

  Solution ->
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

Let’s say we have two tuples like this ->


CLOUD COMPUTING 

tup1 = (1,”a”,True) 
MORE 

tup2 = (4,5,6)

Concatenation of tuples means that we are adding the elements of one tuple at the end of
another tuple.

Now, let’s go ahead and concatenate tuple2 with tuple1:

All you have to do is, use the ‘+’ operator between the two tuples and you’ll get the
concatenated result.

Similarly, let’s concatenate tuple1 with tuple2:

5. What are functions in Python?

Ans: Functions in Python refer to blocks that have organised, and reusable codes to perform
single, and related events. Functions are important to create better modularity for applications
which reuse high degree of coding. Python has a number of built-in functions like print().
However, it also allows you to create user-defined functions.

6. How can you initialize a 5*5 numpy array with only zeroes?

Solution ->

We will be using the .zeros() meethod

Use np.zeros() and pass in the dimensions inside it. Since, we want a 5*5 matrix, we will pass
(5,5) inside the .zeros() method.

This will be the output:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 3/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 
7. What is Pandas?

Pandas is an open source python library which has a very rich set of data structures for data
based operations. Pandas with it’s cool features fits in every role of data operation, whether it
be academics or solving complex business problems. Pandas can deal with a large variety of
files and is one of the most important tools to have a grip on.

8. What are dataframes?

A pandas dataframe is a data structure in pandas which is mutable. Pandas has support for
heterogeneous data which is arranged across two axes.( rows and columns).

Reading files into pandas:-

Import pandas as pd

df=p.read_csv(“mydata.csv”)

Here df is a pandas data frame. read_csv() is used to read a comma delimited file as a
dataframe in pandas.

9. What is a Pandas Series?

Series is a one dimensional pandas data structure which can data of almost any type. It
resembles an excel column. It supports multiple operations and is used for single dimensional
data operations.

Creating a series from data:

10. What is pandas groupby?

A pandas groupby is a feature supported by pandas which is used to split and group an
object.  Like the sql/mysql/oracle groupby it used to group data by classes, entities which can
be further used for aggregation. A dataframe can be grouped by one or more columns.
https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 4/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

11. How to create a dataframe from lists?

To create a dataframe from lists ,

1)create an empty dataframe

2)add lists as individuals columns to the list

12. How to create dataframe from a dictionary?

A dictionary can be directly passed as an argument to the DataFrame() function to create the
data frame.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 5/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

13. How to combine dataframes in pandas?

Two different data frames can be stacked either horizontally or vertically by the concat(),
append() and join() functions in pandas.

Concat works best when the dataframes have the same columns and can be used for
concatenation of data having similar fields and is basically vertical stacking of dataframes
into a single dataframe.

Append() is used for horizontal stacking of dataframes. If two tables(dataframes) are to be


merged together then this is the best concatenation function.

Join is used when we need to extract data from different dataframes which are having one or
more common columns. The stacking is horizontal in this case.

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

14. What kind of joins does pandas offer?

Pandas has a left join, inner join, right join and an outer join.

15. How to merge dataframes in pandas?

Merging depends on the type and fields of different dataframes being merged. If data is
having similar fields data is merged along axis 0 else they are merged along axis 1.

16. Give the below dataframe drop all rows having Nan.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 6/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

The dropna function can be used to do that.


EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

17. How to access the first five entries of a dataframe?

By using the head(5) function we can get the top five entries of a dataframe. By default
df.head() returns the top 5 rows. To get the top n rows df.head(n) will be used.

18. How to access the last five entries of a dataframe?

By using tail(5) function we can get the top five entries of a dataframe. By default df.tail()
returns the top 5 rows. To get the last n rows df.tail(n) will be used.

19. How to fetch a data entry from a pandas dataframe using a given
value in index?

To fetch a row from dataframe given index x, we can use loc.

Df.loc[10] where 10 is the value of the index.

20. What are comments and how can you add comments in Python?

Comments in Python refer to a piece of text intended for information. It is especially relevant
when more than one person works on a set of codes. It can be used to analyse code, leave
feedback, and debug it. There are two types of comments which includes:

1. Single-line comment
2. Multiple-line comment

Codes needed for adding comment

#Note –single line comment

“””Note

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 7/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Note

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 


Note”””—–multiline comment

CLOUD COMPUTING 
21. What is the difference between list and tuples in Python?

MORE 
Lists are mutable, but tuples are immutable.

22. What is a dictionary in Python? Give an example

A Python dictionary is a collection of items in no particular order. Python dictionaries are


written in curly brackets with keys and values. Dictionaries are optimised to retrieve value for
known keys.

Example

d={“a”:1,”b”:2}

23. Find out the mean, median and standard deviation of this numpy
array -> np.array([1,5,3,100,4,48])

<code>import numpy as np n1=np.array([10,20,30,40,50,60]) print(np.mean(n1)) print(np.m

24. What is a classifier?

A classifier is used to predict the class of any data point. Classifiers are special hypotheses
that are used to assign class labels to any particular data points. A classifier often uses
training data to understand the relation between input variables and the class. Classification is
a method used in supervised learning in Machine Learning.

25. In Python how do you convert a string into lowercase?

All the upper cases in a string can be converted into lowercase by using the method:
string.lower()

ex: string = ‘GREATLEARNING’ print(string.lower())


o/p: greatlearning

26. How do you get a list of all the keys in a dictionary?

One of the ways we can get a list of keys is by using: dict.keys()


This method returns all the available keys in the dictionary. dict = {1:a, 2:b, 3:c} dict.keys()

o/p: [1, 2, 3]

27. How can you capitalize the first letter of a string?

We can use the capitalize() function to capitalize the first character of a string. If the first
character is already in capital then it returns the original string.
https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 8/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Syntax: string_name.capitalize() ex: n = “greatlearning” print(n.capitalize())


EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 
o/p: Greatlearning

CLOUD COMPUTING 
28. How can you insert an element at a given index in Python?

MORE 
Python has an inbuilt function called the insert() function.

It can be used used to insert an element at a given index.

Syntax: list_name.insert(index, element)


ex: list = [ 0,1, 2, 3, 4, 5, 6, 7 ]
#insert 10 at 6th index
list.insert(6, 10)
o/p: [0,1,2,3,4,5,10,6,7]

29. How will you remove duplicate elements from a list?

There are various methods to remove duplicate elements from a list. But, the most common
one is, converting the list into a set by using the set() function and using the list() function to
convert it back to a list, if required. ex: list0 = [2, 6, 4, 7, 4, 6, 7, 2]

list1 = list(set(list0)) print (“The list without duplicates : ” + str(list1)) o/p: The list without
duplicates : [2, 4, 6, 7]

30. What is recursion?

Recursion is a function calling itself one or more times in it body. One very important
condition a recursive function should have to be used in a program is, it should terminate,
else there would be a problem of an infinite loop.

31. Explain Python List Comprehension

List comprehensions are used for transforming one list into another list. Elements can be
conditionally included in the new list and each element can be transformed as needed. It
consists of an expression leading a for clause, enclosed in brackets. for ex: list = [i for i in
range(1000)]

print list

32. What is the bytes() function?

The bytes() function returns a bytes object. It is used to convert objects into bytes objects, or
create empty bytes object of the specified size.

33. What are the different types of operators in Python?

Python has the following basic operators:

Arithmetic( Addition(+), Substraction(-), Multiplication(*), Division(/), Modulus(%) ),


Relational ( <, >, <=, >=, ==, !=, ),

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 9/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Assignment ( =. +=, -=, /=, *=, %= ),

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 


Logical ( and, or not ), Membership, Identity, and Bitwise Operators

CLOUD COMPUTING 
34. What is the ‘with statement’?

MORE 
“with” statement in python is used in exception handling. A file can be opened and closed
while executing a block of code, containing the “with” statement., without using the close()
function. It essentially makes the code much more easy to read.

35. What is a map() function in Python?

The map() function in Python is used for applying a function on all elements of a specified
iterable. It consists of two parameters, function and iterable. The function is taken as an
argument and then applied to all the elements of an iterable(passed as the second argument).
An object list is returned as a result.

def add(n):

return n + n number= (15, 25, 35, 45)

res= map(add, num)

print(list(res))

o/p: 30,50,70,90

36. What is __init__ in Python?

_init_ methodology is a reserved method in Python aka constructor in OOP. When an object
is created from a class and _init_ methodolgy is called to acess the class attributes.

37. What are the tools present to perform statics analysis?

The two static analysis tool used to find bugs in Python are: Pychecker and Pylint. Pychecker
detects bugs from the source code and warns about its style and complexity.While, Pylint
checks whether the module matches upto a coding standard.

38. What is the difference between tuple and dictionary?

One major difference between a tuple and a dictionary is that dictionary is mutable while a
tuple is not. Meaning the content of a dictionary can be changed without changing it’s
identity, but in tuple that’s not possible.

39. What is pass in Python?

Pass is a statentemen which does nothing when executed. In other words it is a Null
statement. This statement is not ignored by the interpreter, but the statement results in no
operation. It is used when you do not want any command to execute but a statement is
required.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 10/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

40. How can an object be copied in Python?


EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 
Not all objects can be copied in Python, but most can. We ca use the “=” operator to copy an
CLOUD COMPUTING 
obect to a variable.

MORE 
ex: var=copy.copy(obj)

41. How can a number be converted to a string?

The inbuilt function str() can be used to convert a nuber to a string.

42. What are module and package in Python?

Modules are the way to structure a program. Each Python program file is a module, importing
other attributes and objects. The folder of a program is a package of modules. A package can
have modules or subfolders.

43. What is object() function in Python?

In Python the object() function returns an empty object. New properties or methods cannot be
added to this object.

44. What is the difference between NumPy and SciPy?

NumPy stands for Numerical Python while SciPy stands for Scientific Python. NumPy is the
basic library for defining arrays and simple mathematica problems, while SciPy is used for
more complex problems like numerical integration and optimization and machine learning
and so on.

45. What does len() do?

len() is used to determine the length of a string, a list, an array, and so on. ex: str =
“greatlearning”

print(len(str))
o/p: 13

46. Define encapsulation in Python?

Encapsulation means binding the code and the data together. A Python class for example.

47. What is the type () in Python?

type() is a built-in method which either returns the type of the object or returns a new type
object based on the arguments passed.

ex: a = 100

type(a)

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 11/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

o/p: int
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

48. What is split() function used for?


CLOUD COMPUTING 

Split fuction is used to split a string into shorter string using defined seperatos. letters = (” A, 
MORE 
B, C”)

n = text.split(“,”)

print(n)

o/p: [‘A’, ‘B’, ‘C’ ]

49. What are the built-in types does python provide?

Ans. Python has following built-in data types:

Numbers: Python identifies three types of numbers:

1. Integer: All positive and negative numbers without a fractional part


2. Float: Any real number with floating-point representation
3. Complex numbers: A number with a real and imaginary component represented as x+yj. x
and y are floats and j is -1(square root of -1 called an imaginary number)

Boolean: The Boolean data type is a data type that has one of two possible values i.e. True or
False. Note that ‘T’ and ‘F’ are capital letters.

String: A string value is a collection of one or more characters put in single, double or triple
quotes.

List: A list object is an ordered collection of one or more data items which can be of different
types, put in square brackets. A list is mutable and thus can be modified, we can add, edit or
delete individual elements in a list.

Set: An unordered collection of unique objects enclosed in curly brackets

Frozen set: They are like a set but immutable, which means we cannot modify their values
once they are created.

Dictionary: A dictionary object is unordered in which there is a key associated with each
value and we can access each value through its key. A collection of such pairs is enclosed in
curly brackets. For example {‘First Name’ : ’Tom’ , ’last name’ : ’Hardy’} Note that Number
values, strings, and tuple are immutable while as List or Dictionary object are mutable.

50. What is docstring in Python?

Ans. Python docstrings are the string literals enclosed in triple quotes that appear right after
the definition of a function, method, class, or module. These are generally used to describe
the functionality of a particular function, method, class, or module. We can access these
docstrings using the __doc__ attribute. Here is an example:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 12/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

<code>def square(n): '''Takes in a number n, returns the square of n''' return n**2 print(s
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 
Ouput: Takes in a number n, returns the square of n

MORE 

Python Interview Questions for Experienced Professionals

1. How to create a new column in pandas by using values from other


columns?

We can perform column based mathematical operations on a pandas dataframe. Pandas


columns containing numeric values can be operated upon by operators.

2. What are the different functions that can be used by grouby in


pandas ?

grouby() in pandas can be used with multiple aggregate functions. Some of which are
sum(),mean(), count(),std().

Data is divided into groups based on categories and then the data in these individual groups
can be aggregated by the aforementioned functions.

3. How to select columns in pandas and add them to a new


dataframe? What if there are two columns with the same
name?
If df is dataframe in pandas df.columns gives the list of all columns. We can then form new
columns by selecting columns.

If there are two columns with the same name then both columns get copied to the new
dataframe.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 13/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

4. How to delete a column or group of columns in pandas?


Given the below dataframe drop column “col1”.

drop() function can be used to delete the columns from a dataframe. 

5. Given the following data frame drop rows having


column values as A.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 14/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

6. Given the below dataset find the highest paid player in


each college in each team.

7. Given the above dataset find the min max and average
salary of a player collegewise and teamwise.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 15/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

8. What is reindexing in pandas?


Reindexing is the process of re-assigning the index of a pandas dataframe.

9. What do you understand by lambda function? Create a


lambda function which will print the sum of all the
elements in this list -> [5, 8, 10, 20, 50, 100]

<code>from functools import reduce sequences = [5, 8, 10, 20, 50, 100] sum = reduce (lambd

10. What is vstack() in numpy? Give an example


Ans. vstack() is a function to align rows vertically. All rows must have same number of
elements.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 16/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

11. How do we interpret Python?


When a python program is written, it converts the source code written by the developer into
intermediate language, which is then coverted into machine language that needs to be
executed.

12. How to remove spaces from a string in Python?


Spaces can be removed from a string in python by using strip() or replace() functions. Strip()
function is used to remove the leading and trailing white spaces while the replace() function
is used to remove all the white spaces in the string:

string.replace(” “,””) ex1: str1= “great learning”

print (str.strip())

o/p: great learning

ex2: str2=”great learning”

print (str.replace(” “,””))

o/p: greatlearning

13. Explain the file processing modes that Python


supports.
There are three file processing modes in Python: read-only(r), write-only(w), read-write(rw)
and append (a). So, if you are opening a text file in say, read mode. The preceding modes
become: “rt” fot read-only, “wt” for write and so on. Similarly a binary file can be opened by
specifying “b” along with the file accessing flags (“r” ,”w”, “rw” and “a”) preceding it.

14. What is pickling and unpickling?


Pickling is the process of converthing a Python object hierarachy into a byte stream for
storing it into a database.It is also known as serialization. Unpickling is the reverse of
pickling. The byte stream is conveted back into obhect hierarchy.

15. How is memory managed in Python?

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 17/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Memory management in python comprises of a private heap containing all objects and data
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE 
stucture. The heap is managed by the interpreter and the programmer does not BUSINESS
have acessANALYTICS
to it 

at all. The Python memory manger does all the memory allocation. Moreover, there is an
CLOUD COMPUTING 
inbuilt garbage collector that recycles and frees memory for the heap space.

MORE 

16. What is unittest in Python?


Unittest is a unit testinf framework in Python. It supports sharing of setup and shutdown code
for tests, aggregation of tests into collections,test automation, and independence of the tests
from the reporting framework.

17. How do you delete a file in Python?


Files can be deleted in Python by using the command os.remove (filename) or
os.unlink(filename)

18. How do you create an empty class in Python?


To create an empty class we can use the pass command after the definition of the class object.
A pass is a statement in Python that does nothing.

19. What are Python decorators?


Ans. Decorators are functions that take another functions as argument to modify its behaviour
without changing the function itself. These are useful when we want to dynamically increase
the functionality of a function without changing it. Here is an example :

<code>def smart_divide(func): def inner(a, b): print("Dividing", a, "by", b) if b =

Here smart_divide is a decorator function that is used to add functionality to simple divide
function.

Take up a data science course and power ahead in your career today!

Python Interview Questions for Adavanced Levels

1. You have this covid-19 dataset below:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 18/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

From this dataset, how will you make a bar-plot for the top 5 states having maximum
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 
confirmed cases as of 17=07-2020?

sol:
CLOUD COMPUTING 


<code>#keeping only required columns df = df[[‘Date’,
MORE‘State/UnionTerritory’,’Cured’,’Dea

Code explanation:

We start off by taking only the required columns with this command:

df = df[[‘Date’, ‘State/UnionTerritory’,’Cured’,’Deaths’,’Confirmed’]]

Then, we go ahead and rename the columns:

df.columns = [‘date’, ‘state’,’cured’,’deaths’,’confirmed’]

After that, we extract only those records, where the date is equal to 17th July:

today = df[df.date == ‘2020-07-17’]

Then, we go ahead and select the top 5 states with maximum no. of covide cases:

max_confirmed_cases=today.sort_values(by=”confirmed”,ascending=False)

max_confirmed_cases

top_states_confirmed=max_confirmed_cases[0:5]

Finally, we go ahead and make a bar-plot with this:

sns.set(rc={‘figure.figsize’:(15,10)})

sns.barplot(x=”state”,y=”confirmed”,data=top_states_confirmed,hue=”state”)

plt.show()

Here, we are using seaborn library to make the bar-plot. “State” column is mapped onto the
x-axis and “confirmed” column is mapped onto the y-axis. The color of the bars is being
determined by the “state” column.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 19/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

2. From this covid-19 dataset:


EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

How can you make a bar-plot for the top-5 states with the most amount of deaths?

Sol:

<code>max_death_cases=today.sort_values(by=”deaths”,ascending=False) max_death_case

Code Explanation:

We start off by sorting our dataframe in descending order w.r.t the “deaths” column:

max_death_cases=today.sort_values(by=”deaths”,ascending=False)

Max_death_cases

Then, we go ahead and make the bar-plot with the help of seaborn library:

sns.set(rc={‘figure.figsize’:(15,10)})

sns.barplot(x=”state”,y=”deaths”,data=top_states_death,hue=”state”)

plt.show()

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 20/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Here, we are mapping “state” column onto the x-axis and “deaths” column onto the y-axis.

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 
3. From this covid-19 dataset: 
MORE 

How can you make a line plot indicating the confirmed cases with respect to date?

Sol:

<code>maha = df[df.state == ‘Maharashtra’] sns.set(rc={‘figure.figsize’:(15,10)}) sns.linep

Code Explanation:

We start off by extracting all the records where the state is equal to “Maharashtra”:

maha = df[df.state == ‘Maharashtra’]

Then, we go ahead and make a line-plot using seaborn library:

sns.set(rc={‘figure.figsize’:(15,10)})

sns.lineplot(x=”date”,y=”confirmed”,data=maha,color=”g”)

plt.show()

Here, we map the “date” column onto the x-axis and “confirmed” column onto y-axis.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 21/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 


4. On this “Maharashtra” dataset:
CLOUD COMPUTING 


MORE 

How will you implement a linear regression algorithm with “date” as independent variable
and “confirmed” as dependent variable. That is you have to predict the number of confirmed
cases w.r.t date.

Sol:

<code>from sklearn.model_selection import train_test_split maha[‘date’]=maha[‘date’].map

Code solution:

We will start off by converting the date to ordinal type:

from sklearn.model_selection import train_test_split

maha[‘date’]=maha[‘date’].map(dt.datetime.toordinal)

This is done because we cannot build the linear regression algorithm on top of the date
column.

Then, we go ahead and divide the dataset into train and test sets:

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)

Finally, we go ahead and build the model:

from sklearn.linear_model import LinearRegression

lr = LinearRegression()

lr.fit(np.array(x_train).reshape(-1,1),np.array(y_train).reshape(-1,1))

lr.predict(np.array([[737630]]))

5. On  this customer_churn dataset:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 22/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

Build a keras sequential model to find out how many customers will churn out on the
basis of tenure of customer?

Sol:

<code>from keras.models import Sequential from keras.layers import Dense model = Seque

Code explanation:

We will start off by importing the required libraries:

from keras.models import Sequential

from keras.layers import Dense

Then, we go ahead and build the structure of the sequential model:

model = Sequential()

model.add(Dense(12, input_dim=1, activation=’relu’))

model.add(Dense(8, activation=’relu’))

model.add(Dense(1, activation=’sigmoid’))

Finally, we will go ahead and predict the values:

model.compile(loss=’binary_crossentropy’, optimizer=’adam’, metrics=[‘accuracy’])

model.fit(x_train, y_train, epochs=150,validation_data=(x_test,y_test))

y_pred = model.predict_classes(x_test)

from sklearn.metrics import confusion_matrix

confusion_matrix(y_test,y_pred)

6. On this iris dataset:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 23/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

Build a decision tree classification model, where dependent variable is “Species” and
independent variable is “Sepal.Length”.

Sol:

<code>y = iris[[‘Species’]] x = iris[[‘Sepal.Length’]] from sklearn.model_selection import

(22+7+9)/(22+2+0+7+7+11+1+1+9)

Code explanation:

We start off by extracting the independent variable and dependent variable:

y = iris[[‘Species’]]

x = iris[[‘Sepal.Length’]]

Then, we go ahead and divide the data into train and test set:

from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.4)

After that, we go ahead and build the model:

from sklearn.tree import DecisionTreeClassifier

dtc = DecisionTreeClassifier()

dtc.fit(x_train,y_train)

y_pred=dtc.predict(x_test)

Finally, we build the confusion matrix:

from sklearn.metrics import confusion_matrix

confusion_matrix(y_test,y_pred)

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 24/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 
(22+7+9)/(22+2+0+7+7+11+1+1+9)

MORE 

7. On this iris dataset:

Build a decision tree regression model where the independent variable is “petal length” and
dependent variable is “Sepal length”.

Sol:

<code>x= iris[[‘Petal.Length’]] y = iris[[‘Sepal.Length’]] x_train,x_test,y_train,y_test=train

8. How will you scrape data from the website “cricbuzz”?

Sol:

<code>import sys import time from bs4 import BeautifulSoup import requests import pand

9. Write a user-defined function to implement central-limit


theorem. You have to implement central limit theorem on
this “insurance” dataset:

You also have to build two plots on “Sampling Distribution of bmi” and “Population
distribution of  bmi”.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 25/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Sol:
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

<code>df = pd.read_csv(‘insurance.csv’) series1 = df.charges series1.dtype def central_lim


CLOUD COMPUTING 


MORE 

Code Explanation:

We start off by importing the insurance.csv file with this command:

df = pd.read_csv(‘insurance.csv’)

Then we go ahead and define the central limit theorem method:

def central_limit_theorem(data,n_samples = 1000, sample_size = 500, min_value = 0,


max_value = 1338):

This method comprises of these parameters:

Data
N_samples
Sample_size
Min_value
Max_value

Inside this method, we import all the required libraries:

    import pandas as pd

    import numpy as np

    import matplotlib.pyplot as plt

    import seaborn as sns

Then, we go ahead and create the first sub-plot for “Sampling distribution of bmi”:

  plt.subplot(1,2,1)

    sns.distplot(c.Mean)

    plt.title(f”Sampling Distribution of bmi. \n \u03bc = {round(c.Mean.mean(), 3)} & SE =


{round(c.Mean.std(),3)}”)

    plt.xlabel(‘data’)

    plt.ylabel(‘freq’)

Finally, we create the sub-plot for “Population distribution of bmi”:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 26/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

 plt.subplot(1,2,2)
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

    sns.distplot(data)
CLOUD COMPUTING 

    plt.title(f”population Distribution of bmi. \n \u03bc = {round(data.mean(), 3)} & \u03C3 = 


MORE 
{round(data.std(),3)}”)

    plt.xlabel(‘data’)

    plt.ylabel(‘freq’)

    plt.show()

10.  Write code to perform sentiment analysis on amazon


reviews:
Sol:

<code>import pandas as pd import numpy as np import matplotlib.pyplot as plt from tenso

11. Implement a probability plot using numpy and


matplotlib:
sol:

<code>import numpy as np import pylab import scipy.stats as stats from matplotlib import

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 27/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

12. Implement multiple linear regression on this iris


EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 
dataset:

CLOUD COMPUTING 


MORE 

The independent variables should be “Sepal.Width”, “Petal.Length”, “Petal.Width”, while the


dependent variable should be “Sepal.Length”.

Sol:

<code>import pandas as pd iris = pd.read_csv(“iris.csv”) iris.head() x = iris[[‘Sepal.Width’

Code solution:

We start off by importing the required libraries:

import pandas as pd

iris = pd.read_csv(“iris.csv”)

iris.head()

Then, we will go ahead and extract the independent variables and dependent variable:

x = iris[[‘Sepal.Width’,’Petal.Length’,’Petal.Width’]]

y = iris[[‘Sepal.Length’]]

Following which, we divide the data into train and test sets:

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.35)

Then, we go ahead and build the model:

from sklearn.linear_model import LinearRegression

lr = LinearRegression()

lr.fit(x_train, y_train)
https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 28/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

y_pred = lr.predict(x_test)

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

Finally, we will find out the mean squared error:

CLOUD COMPUTING 

from sklearn.metrics import mean_squared_error 


MORE 

mean_squared_error(y_test, y_pred)

13. From this credit fraud dataset:

Find the percentage of transactions which are fraudulent and not fraudulent. Also build a
logistic regression model, to find out if the transaction is fraudulent or not.

Sol:

<code>nfcount=0 notFraud=data_df[‘Class’] for i in range(len(notFraud)): if notFraud[i]=

14.  Implement a simple CNN on the MNIST dataset using


Keras. Following which, also add in drop out layers.
Sol:

<code>from __future__ import absolute_import, division, print_function import numpy as n

15. Implement a popularity based recommendation system


on this movie lens dataset:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 29/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

<code>import os import numpy as np import pandas as pd ratings_data = pd.read_csv(“ra

16. Implement the naive bayes algorithm on top of the


diabetes dataset:

Sol:

<code>import numpy as np # linear algebra import pandas as pd # data processing, CSV file

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 30/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

# However we want to see correlation in graphical representation so below is function for that

<code>def plot_corr(df, size=11): corr = df.corr() fig, ax = plt.subplots(figsize=(size, s

<code>from sklearn.model_selection import train_test_split X = pdata.drop(‘class’,axis=1)

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 31/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

Python OOPS Interview Questions

1. What do you understand by object oriented


programming in Python?
Object oriented programming refers to the process of solving a problem by creating objects.
This approach takes into account two key factors of an object- attributes and behaviour.

2. How are classes created in Python? Give an example


class Node(object):

  def __init__(self):

    self.x=0

    self.y=0

Here Node is a class

3. What is inheritance in Object oriented programming?


Give an example of multiple inheritance.
Inheritance is one of the core concepts of object-oriented programming. It is a process of
deriving a class from a different class and form a hierarchy of classes that share the same
attributes and methods. It is generally used for deriving different kinds of exceptions, create
custom logic for existing frameworks and even map domain models for database.

Example

class Node(object):

  def __init__(self):

    self.x=0

    self.y=0

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 32/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Here class Node inherits from the object class.


EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

Wish to upskill? Take up a data science course and learn now!


CLOUD COMPUTING 


4. What is multi-level inheritance? MORE
Give an example for
multi-level inheritance?

<code>If class A inherits from B and C inherits from A it’s called multilevel inheritance. clas

Python Programs for Interview

1. How can you find the minimum and maximum values


present in a tuple?
 Solution ->

We can use the min() function on top of the tuple to find out the minimum value present in
the tuple:

We see that the minimum value present in the tuple is 1.

Analogous to the min() function is the max() function, which will help us to find out the
maximum value present in the tuple:

We see that the maximum value present in the tuple is 5

2. If you have a list like this -> [1,”a”,2,”b”,3,”c”]. How


can you access the 2nd, 4th and 5th elements from this list?
Solution ->

We will start off by creating a tuple which will comprise of the indices of elements which we
want to access:

Then, we will use a for loop to go through the index values and print them out:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 33/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


Below is the entire code for the process: MORE 

3. If you have a list like this -> [“sparta”,True,3+4j,False].


How would you reverse the elements of this list?
Solution ->

We can use  the reverse() function on the list:

4. If you have dictionary like this – > fruit=


{“Apple”:10,”Orange”:20,”Banana”:30,”Guava”:40}.
How would you update the value of ‘Apple’ from 10 to
100?
Solution ->

 This is how you can do it:

Give in the name of the key inside the parenthesis and assign it a new value.

5. If you have two sets like this -> s1 = {1,2,3,4,5,6}, s2 =


{5,6,7,8,9}. How would you find the common elements in
these sets.
Solution ->

You can use the intersection() function to find the common elements between the two sets:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 34/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


We see that the common elements between the two sets MORE  6.
are 5 &

6. Write a program to print out the 2-table using while


loop.
Solution ->

Below is the code to print out the 2-table:

We start off by initializing two variables ‘i’ and ‘n’. ‘i’ is initialized to 1 and ‘n’ is initialized
to ‘2’.

Inside the while loop, since the ‘i’ value goes from 1 to 10, the loop iterates 10 times.

Initially n*i is equal to 2*1, and we print out the value.

Then, ‘i’ value is incremented and n*i becomes 2*2. We go ahead and print it out.

This process goes on until i value becomes 10.

7. Write a function, which will take in a value and print


out if it is even or odd.
Solution ->

The below code will do the job:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 35/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

Here, we start off by creating a method, with the name ‘even_odd()’. This function takes a
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE 
single parameter and prints out if the number taken is even DATA SCIENCE
or odd.  BUSINESS ANALYTICS 

CLOUD COMPUTING 
Now, let’s invoke the function:

MORE 

We see that, when 5 is passed as a parameter into the function, we get the output -> ‘5 is
odd’.

8. Write a python program to print the factorial of a


number.
Solution ->

Below is the code to print the factorial of a number:

We start off by taking an input which is stored in ‘num’. Then, we check if ‘num’ is less than
zero and if it is actually less than 0, we print out ‘Sorry, factorial does not exist for negative
numbers’.

After that, we check,if ‘num’ is equal to zero, and it that’s the case, we print out ‘The
factorial of 0 is 1’.

On the other hand, if ‘num’ is greater than 1, we enter the for loop and calculate the factorial
of the number.

9. Write a python program to check if the number given is


a palindrome or not
Solution ->

Below is the code to Check whether the given number is palindrome or not:

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 36/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

We will start off by taking an input and store it in ‘n’ and make a duplicate of it in ‘temp’. We
will also initialize another variable ‘rev’ to 0. 

Then, we will enter a while loop which will go on until ‘n’ becomes 0. 

Inside the loop, we will start off by dividing ‘n’ with 10 and then store the remainder in ‘dig’.

Then, we will multiply ‘rev’ with 10 and then add ‘dig’ to it. This result will be stored back
in ‘rev’.

Going ahead, we will divide ‘n’ by 10 and store the result back in ‘n’

Once the for loop ends, we will compare the values of ‘rev’ and ‘temp’. If they are equal, we
will print ‘The number is a palindrome’, else we will print ‘The number isn’t a palindrome’.

10. Write a python program to print the following pattern


->
1

22

333

4444

55555

Solution ->

Below is the code to print this pattern:

We are solving the problem with the help of nested for loop. We will have an outer for loop,
which goes from 1 to 5. Then, we have an inner for loop, which would print the respective

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 37/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

numbers.
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

11.  Pattern
CLOUD questions.
COMPUTING  Print the following pattern

#
MORE 

# #

# # #

# # # #

#####

Solution –>

<code>def pattern_1(num): # outer loop handles the number of rows # inner loop h

12. Print the following pattern


  # 

      # # 

    # # # 

  # # # #

#####

Solution –>

<code> Code: def pattern_2(num): # define the number of spaces k = 2*num -

13. Print the following pattern:


0

01

012

0 1 2 3

01234

Solution –>

<code>Code: def pattern_3(num): # initialising starting number number = 1 #

14. Print the following pattern:


1

23

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 38/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

456

7 8EXPLORE
9 10
PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

11 12 13 14 15
CLOUD COMPUTING 

Solution –>

MORE 

<code>Code: def pattern_4(num): # initialising starting number number = 1 #

15. Print the following pattern:


A

BB

CCC

DDDD

Solution –>

<code>def pattern_5(num): # initializing value of A as 65 # ASCII value equivalent

16. Print the following pattern:


A

BC

DEF
GHIJ

KLMNO

PQRSTU

Solution –>

<code>def pattern_6(num): # initializing value equivalent to 'A' in ASCII # ASCII va

17. Print the following pattern


  #

    # # 

   # # # 

  # # # # 

 # # # # #

Solution –>

<code>Code: def pattern 7(num): # number of spaces is a function of the input num
https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 39/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know
p _ ( ) p p

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

18. Given
CLOUD the below dataframes form a single dataframe
COMPUTING

by vertical stacking. MORE



We use the pd.concat and axis as 0 to stack them horizontally.

19. Given the below dataframes stack them horizontally to


form a single data frame.

We use the pd.concat and axis as 0 to stack them horizontally.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 40/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 

CLOUD COMPUTING 


MORE 

20. If you have a dictionary like this -> d1=


{“k1″:10,”k2″:20,”k3”:30}. How would you increment
values of all the keys ?

<code>d1={"k1":10,"k2":20,"k3":30} for i in d1.keys(): d1[i]=d1[i]+1</code>

21. How can you get a random number in python?


Ans. To generate a random, we use a random module of python. Here are some examples To
generate a floating-point number from 0-1

<code>import random n = random.random() print(n) To generate a integer between a certain

Great Learning offers extensive courses on Artificial Intelligence and Machine Learning.
Upskilling in this domain can land you the job of your dreams.

Python Interview related FAQs


Ques 1. How do you stand out in a Python coding interview?

Now that you’re ready for a Python Interview in terms of technical skills, you must be
wondering how to stand out from the crowd so that you’re the selected candidate. You must
be able to show that you can write clean production codes and have knowledge about the
libraries and tools required. If you’ve worked on any prior projects, then showcasing these
projects in your interview will also help you stand out from the rest of the crowd.

Ques 2. How do I prepare for a Python interview?

To prepare for a Python Interview, you must know syntax, key-words, functions and classes,
data types, basic coding, and exception handling. Having basic knowledge regarding all the
libraries, IDE’s used and reading blogs related to Python Tutorial’s will help you going

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 41/42
6/15/2021 120 Python Interview Questions and Answers in 2021 - You Should Know

forward. Showcase your example projects, brush up your basic skills about algorithms, data
EXPLORE PROGRAMS  ARTIFICIAL INTELLIGENCE  DATA SCIENCE  BUSINESS ANALYTICS 
structures. This will help you stay prepared.

CLOUD COMPUTING 
Ques 3. Are Python coding interviews very difficult?

MORE 
The difficulty level of a Python Interview will vary depending on the role you are applying
for, the company, their requirements, and your skill and knowledge/work experience. If
you’re a beginner in the field and are not yet confident about your coding ability, you may
feel that the interview is difficult. Being prepared and knowing what type of questions to
expect will help you prepare well and ace the interview.

Ques 4. How do I pass the Python coding interview?

Having adequate knowledge regarding Object Relational Mapper (ORM) libraries, Django or
Flask, unit testing and debugging skills, fundamental design principles behind a scalable
application, Python packages such as NumPy, Scikit learn are extremely important for you to
clear a coding interview. You can showcase your previous work experience or coding ability
through projects, this acts as an added advantage.

Also Read: How to build a Python Developers Resume

Ques 5. Which courses or certifications can help boost knowledge in Python?

If you wish to upskill, taking up a certificate course will help you gain the required
knowledge. You can take up a python programming course and kick-start your career in
Python.

Great Learning Team


Great Learning's Blog covers the latest developments and innovations in technology that can be leveraged to build rewarding
careers. You'll find career guides, tech tutorials and industry news to keep yourself updated with the fast-changing world of tech
and business.

https://www.mygreatlearning.com/blog/python-interview-questions/?highlight=interview 42/42

You might also like