20 Most Asked Interview Questions of Python

saumyab271 Last Updated : 24 Oct, 2024
5 min read

Python is a versatile and interpreted programming language, making it ideal for developing web applications and playing a crucial role in Artificial Intelligence. Python interview questions for data analysts often delve into its applications in machine learning and deep learning, highlighting its increasing significance in the realm of data science.

Therefore, it becomes indispensable for every Data Scientist aspirant to have a good knowledge of Python.

In this blog, some frequently asked questions related to Python in interviews will be discussed to help you get a good grasp of the topic.

This article was published as a part of the Data Science Blogathon.

python interview questions for data analyst

Interview Questions for Python

Question 1: Explain the difference between list and tuple.

A list is a container that can be formed of non-homogenous elements. eg-

list1 = [1, 2, 3, 4, ‘a’, ‘b’]

Tuple can consist of elements of a different data type but are immutable in nature and thus are used only for accessing elements. eg –

tuple = (1, 2, 3, 4, ‘a’, ‘b’)

Question 2: Explain the difference between modules and packages in Python?

Module: A .py file containing Python code and is an executable file.

Package: It is a collection of modules along with  _init_.py file which is being used by the interpreter to interpret it as a package.

Question 3: Explain the difference between list and array.

The list can contain elements of multiple data types i.e., non-homogenous elements. eg-List1 = [1, 2, 3, 4, ‘a’, ‘b’]

The array can only contain elements of the same type i.e., homogenous elements. eg-

from array import *
array1 = array('i', [3, 2, 1])  
for x in array1:
     print(x)

Output:

3
2
1

where ‘i’ defines the type of values the array will hold i.e., unsigned integer.

Question 4: What are private, protected, and public members in Python?

Private Members: Data members that can be accessed only within the class. They are declared by using the prefix double underscore ‘__’.Protected Members: Data members that can be accessed within the class and by subclasses. They are declared by using the prefix single underscore ‘_’.

Public Members: Data members that can be accessed from any class.

Question 5: Explain the use of self in Python.

The self is used to refer to the current instance of a class i.e. self binds attribute with a class.

Question 6: Describe _init_ in Python.

The _init_ is just like the constructor of the class which is called whenever an object of the class is created. The purpose is to initialize the attributes of an object created. For eg-

Class people:
      def __init__(self, age)
            self.age = age
p = people(27)

Question 7: Explain slicing in python.

The slice () function is used to get a slice of a sequence. Its syntax is a slice(start, end, step). eg-

s1 = "analytics"
x =slice(2,5)
print(s1[x])

Output:

aly

Question 8: Explain the difference between List and Dict comprehension.

The concise way of creating a list is known as list comprehension. eg-Suppose the code is:

for i in range(5):
            if i<10:
                 print(i)

Comprehension:

 [i for i in range(5) if i < 10]

In Dict comprehension, two expressions separated by a colon followed by a for are required. Eg-

lst1 = {1, 2, 3}
lst2 = {'a', 'b', 'c'}
dict1 = {x:y for (x,y) in zip(lst1, lst2)}
print(dict1)

Output:

{1: 'c', 2: 'b', 3: 'a'}

Question 9: Explain the use of lambda in Python.

The lambda is used to define a function without a name in Python. However, it can contain only a single expression. eg-

y = lambda x: x+ 5
print(y(5))

Output:

 10

Question 10: What is the difference between split() and join().

Functions split() and join() are opposite of each other where split() is used to break a string into a list of strings using a specified separator whereas join() is used to join elements of a string by a defined separator. eg-

str1 = 'Analytics Vidhya'
print(str1.split(" "))
print("-".join(str1.split(" ")))

Output:

['Analytics', 'Vidhya']
Analytics-Vidhya

Question 11: Create a dataframe in Python.

We can create a dataframe in Python using the below code:

import pandas as pd
data = [['a', 1], ['b', 2], ['c', 3]]
df = pd.DataFrame(data, columns=['Alphabet', 'Number'])
print(df)

Output:

      Alphabet   Number
0          a              1
1          b              2
2          c              3

Question 12: Write a code to add a new column in the dataframe.

The new column in the dataframe can be added using the below code:

age = [27, 34, 56]
df['age'] = age
print(df)

Output:

      Alphabet   Number    age
0          a      1        27
1          b      2        34
2          c      3        56

Question 13: Write a code to delete a row from the dataframe.

The column ‘Number’ from the above-created dataframe can be deleted using code:

df.drop([‘Number’], axis = 1)

To drop 0th row, we can do it by:

df.drop(0)

Question 14: Read csv data in Python.

We can read a csv file using commands:

import pandas as pd
Df1 = pd.read_csv(‘transport.csv’)

Question 15: Write a code snippet to reverse list and array in Python.

Lst1 = [1, 2, 3, 4, 5]
lst2=[] 
for i in lst1:
  lst2.insert(0,i)
print(lst2)

Output:

[5, 4, 3, 2, 1]

Question 16: Write code to find the shape of an array in Python.

The shape of an array can be found using the command: arr.shape

Question 17: Explain zip() function in Python.

Zip()- Takes two iterables and combines them to form a single iterable.

lst1 = [15, 14, 13, 12, 11, 10]
lst2 = [10, 11, 12, 13, 14, 15]
x= zip(lst1, lst2)
print(set(x))

Output:

{(14, 11), (13, 12), (12, 13), (15, 10), (10, 15), (11, 14)}

Question 18: Explain the difference between del and remove()

Del- It removes the item from a specified index.

Lst1 = [40, 30, 20, 10]
Del lst1[1]

Output:

[40, 20, 10]

Remove()- It removes the first matching value from the list.

Lst1 = [40, 30, 20, 10]
Lst1.remove(30)
Print(lst1)

Output:

[40, 20, 10]

Question 19: Explain the usage of the strip() and lstrip().

Strip()- Used to remove all leading and trailing mentioned characters. Eg-

Str = “--analytics vidhya--“
Print(str.strip(‘-‘))

Output:

analytics vidhya

Lstrip()- Used to remove all leading mentioned characters.

print(str.lstrip(‘-‘))

Output:

Analytics vidhya--

Question 20: Explain the usage of join().

The function of join() is to combine together elements of a sequence. eg-

lst1 = ['4','3','2','1'] 
s1 = " "
s1 = s1.join(list1)
print(s1)

Output:

4 3 2 1

Key Takeaways

Following are takeaways from the blog:

  1. We learn about the difference between the most used terms in Python i.e., list, array, tuple, etc.
  2. We covered the basics of DataFrames and learned how to create and manipulate them.
  3. We also discussed some frequently used functions like zip(), strip(), remove(), and others.

Thanks for the read!

Frequently Asked Questions

Q1.How to prepare for a Python data analyst interview?

1. Master Python Basics
2. Excel in Data Libraries (Pandas, NumPy, Matplotlib)
3. Hands-on Data Experience
4. Learn SQL Basics
5. Build a Portfolio

Q2. How do you handle missing data in Python for data analysis?

Learn about techniques like interpolation or using Pandas functions like dropna() and fillna() to manage missing data effectively.

Q3. Can you explain the role of NumPy in data analysis with Python?

Understand how NumPy facilitates numerical operations and array manipulation, playing a crucial role in scientific computing and data analysis.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details