Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
208 views

Python Coding Interview Questions For Freshers

This article discusses 13 common Python coding interview questions for beginners. It covers questions about converting between data types like strings and integers, manipulating strings and lists, accessing and modifying array elements, and using built-in functions. The questions focus on Python basics to evaluate a candidate's logic and coding skills. Mastering these fundamental techniques is more important than advanced topics for entry-level roles.

Uploaded by

Shubham J
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
208 views

Python Coding Interview Questions For Freshers

This article discusses 13 common Python coding interview questions for beginners. It covers questions about converting between data types like strings and integers, manipulating strings and lists, accessing and modifying array elements, and using built-in functions. The questions focus on Python basics to evaluate a candidate's logic and coding skills. Mastering these fundamental techniques is more important than advanced topics for entry-level roles.

Uploaded by

Shubham J
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Coding Interview Questions for Freshers

BE G I NNE R I NT E RVI E W Q UE S T I O NS I NT E RVI E W S PYT HO N

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

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.

So, let’s start:

Interview Questions for Python Coding

Question 1: Convert a given string to int using a single line of code.

We can convert a given string to an integer using a built-in function int(). e.g.-

a = ‘5’ print(int(a))

Variable ‘a’ is a string that is now converted to an integer, as shown below:

Output: 
5

Question 2: Write a code snippet to convert a string to a list.

Below is the code to convert string to list in Python.

str1 = "Analytics Vidhya" print(str1.split(" "))

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']

Question 3: Write a code snippet to reverse a string.

Here, we have reversed a string without using any in-built function.

str1 = "Analytics Vidhya" str2 = "" for i in str1: str2 = i + str2

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.

Further, ‘nA’ is taken as str2, ‘ a’ is added before it, and so on.

Then ‘anA’ becomes str2, and the next letter, i.e., ‘l’, is appended at the start of str2 to make it ‘lanA.’

This is how the above code works to reverse the string.

Output:

ayhdiV scitylanA

Question 4: Write a code snippet to sort a list in Python.

The list in Python can be sorted using the sort() function. eg-

lst1 = [3, 2, 1] lst1.sort() print(lst1)

The above code sort the list using sort() function.

Output:

[1, 2, 3]
Question 5: What is the difference between mutable and immutable.

Mutable objects: They can’t be updated once defined. eg- list

Immutable objects: They can be updated when required. eg- tuples

Question 6: How can you delete a file in Python.

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”)

Question 7: How to access an element of a list?

The element in a list can be accessed using list_name [index]. For instance-

Given a list [1, 2, 3, 4].

The indexing of the list starts from 0.

The first element of the list can be accessed using list[0] which will print element “1”.

Second element can be accessed using list[1] and so on.

Question 8: Discuss different ways of deleting an element from a list?

Two ways in which elements can be deleted from the list :

1. By using remove() function

Remove() function deletes mentioned element from the list.

list1 = [1, 2, 3, 4] list1.remove(2) print(list1)

Output:

[1, 3, 4]

2. By using pop() function

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?

A list in python can be deleted by using the clear() function.

list1 = [1, 2, 3, 4] list1.clear()

It will delete the entire list.

Question 10: Write a code snippet to reverse an array.

Two ways of reversing an array are as follows:

1. Using flip() function

import numpy as np arr1=np.array([1, 2, 3, 4]) arr2=np.flip(arr1) print(arr2)

Output:

[4, 3, 2, 1]

2. Without using any function

import numpy as np arr1=np.array([1, 2, 3, 4]) arr2= arr1[::-1] print(arr2)

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.

Access: The element of an array can be accessed by using array_name[index].

print(arr[index])

Delete: The element of an array can be deleted using delete() function.

arr2 = [1,2,3,4] import numpy as np x= np.delete(arr2,0) print(x)

Output:

[2,3,4]

Update: The element of an array can be using using below syntax:

array_name[index] = element
Question 12: Write a code snippet to concatenate lists.

Suppose, given two lists are:

List1= [“W”, “a”, “w”,”b”]

List2 = [“e”, “ “,”riting”,”log”]

And the output should be:

[‘We’, ‘a ‘, ‘writing’, ‘blog’]

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:

['We', 'a ', 'writing', 'blog']

Question 13: Write a code snippet to generate square of every element of a list.

Input: [1, 2, 3, 4]

Output: [1, 4, 9, 16]

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.

lst = [1, 2, 3, 4] lst_final = [] for x in lst: lst_final.append(x * x) print(lst_final)

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.

Following are takeaways from the article:

1. We studied questions on strings that are used in most python projects.

2. We became familiar with the concept of list and array.

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.

Article Url - https://www.analyticsvidhya.com/blog/2022/07/python-coding-interview-questions-for-


freshers/

Saumyab271

You might also like