91. Can we use singleton functionality without making a singleton class in Python?
A module with functions (and not a class) would serve well as a singleton. All its variables would be bound to the module, which could not be instantiated repeatedly anyways.
92. Does python support database programming?
Yes
93. What is MySQLdb?
MySQLdb is an interface for connecting to a MySQL database server from Python.
It implements the Python Database API v2.0 and is built on top of the MySQL C API.
94. How would you check if MySQLdb is installed?
Try importing it with import MySQLdb. An error would indicate that it’s not installed.
95. Write a script to connect to MySql database using Python.
#!/usr/bin/python
importMySQLdb
# Open database connection
db =MySQLdb.connect(“localhost”,”username”,”password”,”databasename” )
Prepare a cursor object using cursor () method cursor =db.cursor()
execute SQL query using execute() method. cursor.execute(“SELECT VERSION()”)
Fetch a single row using fetchone() method. data =cursor.fetchone()
print”Database version : %s “%data
disconnect from server db.close()
If a connection is established with the datasource, then a Connection Object is returned and saved into db for further use, otherwise db is set to None. Next, db object is used to create a cursor object, which in turn is used to execute SQL queries. Finally, before coming out, it ensures that database connection is closed and resources are released.
96. How do you disconnect from the database?
Use the close() method. db.close() closes the connection from the database like in the script above.
97. Does Python support enums?
Python 3.4 does. Earlier versions of Python dont.
98. How do you use enums in Python?
The following code would do.
from enum import Enum
Game=Enum(‘Game’,’hockey football rugby’
99. Booleans have 2 possible values. Are there types in python that have 3 possible values?
Yes. This functionality can be achieved by enums. refer to the example in previous enum question.
100. What is the output of print str + “TEST” if str = ‘Hello World!’?
It will print concatenated string. Output would be Hello World!TEST.