Python Interview Questions – Part III

21. What is the significance of functions that start and end with _ symbol in Python?

Ans : 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_
22. What is the difference between xrange and range in Python?
 
Ans : 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 Python documentation, the benefit of xrange() over range() is very minimal in regular scenarios.

As of version 3.1, xrange is deprecated.

23. What is lambda expression in Python?

Ans : 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 two arguments and returns their sum.

We can use lambda expression to return a function.

A lambda expression can be used to pass a function as an argument in another function.

24. How will you copy an object in Python?

Ans : In Python we have two options to copy an object. It is similar to cloning an object in Java.

  • 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 into copy object.
  • Deep Copy: To create a deep copy, we call copy.deepcopy(x). In a deep copy, Python creates a new object and recursively creates and inserts copies of the objects from original object into copy object. In a deep copy, we may face the issue of recursive loop due to infinite recursion.
 
25. What are the main benefits of using Python?
 
Ans : Some of the main benefits of using Python are as follows:

Easy to learn: Python is simple language. It is easy to learn for a new programmer.

Large library: There is a large library for utilities in Python that can be used for different kinds of applications.

Readability: Python has a variety of statements and expressions that are quite readable and very explicit in their use. It increases the readability of overall code.

Memory management: In Python, memory management is built into the Interpreter. So a developer does not have to spend effort on managing memory among objects.

Complex built-in Data types: Python has built-in Complex data types like list, set, dict etc. These data types give very good performance as well as save time in coding new features.

26. What is a meta class in Python?

Ans : 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 create different types of classes.

27. What is the use of frozen set in Python?

Ans : 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 frozenset as keys in a Dictionary.

28. What is Python Flask?

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

29. What is None in Python?

Ans : 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 to use “is” operator instead of “==” for None.

30. What is the use of zip() function in Python?

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

a1 

b2 

c3

By using zip() function we can divide our input data from different sources into fixed number of sets.

 

Leave a Comment

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

Scroll to Top