Top 10 Most Asked Python Interview Questions With Answers Part 06
By Muhammad Umair
Q.01 What are negative indexes and why are they used?
Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2) is the second last index and so forth.
The sequences in Python are indexed and it consists of the positive as well as negative numbers. The numbers that are positive uses ‘0’ that is uses as first index and ‘1’ as the second index and the process goes on like that.
The index for the negative number starts from ‘-1’ that represents the last index in the sequence and ‘-2’ as the penultimate index and the sequence carries forward like the positive number.
The negative index is used to remove any new-line spaces from the string and allow the string to except the last character that is given as S[:-1]. The negative index is also used to show the index to represent the string in correct order.
Let’s take a list for this.
A negative index, unlike a positive one, begins searching from the right.
This also helps with slicing from the back:
[3, 4, 5, 6, 7]
Q.02 Explain split(), sub(), subn() methods of
re module in Python.
- To modify the strings, Python’s “re” module is providing 3 methods. They are:
- 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.
Q.03 What is map function in Python?
Map function executes the function given as the first argument on all the elements of the iterable given as the second argument. If the function given takes in more than 1 arguments, then many iterables are given. #Follow the link to know more similar functions
Q.04 How to get indices of N maximum values in a NumPy array?
We can get the indices of N maximum values in a NumPy array using the below code:
import numpy as nparr = np.array([1, 3, 2, 4, 5])print(arr.argsort()[-3:][::-1])
Q.05 What is a Python module?
A module is a Python script that generally contains import statements, functions, classes and variable definitions, and Python runnable code and it “lives” file with a ‘.py’ extension. zip files and DLL files can also be modules.Inside the module, you can refer to the module name as a string that is stored in the global variable name .
Q.06 Name the File-related modules in Python?
Python provides libraries / modules with functions that enable you to manipulate text files and binary fileson file system. Using them you can create files, update their contents, copy, and delete files. The librariesare : os, os.path, and shutil.
os.path – modules include functions for accessing the filesystem
shutil – module enables you to copy and delete the files.
Q.07 How many kinds of sequences are supported by Python? What are they?
Python supports 7 sequence types. They are str, list, tuple, unicode, byte array, xrange, and buffer. where xrange is deprecated in python 3.5.X.
Q.08 How to display the contents of text file in reverse order?How will you reverse a list?
list.reverse() − Reverses objects of list in place, convert the given file into a list. Reverse the list by using
for line in reversed(list(open("file-name","r"))): print(line)
Q.09 What is the difference between NumPy and SciPy?
In an ideal world, NumPy would contain nothing but the array data type and the most basic operations: indexing, sorting, reshaping, basic element wise functions, et cetera. All numerical code would reside in SciPy. However, one of NumPy’s important goals is compatibility, so NumPy tries to retain all features supported by either of its predecessors. Thus NumPy contains some linear algebra functions, even though these more properly belong in SciPy. In any case, SciPy contains more fully-featured versions of the linear algebra modules, as well as many other numerical algorithms. If you are doing scientific computing with python, you should probably install both NumPy and SciPy. Most new features belong in SciPy rather than NumPy.
Q.10 Which of the following is an invalid statement?
a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000Answer: b