Python Interview Questions and Answers – Part I

1. How will you improve the performance of a program in Python?

Ans : There are many ways to improve the performance of a Python program. Some of these are as follows:

  • Data Structure: We have to select the right data structure for our purpose in a Python program.
  • Standard Library: Wherever possible, we should use methods from standard library. Methods implemented in standard library have much better performance than user implementation.
  • Abstraction: At times, a lot of abstraction and indirection can cause slow performance of a program. We should remove the redundant abstraction in code.
  • Algorithm: Use of right algorithm can make a big difference in a program. We have to find and select the suitable algorithm to solve our problem with high performance.

2. What are the benefits of using Python?

Ans : Python is strong that even Google uses it. Some of the benefits of using Python are as follows:

  • Efficient: Python is very efficient in memory management. For a large data set like Big Data, it is much easier to program in Python.
  • Faster: Though Python code is interpreted, still Python has very fast performance.
  • Wide usage: Python is widely used among different organizations for different projects. Due to this wide usage, there are thousands of add-ons available for use with Python.
  • Easy to learn: Python is quite easy to learn. This is the biggest benefit of using Python. Complex tasks can be very easily implemented in Python.

3. How will you specify source code encoding in a Python source file?

Ans : By default, every source code file in Python is in UTF-8 encoding. But we                 can also specify our own encoding for source files. This can be done by adding following line after #! line in the source file.

# -*- coding: encoding -*-  

In the above line we can replace encoding with encoding that we want to use.
 
4. What is the use of PEP 8 in Python?
 
Ans : PEP 8 is a style guide for Python code. This document provides the coding conventions for writing code in Python. Coding conventions are about indentation, formatting, tabs, maximum line length, imports organization, line spacing etc. We use PEP 8 to bring consistency in our code. We consistency it is easier for other developers to read the code.
 
5. What is Pickling in Python?
 
Ans : Pickling is a process by which a Python object hierarchy can be converted into a byte stream. The reverse operation of Pickling is Unpickling.
Python has a module named pickle. This module has the implementation of a powerful algorithm for serialization and de-serialization of Python object structure.
Some people also call Pickling as Serialization or Marshalling.
With Serialization we can transfer Python objects over the network. It is also used in persisting the state of a Python object. We can write it to a file or a database.
 
6. How does memory management work in Python?
 
Ans : There is a private heap space in Python that contains all the Python objects and data structures. In CPython there is a memory manager responsible for managing the heap space.
There are different components in Python memory manager that handle segmentation, sharing, caching, memory pre-allocation etc.
Python memory manager also takes care of garbage collection by using Reference counting algorithm.
 
7. How will you perform Static Analysis on  a Python Script?
 
Ans : We can use Static Analysis tool called PyChecker for this purpose. PyChecker can detect errors in Python code.
PyChecker also gives warnings for any style issues.
Some other tools to find bugs in Python code are pylint and pyflakes.
 
8. What is the difference between a Tuple and List in Python?
 
Ans : In Python, Tuple and List are built-in data structures.

Some of the differences between Tuple and List are as follows:

Syntax: A Tuple is enclosed in parentheses:

E.g. myTuple = (10, 20, “apple”); A List is enclosed in brackets:

E.g. myList = [10, 20, 30];

Mutable: Tuple is an immutable data structure. Whereas, a List is a mutable data structure.

Size: A Tuple takes much lesser space than a List in Python.

Performance: Tuple is faster than a List in Python. So it gives us good performance.

Use case: Since Tuple is immutable, we can use it in cases like Dictionary creation. Whereas, a List is preferred in the use case where data can alter.

9. What is a Python Decorator?

Ans : A Python Decorator is a mechanism to wrap a Python function and modify its behavior by adding more functionality to it. We can use @ symbol to call a Python Decorator function.

10. How are arguments passed in a Python method? By value or by reference?

Ans : Every argument in a Python method is an Object. All the variables in Python have reference to an Object. Therefore arguments in Python method are passed by Reference.

Since some of the objects passed as reference are mutable, we can change those objects in a method. But for an Immutable object like String, any change done within a method is not reflected outside.

 

 

Leave a Comment

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

Scroll to Top