Python Coding Interview Questions For Freshers
Python Coding Interview Questions For Freshers
Introduction
Python is an interpreted and general-purpose programming language that is in high demand nowadays due
to its usage in Artificial Intelligence. Therefore, it becomes indispensable for every Data Science aspirant
to have a strong understanding of functions used in Python.
Almost every interview for the position of Data Scientist starts with the basics of Machine Learning, where
the focus is mostly on Decision Trees, SVM, and kNN models. If the job description includes the term
“Deep Learning,” then questions on Deep Learning models may be asked. Once the basics have been asked,
the interviewer will move to Python coding, where he wants to know about your logic and coding skills, so
he starts with the basics of Python coding. Advanced level coding is generally not asked in interviews. So,
it is always better to have a strong grip on basics instead of getting into the rat race.
In this article, some important and frequently asked questions related to Python coding will be discussed
to help Data Science aspirants get a good grasp of the topic.
We can convert a given string to an integer using a built-in function int(). e.g.-
a = ‘5’ print(int(a))
Output:
5
The split() function separates the given string by the defined delimiter i.e., space(” “) here. Therefore,
Analytics and Vidhya break down to two strings in a list.
Output:
['Analytics', 'Vidhya']
print("The original string is: ",str1) print("The reversed string is: ",str2)
The above code picks the first letter, i.e., ‘A’, then adds ‘n’ at the beginning.
Then ‘anA’ becomes str2, and the next letter, i.e., ‘l’, is appended at the start of str2 to make it ‘lanA.’
Output:
ayhdiV scitylanA
The list in Python can be sorted using the sort() function. eg-
Output:
[1, 2, 3]
Question 5: What is the difference between mutable and immutable.
The file in python can be deleted using the os module. The remove() function of the os module is used to
delete a file in Python. eg-
import os os.remove(“txt1.txt”)
The element in a list can be accessed using list_name [index]. For instance-
The first element of the list can be accessed using list[0] which will print element “1”.
Output:
[1, 3, 4]
Pop() function delete element mentioned at specific index from the list
list1.pop(1) print(list1)
Output:
[1, 4]
Question 9: Write a code snippet to delete an entire list?
Output:
[4, 3, 2, 1]
Output:
[4,3,2,1]
Question 11: Write a code snippet to get an element, delete an element and update an
element in an array.
print(arr[index])
Output:
[2,3,4]
array_name[index] = element
Question 12: Write a code snippet to concatenate lists.
This can be done using zip() function which iterates through both lists and combines them index-wise.
lst1= ['W', 'a', 'w','b'] lst2 = ['e', ' ','riting','log'] lst3 = [x + y for x, y in zip(lst1, lst2)]
print(lst3)
Output:
Question 13: Write a code snippet to generate square of every element of a list.
Input: [1, 2, 3, 4]
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 square of it. Then, append to the newly generated list.
The for loop takes first element i.e., 1, multiply it with itself and then append it to the list. Then second
element i.e., 2 is taken, multiply it by itself and appended to the list and so on.
Output:
[1, 4, 9, 16]
Conclusion
To sum up, basic Python coding interview questions have been discussed, covering functions that are
frequently used in any Python project.
3. Further, how functions can be performed on arrays and lists have been discussed.
The media shown in this ar ticle is not owned by Analytics Vidhya and is used at the Author’s discretion.
Saumyab271