Python Programming Questions for Data Science Interview – Part I

 

1. What is Python?

Python is an object-oriented, open-source, high-level general-purpose programming language with automated memory management.

2. Why should we use Python?

  • Open Source Framework
  • Readability
  • Easily Maintainable Code
  • Compatible with Major Platforms and Systems
  • Robust Standard Library
  • A big Community

3. What are the native data types in Python?

 
 

There a many native data types in Python. Below are a few important ones:


1. Number

 

 

 

2. Float

 

 

 

3. String

 

 

 

4. Boolean

 

 

 

5. Set

 

 

 

6. List

 

 

 

7. Tuple

 

 

 

8. Dictionary

 

 

4. Of These, Which Are Mutable, and Which Are Immutable?

 

Mutable: Can be modified during runtime:

  • Sets
  • Lists
  • Dictionaries

Immutable: Cannot be modified during runtime

  • Numbers
  • Strings
  • Tuples

Mutable Example:

 

 

 

 

 

 

 

 

Immutable Example:

 

 

 

 

 

5.What is a Set in Python?

A set is an unordered collection of unique items.

Example:
 
# set of integers 
my_set = {7, 2, 9,20}
print(my_set)
# set of mixed datatypes
my_set1 = {1.0, "Set", (1, 2, 3)} 
print(my_set1)
 

 

6.What is a list in Python?

A list is a collection of ordered and changeable items.

Example:

 
list1 = ["apple", "banana", True, 8]
print(list1) 
list2 = [20,6,8,0]
print(list2)
 


7.What is a Tuple in Python?

A tuple is a collection of items that are ordered and unchangeable.
Example:
 
t1 = ("apple", "banana", "cherry")
print(t1)
t2 = ("apple", 2, True,'cherry')
print(t2)
t3 = (20,7,5,16)
print(t3)
t3[2]
t3[2] = 100
 


8.What is a Python Dictionary?

A dictionary is a collection that is unordered, changeable, and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values.

Example:

 
g = { "product": "Fruits", "name": ["apple", "banana", "cherry"] }
print(type(g))
print(g.keys())
print(g.values())
 
 
 

9.What are the differences between a set and a list?

Sets
Lists
Sets are non-indexable i.e. an index can’t be used to fetch the nth element
Lists are Indexable

 

Since there is no index, sets are not sliceable
From Lists, we can extract a group of consecutive elements
Sets cannot contain duplicates of an instance
Lists can contain multiple instances.

 

Sets can only contain hashable items
Objects in a list don’t need to be hashable


10.What are the differences between a list and a tuple?

List
Tuple
Lists are mutable
Tuples are immutable

 

Lists are created with square brackets
Tuples are enclosed in parentheses
Lists are slower
Tuples are faster


11.What’s the Difference Between a List and a Dictionary?

List
Dictionary
Lists can be used to store a sequence of objects that are mutable and that can be indexed
A Python dictionary is a collection of key-value pairs.
Elements in a list are accessed via the index
Elements are in a dictionary are accessed using key values.

 

A list maintains an order unless explicitly sorted
Ordering is not guaranteed in a dictionary.

 

Lists are created with square brackets
A dictionary is created with curly braces and indexed using the square bracket


12.What is a List or Dict comprehension?

List Comprehensions are used to create a new list from another list in a concise way.

Syntax of list comprehension:

[expression for items in iterable]

Example1:

 
numbers = [1, 2, 3, 4]
squares = [n**2 for n in numbers]
print(squares)
 

 

Example2:

letters = [ letter for letter in 'python' ]
print( letters)

 

Similarly, Dict Comprehension is used to create a new dictionary.

List or Dictionary comprehension is a powerful concept and can be used to substitute for loops and lambda functions.  

 
Example1:
 
# Dictinary from a list of numbers
new_dict_comp = {str(i):i for i in [1,2,3,4,5]}
print(new_dict_comp)
print(new_dict_comp.keys())
print(new_dict_comp.values())
 

 

Example 2:

keys = ['a','b','c','d','e']
values = [1,2,3,4,5] 
# but this line shows dict comprehension here 
Dict1 = { k:v for (k,v) in zip(keys, values)} 
print (Dict1)
# We can use below too
Dict2 = dict(zip(keys, values))
print (Dict2)
 

 

13.What is a negative index in Python?

A negative index is used in Python to index a list, string, or any other container class in reverse order i.e. from the end.

Example:
 
list = [1, 2, 8, 4, 7, 5, 6, 9] 
print (list[-1])
The above print statement will print the last element from the above list i.e. 9.


14.What is a Lambda Function in Python?

A lambda function is a small anonymous function that can take any number of arguments but can only have one expression.

Example:

 
my_lambda = lambda a, b : a * b
print(my_lambda(5 ,6))

The above print statement would print 30.


15. Name a few packages in the standard library, useful for Data Science work.

NumPy – It is used for general-purpose array-processing
Pandas – It is used for Data Analysis and Data Manipulation
Matplotlib – It is used for Plots
Seaborn – It is used for Statistical Data Visualization
Scikit-Learn – It is used for Machine Learning Algorithms


16.What is Pandas?

Pandas is a Python open-source library that provides high-performance and flexible data structures and data analysis tools that make working with relational or labeled data both easy and intuitive.


