DEV Community

Cover image for Python coding interview questions for data scientist
Shivani tiwari
Shivani tiwari

Posted on

Python coding interview questions for data scientist

1) How do you debug a Python program?

By using this command we can debug a python program

Example- Copy this code to your compiler

$ python -m pdb python-script.py
2) What is Keyword in Python?

The keyword in Python can turn any function into a generator. Yields work like a standard return keyword.

But it’ll always return a generator object. Also, a function can have multiple calls to the keyword

Example- Copy this code to your compiler

def testgen(index):
weekdays = ['sun','mon','tue','wed','thu','fri','sat']
yield weekdays[index]
yield weekdays[index+1]
day = testgen(0)
print next(day), next(day)
Output: sun mon
Read Article-How to find Count pairs with given sum using python

3) How to convert a list into a string?

When we want to convert a list into a string, we can use the <”.join()> method which joins all the elements into one and returns as a string.

Example- Copy this code to your compiler

weekdays = ['sun','mon','tue','wed','thu','fri','sat']
listAsString = ' '.join(weekdays)
print(listAsString)P

4) How to convert a list into a tuple?

By using Python function we can convert a list into a tuple. But we can’t change the list after turning it into a tuple, because it becomes immutable.

Example- Copy this code to your compiler

weekdays = ['sun','mon','tue','wed','thu','fri','sat']
listAsTuple = tuple(weekdays)
print(listAsTuple)
Read Article-How to Find duplicate elements of an array in python

5) How to convert a list into a set?

User can convert list into set by using function.

Example- Copy this code to your compiler

weekdays = ['sun','mon','tue','wed','thu','fri','sat','sun','tue']
listAsSet = set(weekdays)
print(listAsSet)
6) How to count the occurrences of a particular element in the list?

In the Python list, we can count the occurrences of an individual element by using a function.

Example- Copy this code to your compiler

weekdays = ['sun','mon','tue','wed','thu','fri','sun','mon','mon']
print(weekdays.count('mon'))
7) What is the NumPy array?

NumPy arrays are more flexible than lists in Python. By using NumPy arrays reading and writing items is faster and more efficient.

8) How can you create an Empty NumPy Array In Python?

We can create an Empty NumPy Array in two ways in Python,

1) import numpy
numpy.array([])

2) numpy.empty(shape=(0,0))

9) What is a negative index in Python?

Python has a special feature like a negative index in Arrays and Lists. A positive index reads the elements from the starting of an array or list but in the negative index, Python reads elements from the end of an array or list.

10) What is the output of the below code?

Example- Copy this code to your compiler

import array

a = [1, 2, 3]
print a[-3]
print a[-2]
print a[-1]
The output is: 3, 2, 1


Top comments (0)