This article was published as a part of the Data Science Blogathon.
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.
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 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?
Public Members: Data members that can be accessed from any class.
Question 5: Explain the use of self in Python.
Question 6: Describe _init_ in Python.
Class people: def __init__(self, age) self.age = age p = people(27)
Question 7: Explain slicing in python.
s1 = "analytics" x =slice(2,5) print(s1[x])
Output:
aly
Question 8: Explain the difference between List and Dict comprehension.
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.
y = lambda x: x+ 5 print(y(5))
Output:
10
Question 10: What is the difference between split() and join().
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.
df.drop([‘Number’], axis = 1)
To drop 0th row, we can do it by:
df.drop(0)
Question 14: Read csv data in Python.
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.
Question 17: Explain zip() function in Python.
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()
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().
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().
lst1 = ['4','3','2','1'] s1 = " " s1 = s1.join(list1) print(s1)
Output:
4 3 2 1
In this blog, we have studied some important and frequently asked interview questions on Python. 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 got a basic understanding of Dataframes and how they are created and manipulated.
3. Some of the frequently used functions such as zip(), strip(), remove(), etc., has been discussed.
Thanks for the read!
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
Learn about techniques like interpolation or using Pandas functions like dropna() and fillna() to manage missing data effectively.
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.
Lorem ipsum dolor sit amet, consectetur adipiscing elit,