PYTHON INTERVIEW QUESTIONS
What is the use of zip() function in Python?
In Python, we have a built-in function zip() that can be used to aggregate all the Iterable objects of an Iterator. We can use it to aggregate Iterable objects from two iterators as well. E.g. list_1 = [‘a’, ‘b’, ‘c’] list_2 = [‘1’, ‘2’, ‘3’] for a, b in zip(list_1, list_2): print a, b Output:…
What is None in Python?
None is a reserved keyword used in Python for null objects. It is neither a null value nor a null pointer. It is an actual object in Python. But there is only one instance of None in a Python environment. We can use None as a default argument in a function. During comparison we have…
What is Python Flask?
Python Flask is a micro-framework based on Python to develop a web application. It is a very simple application framework that has many extensions to build an enterprise level application. Flask does not provide a data abstraction layer or form validation by default. We can use external libraries on top of Flask to perform such…
What is the use of frozenset in Python?
A frozenset is a collection of unique values in Python. In addition to all the properties of set, a frozenset is immutable and hashable. Once we have set the values in a frozenset, we cannot change. So we cannot use and update methods from set on frozenset. Being hashable, we can use the objects in…
What is a metaclass in Python?
A metaclass in Python is also known as class of a class. A class defines the behavior of an instance. A metaclass defines the behavior of a class. One of the most common metaclass in Python is type. We can subclass type to create our own metaclass. We can use metaclass as a class-factory to…
What are the main benefits of using Python?
Some of the main benefits of using Python are as follows: I. Easy to learn: Python is simple language. It is easy to learn for a new programmer. II. Large library: There is a large library for utilities in Python that can be used for different kinds of applications. III. Readability: Python has a variety…
How will you copy an object in Python?
In Python we have two options to copy an object. It is similar to cloning an object in Java. I. Shallow Copy: To create a shallow copy we call copy.copy(x). In a shallow copy, Python creates a new compound object based on the original object. And it tries to put references from the original object…
What is lambda expression in Python?
A lambda expression in Python is used for creating an anonymous function. Wherever we need a function, we can also use a lambda expression. We have to use lambda keyword for creating a lambda expression. Syntax of lambda function is as follows: lambda argumentList: expression E.g. >>>lambda a,b: a+b The above mentioned lambda expression takes…
What is the difference between xrange and range in Python?
In Python, we use range(0,10) to create a list in memory for 10 numbers. Python provides another function xrange() that is similar to range() but xrange() returns a sequence object instead of list object. In xrange() all the values are not stored simultaneously in memory. It is a lazy loading based function. But as per…
What is the significance of functions that start and end with underscore_ symbol in Python?
Python provides many built-in functions that are surrounded by _ symbol at the start and end of the function name. As per Python documentation, double _ symbol is used for reserved names of functions. These are also known as System-defined names. Some of the important functions are: Object._new_ Object._init_ Object._del_
What is the use of Generator in Python?
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…
What is the difference between an Iterator and Iterable in Python?
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…
How do you perform unit testing for Python code?
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: I. Test fixture: We use test fixture to create preparation methods required to run a test.…
What is the difference between Docstring in Python and Javadoc in Java?
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…
What is Slicing in Python?
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.…
What is the use of Pass statement in Python?
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…
How to concatenate multiple strings together in Python?
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’
What is a Namespace in Python?
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.…
What are the different builtin datatypes available in Python?
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…
What is the difference between List and Dictionary data types in Python?
Main differences between List and Dictionary data types in Python are as follows: I. Syntax: In a List we store objects in a sequence. In a Dictionary we store objects in key-value pairs. II. Reference: In List we access objects by index number. It starts from 0 index. In a Dictionary we access objects by…