PYTHON INTERVIEW QUESTIONS
What is Python? What are the benefits of using Python?
Python is a programming language with objects, modules, threads, exceptions, and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure, and it is open-source.
What is pickling and unpickling?
Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function. This process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.
What are the tools that help to find bugs or perform the static analysis?
PyChecker is a static analysis tool that detects the bugs in Python source code Pylint is another tool that verifies whether the module meets the coding standard.
What is the difference between list and tuple?
The difference between list and tuple is that list is mutable [ ] while tuple is not () .
What are built-in type does python provides?
Python provides two built-in types: 1) Mutable and 2) Immutable.
Mutable built-in types are:
- List
- Sets
- Dictionaries
- Immutable built-in types
- Strings
- Tuples
- Numbers
Immutable built-in types are:
- Strings
- Tuples
- Numbers
1. index()
index is an inbuilt function in Python, which searches for a given element from the
start of the list and returns the lowest index where the element appears.
Syntax :
list_name.index(element, start, end)
2.Leave One Out Cross-Validation (LOOCV)
LOOCV is the cross-validation technique in which the size of the fold is “1” with “k” being set to the number of observations in the data. This variation is useful when the training data is of limited size and the number of parameters to be tested is not high.from math import
import operator
#reindex the DataFrame df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'B']) print df_reindexed
Its output is as follows −
A C B 0 2016-01-01 Low NaN 2 2016-01-03 High NaN 5 2016-01-06 Low NaN
ABC ACB BAC BCA CAB CBA
Example :
The following example passes a function and checks the value of each element in series and returns low, normal or High accordingly.
Output:
11. Lambda:-
We use lambda functions when we require a nameless function for a short period of time. In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments).
Example :
In the following example, a temporary anonymous function is made in .apply itself using lambda. It adds 5 to each value in series and returns a new series.
Output:
0 50.12 1 54.10 2 54.65 3 52.38 4 52.95 Name: Stock Price, dtype: float64 0 55.12 1 59.10 2 59.65 3 57.38 4 57.95 Name: Stock Price, dtype: float64 3007 772.88 3008 771.07 3009 773.18 3010 771.61 3011 782.22 Name: Stock Price, dtype: float64 3007 777.88 3008 776.07 3009 778.18 3010 776.61 3011 787.22 Name: Stock Price, dtype: float64
As observed, New values = old values + 5
12.value count():-
Index.value_counts() function to count the number of unique values in the given Index.Output :
Let’s find the count of all unique values in the index.
Output :
13. groupby:-
groupby() function to form groups based on more than one category (i.e. Use more than one column to perform the splitting).Output :
Stack method works with the MultiIndex objects in DataFrame, it returning a DataFrame with an index with a new inner-most level of row labels. It changes the wide table to a long table.
Unstack is similar to stack method, It also works with multi-index objects in dataframe, producing a reshaped DataFrame with a new inner-most level of column labels.






Comments
Post a Comment