Python Interview Questions 16-20
Let’s start with the 4th set of Python Interview Questions. Mostly, the product-based companies in India ask these questions in the first round of interviews for Business Intelligence Engineer/Data Analyst/Business Analyst/Data Scientist
Python Interview Questions
16. Interchange first and last element in a list
l = [1,2,3,5,6]
temp = l[0]
l[0] = l[len(l)-1]
l[len(l)-1] = temp
print(l)
or
l = [4,5,6,7,8]
print("List",l)
def inter(l):
l[0],l[-1] = l[-1],l[0]
return l
print(“Interchanged List” , inter(l))
17. Swap two positions in a list
p1 = 2
p2 = 5
l = [1,2,3,4,5,6,7]
print("Original List",l)
def swap(l,p1,p2):
l[p1],l[p2] = l[p2],l[p1]
return l
print("Swaped list", swap(l,p1,p2))
18. To check if a particular element is there in the list
l = ['a','b','d']
count = l.count('d')
print(count)
or
l_set = set(l)
if 'b' in l_set:
print("Mil gaya")
or
Use a naive method of loop
for i in range(0,len(l)):
if l[i] == 'd':
print("Mil gaya at",i)
19. Get the sum of elements in a list
l = [1,3,5,6]
s = 0
for i in range(0,len(l)):
s = s+l[i]
print(s)
or
list1 = [9,8,7,6]
def sumOfList(list, size):
if (size == 0):
return 0
else:
return list[size - 1] + sumOfList(list, size - 1)
total = sumOfList(list1, len(list1))
print("Sum of all elements in given list: ", total)
20. Multiple all the numbers in a list
l = [1,3,5,6]
m = 1
for i in range(0,len(l)):
s = s*l[i]
print(s)
The Data Monk services
We are well known for our interview books and have 70+ e-book across Amazon and The Data Monk e-shop page . Following are best-seller combo packs and services that we are providing as of now
- YouTube channel covering all the interview-related important topics in SQL, Python, MS Excel, Machine Learning Algorithm, Statistics, and Direct Interview Questions
Link – The Data Monk Youtube Channel - Website – ~2000 completed solved Interview questions in SQL, Python, ML, and Case Study
Link – The Data Monk website - E-book shop – We have 70+ e-books available on our website and 3 bundles covering 2000+ solved interview questions. Do check it out
Link – The Data E-shop Page - Instagram Page – It covers only Most asked Questions and concepts (100+ posts). We have 100+ most asked interview topics explained in simple terms
Link – The Data Monk Instagram page - Mock Interviews/Career Guidance/Mentorship/Resume Making
Book a slot on Top Mate
The Data Monk e-books
We know that each domain requires a different type of preparation, so we have divided our books in the same way:
1. 2200 Interview Questions to become Full Stack Analytics Professional – 2200 Most Asked Interview Questions
2.Data Scientist and Machine Learning Engineer -> 23 e-books covering all the ML Algorithms Interview Questions
3. 30 Days Analytics Course – Most Asked Interview Questions from 30 crucial topics
You can check out all the other e-books on our e-shop page – Do not miss it
For any information related to courses or e-books, please send an email to [email protected]