Python Interview Questions – Part V

41. How do you profile a Python script?

Ans : 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.

42. What is the difference between ‘is’ and ‘==’ in Python?

Ans : 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

43. How will you share variables across modules in Python?  (Pending )

Ans :  

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.

44. How can we do Functional programming in Python?

Ans : 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:

I. Map()

II. reduce()

III. filter()

Event iterators and generators can be used for Functional programming in Python.

45. What is the improvement in enumerate() function of Python?

Ans : 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

46. How will you execute a Python script in Unix?

Ans : To execute a Python script in Unix, we need to have Python executor in   Unix environment.

In addition to that we have to add following line as the first line in a Python script file.

#!/usr/local/bin/python

This will tell Unix to use Python interpreter to execute the script.

47. What are the popular Python libraries used in Data analysis?

Ans : Some of the popular
libraries of Python used for Data analysis are:

  • Pandas: Powerful
    Python Data Analysis
    Toolkit
  • SciKit: This is a machine learning library in
    Python.
  • Seaborn: This is a statistical data visualization
    library in Python.
  • SciPy:
    This is an open source system for science, mathematics and
    engineering implemented in Python.
               

48. What is the output of following code in Python?

Ans : >>> thelist=[‘a’,’b’]

>>> print thelist[3:]

Ans: The output of this code is following:

[]

Even though the list has only 2 elements, the call to thelist with index 3 does not give any index error.

49. What is the output of following code in Python?

Ans : >>>name=’John Smith’

>>>print name[:5] + name[5:] Ans: Output of this will be John Smith

This is an example of Slicing. Since we are slicing at the same index, the first name[:5] gives the substring name upto 5th location excluding 5th location. The name[5:] gives the rest of the substring of name from the 5th location. So we get the full name as output.

50. If you have data with name of customers and their location, which data type will you use to store it in Python?

Ans : In Python, we can use dict data type to store key value pairs. In this example, customer name can be the key and their location can be the value in a dict data type.

Dictionary is an efficient way to store data that can be looked up based on a key.

 

 

 

Leave a Comment

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

Scroll to Top