Avatto>>DATA SCIENTIST>>SHORT QUESTIONS>>Python Interview Questions
We can use the unit testing modules unittest or unittest2 to create and run unit tests for Python code.

We can even do automation of tests with these modules. Some of the main components of unittest are as follows:

Test fixture: We use test fixture to create preparation methods required to run a test. It can even perform post-test cleanup.

Test case: This is main unit test that we run on a piece of code. We can use Testcase base class to create new test cases.

Test suite: We can aggregate our unit test cases in a Test suite.

Test runner: We use test runner to execute unit tests and produce reports of the test run.
A Docstring in Python is a string used for adding comments or summarizing a piece of code in Python.

The main difference between Javadoc and Docstring is that docstring is available during runtime as well. Whereas, Javadoc is removed from the Bytecode and it is not present in .class file.

We can even use Docstring comments at run time as an interactive help manual.

In Python, we have to specify docstring as the first statement of a code object, just after the def or class statement.

The docstring for a code object can be accessed from the '__doc__' attribute of that object.
We can use Slicing in Python to get a substring from a String.

The syntax of Slicing is very convenient to use.
E.g. In following example, we are getting a substring out of the name John.

>>> name="John"

>>> name[1:3]


In Slicing, we can give two indices in the String to create a Substring. If we do not give the first index, then it defaults to 0.


>>> name="John"

>>> name[:2]


If we do not give second index, then it defaults to the size of the String.

>>> name="John"

>>> name[3:]

The use of Pass statement is to do nothing. It is just a placeholder for a statement that is required for syntax purpose. It does not execute any code or command.

Some of the use cases for pass statement are as follows:

1.Syntax purpose:

>>> while True:

... pass # Wait till user input is received

2.Minimal Class: It can be used for creating minimal classes:

>>> class MyMinimalClass:

... pass

3.Place-holder for TODO work: We can also use it as a placeholder for TODO work on a function or code that needs to be implemented at a later point of time.

>>> def initialization():

... pass # TODO
We can use the following ways to concatenate multiple strings together in Python:

1. use + operator:
>>> fname="John"
>>> lname="Ray"
>>> print fname+lname
JohnRay 2.  use join function:
>>> ''.join(['John','Ray'])