Python Coding Interview Questions

Avatto > > DATA SCIENTIST > > SHORT QUESTIONS > > Python Coding Interview Questions

In Python, enumerate() function is an improvement over regular iteration. The enumerate() function returns an iterator that gives (0, item[0]).
E.g. >>> thelist=['a','b']
>>> for i,j in enumerate(thelist): ...
print i,j
...
0 a
1 b
In Functional Programming, we decompose a program into functions. These functions take input and after processing give an output. The function does not maintain any state.
Python provides built-in functions that can be used for Functional programming. Some of these functions are:
Map()
reduce()
filter()
Event iterators and generators can be used for Functional programming in Python.
We can create a common module with variables that we want to share.
This common module can be imported in all the modules in which we want to share the variables.
In this way, all the shared variables will be in one module and available for sharing with any new module as well.
We use ‘is’ to check an object against its identity.
We use ‘==’ to check equality of two objects.
E.g.
>>> lst = [10,20, 20]
>>> lst == lst[:]
True
>>> lst is lst[:]
False
Python provides a profiler called cProfile that can be used for profiling Python code.
We can call it from our code as well as from the interpreter.
It gives use the number of function calls as well as the total time taken to run the script.
We can even write the profile results to a file instead of standard out.