31. What is the use of // operator in Python?
It can also be used floordiv(a,b). E.g.
10// 4 = 2
-10//4 = -3
32. What is a Module in Python?
Ans : A Module is a script written in Python with import statements, classes, functions etc. We can use a module in another Python script by importing it or by giving the complete namespace.
With Modules, we can divide the functionality of our application in smaller chunks that can be easily managed.
33. How can we create a dictionary with ordered set of keys in Python?
Ans : In a normal dictionary in Python, there is no order maintained between keys. To solve this problem, we can use OrderDict class in Python. This class is available for use since version 2.7.
It is similar to a dictionary in Python, but it maintains the insertion order of keys in the dictionary collection.
34. Python is an Object Oriented programming language or a functional programming language?
Ans : Python uses most of the Object Oriented programming concepts. But we can also do functional programming in Python. As per the opinion of experts, Python is a multi-paradigm programming language.
We can do functional, procedural, object-oriented and imperative programming with the help of Python.
35. How can we retrieve data from a MySQL database in a Python script?
Ans : To retrieve data from a database we have to make use of the module available for that database. For MySQL database, we import MySQLdb module in our Python script.
We have to first connect to a specific database by passing URL, username, password and the name of database.
Once we establish the connection, we can open a cursor with cursor() function. On an open cursor, we can run fetch() function to execute queries and retrieve data from the database tables.
36. What is the difference between append() and extend() functions of a list in Python?
Ans : 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.
37. How will you handle an error condition in Python code?
Ans : 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)
38. What is the difference between split() and slicing in Python?
Ans : 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
39. How will you check in Python, if a class is subclass of another class?
Ans : 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
40. How will you debug a piece of code in Python?
Ans : 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.