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?
# -*- coding: encoding -*-
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.