17.What is the difference between range & xrange?

range() and xrange() are two functions that could be used to iterate a certain number of times in for loops in Python. 
In Python 3, there is no xrange and only range.
Example:

for i in range(10):
    print(i)
 

 

 

 

18. What is the purpose of the PYTHONPATH environment variable?

PYTHONPATH is set to maintain directories of custom Python libraries that we do not want to install in the global default location. Python knows where to find its standard library and thus usually it is not required to set this variable.

19. How to perform pattern matching in Python?

In Python, text pattern matching is done with Regular Expressions or REs. 

The Python “re” module provides regular expression support by providing methods to search text strings, or replace text strings along with methods for splitting text strings based on the pattern defined.

 
Example:
 
import re
match = re.search('pat','find a pattern in the string')
if match:
    print("Pattern Found")
else:
    print("Pattern Not Found")
 

Since “pat” is there in the string “find a pattern in the string”, the above piece of code will return “Pattern Found”.

20. Sort a numerical dataset in Python.

 
my_list = [1,4,0,3,5,6,9,11,100]
print(my_list)
print(sorted(my_list))
 
 

 

21. How will you reverse a list?

my_list = [1, 9, 6, 33, 0, 7] 
print("Print original list", my_list)
print ("Print reversed list",my_list[::-1] )


22. Differentiate between append() and extend() methods?

append:  Appends object at the end.
Example:
 
x = [1, 2, 3]
x.append([4, 5])
print ("Append: ", x)

Output:

Append: [1, 2, 3, [4, 5]]

extend:   Extends list by appending elements from the iterable.
 
 
Example:
 
y = [1, 2, 3]
y.extend([4, 5])
print ("Extend: ", y)
Output:
Extend: [1, 2, 3, 4, 5]


23. How to create a list combining 2 different lists?

The task is to create a list which has all the elements of a and b in one dimension

Option1:

a = [1,2,3,4,5]
b = [6,7,8,9]
print(a + b)
 

 

Option2:

a = [1,2,3,4,5]
b = [6,7,8,9]
a.extend(b)
print(a)
 

 

The extend function makes the changes to the original list itself. 

24. Calculate the sum of a list of numbers.

import numpy as np
a = [1,6,8,12]
print(np.sum(a))

The output of the above print statement is 27.

25. How to convert a string to all lowercase?

string = 'HELLO WORLD!'
print(string)
print(string.lower())
 
 

 

 

26. How is memory managed in Python?

-Memory management in python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The python interpreter takes care of this instead.

– The allocation of heap space for Python objects is done by Python’s memory manager. The core API gives access to some tools for the programmer to code.

– Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space.


27. What is pep 8?

PEP stands for Python Enhancement Proposal. It is a set of rules that specify how to format Python code for maximum readability.

 

28. What is a map function in Python?

The map() function executes a specified function for each item in an iterable (list, tuple, etc.). The item is sent to the function as a parameter.

 
# Return double of n 
def add_numbers(n):
    return n + n
# We double all numbers using map()
n = (1, 2, 3, 4)
mymap = map(add_numbers, n)
print(mymap)
print(next(mymap))
print(next(mymap))
print(next(mymap))
print(next(mymap))
 
 

29. What is Type Conversion in Python?

 

Type conversion means the conversion of one data type into another. This can happen implicitly where the Python Compiler does that automatically or by explicitly telling the compiler.

Implicit Data Type Conversion:

a_int = 1
b_float = 1.0
c_sum = a_int + b_float
print(c_sum)
print(type(c_sum))
 

Explicit Data Type Conversion:
int() – converts any data type into integer type

float()– converts any data type into float type

str() – Used to convert an integer into a string.

set() – This function returns the type after converting to set.

tuple() – This function is used to convert to a tuple.

dict() – This function is used to convert a tuple of order (key, value) into a dictionary.

# using int(), float()
# initializing string
s = "10010"
# printing string converting to int base 2
c = int(s)
print ("After converting to integer : ", c) 
# printing string converting to float
e = float(s)
print ("After converting to float : ", e)

 

30.Explain split(), sub() and subn() methods of “re” module in Python.

split() – uses a regex pattern to “split” a given string into a list.

sub() – finds all substrings where the regex pattern matches and then replace them with a different string

subn() – it is similar to sub() and also returns the new string along with the no. of replacements.

import re
print(split('W+', 'apple, mango , banana'))
print(split('W+', 'On 07th Nov 2019'))
###
print(re.sub('oo', '~*' , 'book moon broom', flags = re.IGNORECASE))
print(re.sub('oo', '~*' , 'sky sun stars', flags = re.IGNORECASE))
##
print(re.subn('oo', '~*' , 'book moon broom'))

So, that’s all for basic python programming questions. Sometimes it gets essential to revise the basics.

For more interview prepataion, please check the below posts:

A Quick Review of Data Science Concepts for InterviewPrep – 1

20 Behavioral (Non Technical) Interview Questions

If you want to practice a few programming challenges, please check the below posts:

Codility CyclicRotation – Python Solution

Codility BinaryGap – Python Solution

Thank You for reading!

0

Leave a Reply

Your email address will not be published. Required fields are marked *