Avatto>>DATA SCIENTIST>>SHORT QUESTIONS>>Python Interview Questions
In Python, we can use the debugger pdb for debugging the code. To start debugging we have to enter following lines on the top of a Python script.
import pdb
pdb.set_trace()
After adding these lines, our code runs in debug mode. Now we can use commands like breakpoint, step through, step into etc for debugging.
Python provides a useful method issubclass(a,b) to check whether class a is a subclass of b.
E.g. int is not a subclass of long
>>> issubclass(int,long)
False
bool is a subclass of int
>>> issubclass(bool,int)
True
Both split() function and slicing work on a String object. By using split() function, we can get the list of words from a String.
E.g. 'a  b c '.split() returns [‘a’, ‘b’, ‘c’]
Slicing is a way of getting substring from a String. It returns another String.
E.g. >>> 'a b c'[2:3] returns b
We can implement exception handling to handle error conditions in Python code. If we are expecting an error condition that we cannot handle, we can raise an error with appropriate message.
E.g. >>> if student_score < 0: raise ValueError(“Score can not be negative”)

If we do not want to stop the program, we can just catch the error condition, print a message and continue with our program.
E.g. In following code snippet we are catching the error and continuing with the default value of age.

#!/usr/bin/python
try:
age=18+'duration'
except:
print("duration has to be a number")
age=18
print(age) Powerhouse, Knowledge. Top 50 Python Interview Questions and Answers . Kindle Edition.
In Python, we get a built-in sequence called list. We can call standard functions like append() and extend() on a list.
We call append() method to add an item to the end of a list.
We call extend() method to add another list to the end of a list.
In append() we have to add items one by one. But in extend() multiple items from another list can be added at the same time.


Description