Analytics Vidhya
Analytics Vidhya
5, Create Your
Login
ChatGPT, Build Text-to-Image Models.
Learn More
Home Beginner
90+ Python Interview Questions to Ace Your Next Job Interview in 2024
Saumyab271
S 29 Feb, 2024 • 22 min read
Introduction
Welcome to the first step of preparing for your Data Science job
questions and answers to help you ace that interview and get your
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 1 of 50
:
Almost every interview for the position of Data Scientist starts with
Trees, SVM, and kNN models. If the job description includes the term
“Deep Learning,” then prepare to answer questions on Deep Learning
models. After covering the basic topics, the interviewer will move to
Python coding. Here they would want to know about your logic and
coding skills, starting with the basics of Python coding, useful codes
This article will discuss some important and frequently asked Python
a = ‘5’ print(int(a))
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 2 of 50
:
Variable ‘a’ is a string that is now converted to an integer, as shown
below:
Output:
Output:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 3 of 50
:
[‘Analytics’, ‘Vidhya’]
function.
The above code picks the first letter, i.e., ‘A’, then adds ‘n’ at the
beginning.
Output:
ayhdiV scitylanA
Ans. We can sort a list in Python using the sort() function. The
following code will illustrate this:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 4 of 50
:
my_list = [3, 2, 1]
my_list.sort()
print(my_list)
The above code sort the list using the sort() function.
Output:
[1, 2, 3]
Ans. Mutable objects: They can be updated once defined. e.g., list.
Ans. We can delete the file in Python using the os module. The
import os
os.remove("txt1.txt")
For instance:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 5 of 50
:
Given a list [1, 2, 3, 4].
The first element of the list can be accessed using list[0], which will
Ans. There are two ways in which we can delete elements from the
list:
The remove () function deletes the mentioned element from the list.
list1 = [1, 2, 3, 4]
list1.remove(2)
print(list1)
Output:
[1, 3, 4]
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 6 of 50
:
Pop() function delete element mentioned at a specific index from the
list
list1.pop(1)
print(list1)
Output:
[1, 4]
Ans. We can delete a list in Python using the clear() function. The
list1 = [1, 2, 3, 4]
list1.clear()
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.flip(arr1)
print(arr2)
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 7 of 50
:
Output:
[4, 3, 2, 1]
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = arr1[::-1]
print(arr2)
Output:
[4,3,2,1]
array_name[index].
print(arr[index])
import numpy as np
arr2 = [1, 2, 3, 4]
x = np.delete(arr2, 0)
print(x)
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 8 of 50
:
Output:
[2,3,4]
syntax:
array_name[index] = element
This can concatenate the two lists using the zip() function, which
Output:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 9 of 50
:
Q13. Write a code snippet to generate the square of every
element of a list.
Ans. First, create an empty list. We used a for loop to iterate through
every element of a list and multiply the element by itself to generate a
lst = [1, 2, 3, 4]
lst_final = []
for x in lst:
lst_final.append(x * x)
print(lst_final)
The for loop takes the first element, i.e., 1, multiply it with itself, and
then appends it to the list. It then takes the second element, i.e., 2,
Input: [1, 2, 3, 4]
returns an iterator.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 10 of 50
:
Ans. Pickling is converting a Python object (list, dict, function, etc.) to
a byte stream(0s and 1s), and unpickling is converting the byte stream
easier readability of the code. Since Multiple people work on the same
project, it is preferable to follow a similar style for better readability
and consistency. However, we can use our judgment about what style
Ans. NumPy arrays are faster than Python lists for numerical
operations.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 11 of 50
:
and it provides a number of functions for performing operations on
arrays efficiently.
They are faster than Python lists because they are implemented in C,
which makes them faster than operations on Python lists, which are
implemented in an interpreted language.
Python also has an inbuilt array library for basic operations. W can
use it as ‘import array as arr’
different types. Lists are mutable, which means that you can change
the value of a list element or add or remove elements from a list. Lists
are created using square brackets and a comma-separated list of
values.
Lists are defined using square brackets ([ ‘’ ]), while tuples are defined
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 12 of 50
:
using parentheses ((‘’, )).
cannot index or slice them like you can with lists or tuples.
Sets are unique: Sets only allow unique objects, so if you try to
Sets are mutable: You can add or remove elements from a set
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 13 of 50
:
Sets are not hashable: Sets are mutable, so they cannot be used
Ans. Split and join are both functions of Python strings, but they are
The split function is used to create a list from strings based on some
delimiter, for e.g., space.
a = 'This is a string'
li = a.split(' ')
print(li)
delimiter string is inserted between each string in the list when the
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 14 of 50
:
“ “.join(li)
Ans. In Python, the logical operations and, or, and not can be used to
The and operator returns True if both the operands are True, and
False otherwise.
The not operator inverts the boolean value of its operand. If the
operand is True, not return False, and if the operand is False, not
return True.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 15 of 50
:
s = 'Hello, World!'
print(len(s))
13
from a string.
‘Hello, World!’
s = 'Hello, World!'
print(s.replace('World', 'Universe'))
‘Hello, Universe!’
on a delimiter.
s = 'Hello, World!'
print(s.split(','))
[‘Hello’, ‘ World!’]
upper() and lower(): These functions convert a string to
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 16 of 50
:
uppercase or lowercase, respectively.
s = 'Hello, World!'
print(s.upper())
‘HELLO, WORLD!’
s.lower()
‘hello, world!’
other methods.
class but haven’t yet decided what it should do, you can use the pass
as a placeholder.
Ans. Continue is used in a loop to skip over the current iteration and
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 17 of 50
:
Q26. What are immutable and mutable data types?
be modified after it is created. This means that you can’t change the
value of an immutable object once it is created. Examples of
modified after it is created. This means that you can change the value
objects in Python is important because it can affect how you use and
manipulate data in your code. For example, if you have a list of
numbers and you want to sort the list in ascending order, you can use
numbers, you can’t use the sort() method because tuples are
immutable. Instead, you would have to create a new sorted tuple from
the original tuple.
Ans. The try and except blocks in Python are used to handle
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 18 of 50
:
of a program.
Using a try-except block will save the code from an error to occur and
block.
Ans. 2 mutable data types are Dictionary and List. You can
change/edit the values in a Python dictionary and a list. It is not
property of mutability.
2 immutable data types are Tuples and String. You cannot edit a
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 19 of 50
:
you to reuse code and divide your code into logical blocks that can be
function may or may not return a value that depends on the usage of
your program. This can help to reduce redundancy and make your
code more concise and easier to maintain.
more effectively.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 20 of 50
:
Q30. Why does NumPy have huge popularity in the field
of data science?
functions for reading and writing data to disk and for loading only a
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 21 of 50
:
complex data science tasks.
then zero or more for or if clauses. The result is a new list that
evaluates the key-value pair in the context of the for and if clauses.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 22 of 50
:
It is important to note that you can use the same name for a global
variable and a local variable, but the local variable will take
which can change over time as the dictionary grows and evolves. An
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 23 of 50
:
ordered dictionary, on the other hand, uses a doubly linked list to
Ans. Return is used to exit a function and return a value to the caller.
produces a value and suspends its execution, saving its state for later.
anonymous function. You can use lambda functions when you don’t
want to define a function using the def keyword.
Lambda functions are useful when you need a small function for a
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 24 of 50
:
short period of time. They are often used in combination with higher-
a = lambda x: x + 10
a(5)
In this example, the lambda function takes one argument (x) and adds
10 to it. The lambda function returns the result of this operation when
it is called.
exception.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 25 of 50
:
43 3
of a program. For example, you might use an assert statement to
list.
It’s important to note that the assert statement is used for debugging
errors. In production code, you should use try and except blocks to
handle exceptions that might be raised at runtime.
Ans. The 5 built-in data types in Python are: None Type, Numeric
Types (int, float, complex, bool), Sequence Types (list, tuple, range,
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 26 of 50
:
Q39. What’s the difference between a set and a
frozenset?
Ans. We can use tuples as dictionary keys as they are hashable. Since
change. Tuples are faster and have less memory, so we can use
Q41. Is removing the first item or last item takes the same
time in the Python list?
Ans. No, removing the last item is O(1), while removing the first item is
O(n)
index.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 27 of 50
:
Ans. Python sequence data types can be accessed with both positive
Ans. While representing floating point numbers like 2.5 is easier in the
class.
Ans. args and *kwargs are syntax used for denoting variable length
arguments and variable length keyword arguments, respectively. Here
and * represents the syntax, while the args and kwargs are words
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 28 of 50
:
used by convention. We can also use other words.
produce values.
Ans. Since the generator doesn’t produce all the values at the same
time, it saves memory if we use the generator to process the
Ans. We can use zip() function to aggregate multiple lists and return
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 29 of 50
:
rq We can use append() to add at the end of a list a single item.
Ans.
def prime_or_not(number):
for i in range(2, int(sqrt(number)) + 1):
if number % i == 0:
return 0
return 1
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 30 of 50
:
Ans. One way is by using a third variable
temp = a
a = b
b = temp
a, b = b, a
Ans.
def factorial(n):
if n == 1:
return n
else:
return n * factorial(n - 1)
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 31 of 50
:
Ans.
Ans.
‘,’.join(list)
Ans.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 32 of 50
:
dictionary.
Ans.
Ans.
def mean_tuple(numbers):
result = [sum(x) / len(x) for x in zip(*numbers)]
return result
Ans. Self is used to represent the instance of the class. With this, we
can access the attributes and methods of the class with an object. It
binds the attributes of the object with the given arguments. Self is not
programming) are:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 33 of 50
:
pq Class variables: They are defined inside the class but outside
other methods and are available to access for any instance of the
class.
rq Instance variables: They are defined for each instance of the class
separately and accessible by that instance of the class.
pq Class methods: They can access only class variables and are used
to modify the class state.
state.
Ans. With inheritance, we can use the attributes and methods of the
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 34 of 50
:
parent class in the child class. We can also modify the parent class
Ans. We can use the size method of the array to check whether it
import numpy as np
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 35 of 50
:
c = np.concatenate((a, b), axis=0)
[[10, 4]
[8, 12]
[15, 6]]
Ans. A local maxima is a point greater than both of its neighbors from
either side. In a given array, the following code can produce local
maxima points.
Ans.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 36 of 50
:
resulted_arr = np.char.join(",", array)
np.swapaxes(arr,0,1)
Ans. argsort() method returns indices that can sort the array.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 37 of 50
:
can use category datatype for that variable which is internally
Ans.
df["column"] = df["column"].astype(int)
Ans. loc() is used to access the rows or columns using labels, while
Ans.
df.sort_values(by=['col1', 'col2'])
df[‘column’].idxmax()
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 38 of 50
:
This returns the row index with the maximum value of the column
specified.
Ans.
df[‘column’].str.split(pat=” ”, expand=True)
Here we can specify the pattern by which to split the column with the
pat argument.
Ans. We can use the tilde(~) operator to convert True values to False
and vice versa.
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 39 of 50
:
Ans. We can use the following code to find the percentage change
df.pct_change(periods=1)
Changes made in one object do not affect the other. In shallow copy,
only the reference is copied. So, changes made in one affect the
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 40 of 50
:
other object.
the call method. Functions are examples of that. Callable objects have
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 41 of 50
:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 42 of 50
:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 43 of 50
:
Conclusion
been beneficial to you. I’m sure you’ve all learned a few things from
this and are now confident about your next interview. These coding
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 44 of 50
:
questions also act as Python tutorials for freshers and intermediate-
frequently use.
Reading List
You can go through the following articles to learn and practice python
topics:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 45 of 50
:
30+ MCQs on Python Dictionary Manipulation
Saumyab271
S 29 Feb 2024
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 46 of 50
:
Beginner Interview Questions Interviews Python
Submit reply
joe
24 Jul, 2022
Mothilal
10 Oct, 2022
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 47 of 50
:
Mahima Choudhary
03 Nov, 2022
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 48 of 50
:
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 49 of 50
:
Company Discover
About Us Blogs
Careers Podcasts
Comprehensive Guides
Learn Engage
Contribute Enterprise
Download App
https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-freshers/ 07/03/24, 11 42
Page 50 of 50
: