11. What is the difference between List and Dictionary data types in Python?
- Syntax: In a List we store objects in a sequence. In a Dictionary we store objects in key-value pairs.
- Reference: In List we access objects by index number. It starts from 0 index. In a Dictionary we access objects by key specified at the time of Dictionary creation.
- Ordering: In a List objects are stored in an ordered sequence. In a Dictionary objects are not stored in an ordered sequence.
- Hashing: In a Dictionary, keys have to be hashable. In a List there is no need for hashing.
Ans : Some of the built-in data types available in Python are as follows:
Numeric types: These are the data types used to represent numbers in Python.
int: It is used for Integers
long: It is used for very large integers of non-limited length. float: It is used for decimal numbers.
complex: This one is for representing complex numbers
Sequence types: These data types are used to represent sequence of characters or objects.
str: This is similar to String in Java. It can represent a sequence of characters.
bytes: This is a sequence of integers in the range of 0-255.
byte array: like bytes, but mutable (see below); only available in Python 3.x list: This is a sequence of objects.
tuple: This is a sequence of immutable objects.
Sets: These are unordered collections. set: This is a collection of unique objects.
frozen set: This is a collection of unique immutable objects.
Mappings: This is similar to a Map in Java.
dict: This is also called hashmap. It has key value pair to store information by using hashing.
13. What is a Namespace in Python?
Ans : A Namespace in Python is a mapping between a name and an object. It is currently implemented as Python dictionary.
E.g. the set of built-in exception names, the set of built-in names, local names in a function
At different moments in Python, different Namespaces are created. Each Namespace in Python can have a different lifetime.
For the list of built-in names, Namespace is created when Python interpreter starts.
When Python interpreter reads the definition of a module, it creates global namespace for that module.
When Python interpreter calls a function, it creates local namespace for that function.
14. How will you concatenate multiple strings together in Python?
Ans : We can use following ways to concatenate multiple string together in Python:
I. use + operator:
E.g.
>>> fname=”John”
>>> lname=”Ray”
>>> print fname+lname JohnRay
II. use join function:
E.g.
>>> ”.join([‘John’,’Ray’]) ‘JohnRay’
15. What is the use of Pass statement in Python?
Ans : 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:
I. Syntax purpose:
>>> while True:
… pass # Wait till user input is received
II. Minimal Class: It can be used for creating minimal classes:
>>> class MyMinimalClass:
… pass
III. 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
16. What is the use of Slicing in Python?
Ans : 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] ‘oh’
In Slicing we can give two indices in the String to create a Substring. If we do not give first index, then it defaults to 0.
E.g.
>>> name=”John”
>>> name[:2] ‘Jo’
If we do not give second index, then it defaults to the size of the String.
>>> name=”John”
>>> name[3:] ‘n’
17. What is the difference between Docstring in Python and Javadoc in Java?
Ans : 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.
Ans : 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.
Ans : An Iterable is an object that can be iterated by an Iterator.
In Python, Iterator object provides _iter_() and next() methods.
In Python, an Iterable object has _iter_ function that returns an Iterator object.
When we work on a map or a for loop in Python, we can use next() method to get an Iterable item from the Iterator.
20. What is the use of Generator in Python?
Ans : We can use Generator to create Iterators in Python. A Generator is written like a regular function. It can make use yield statement to return data during the function call. In this way we can write complex logic that works as an Iterator.
A Generator is more compact than an Iterator due to the fact that _iter_() and next() functions are automatically created in a Generator.
Also within a Generator code, local variables and execution state are saved between multiple calls. Therefore, there is no need to add extra variables like self.index etc to keep track of iteration.
Generator also increases the readability of the code written in Python. It is a very simple implementation of an Iterator.