Page 103 - PYTHON-12
P. 103
40. What are the different types of operators in Python?
Ans. Following is a list of operators in Python:
• Arithmetic Operators • Relational Operators • Assignment Operators
• Logical Operators • Membership Operators • Identity Operators
41. What is a Dictionary in Python?
Ans. Dictionary is an important built-in data type in Python. It defines one-to-one relationship between keys and
values. Dictionaries contain a pair of keys and their corresponding values.
Dictionaries are indexed by keys.
42. What is the use of help() function in Python?
Ans. The help() function is used to display the documentation string and also enables us to see the help related to
modules, keywords, attributes, etc.
43. Which package must be imported in Python to create a database connectivity application?
Ans. mysql.connector
44. Which function will return all rows from the ResultSet in the form of tuple containing records?
Ans. fetchall()
45. Write a statement to import module for MySQL connectivity with Python.
Ans. import mysql.connector
46. In the stack, if a user tries to remove element from the empty stack, what is the situation called?
Ans. Underflow of Stack
47. How does Python do compile-time and run-time code checking?
Ans. In Python, some amount of coding is done at compile-time but most of the checking such as type, name, etc.,
is held up until the code execution. Consequently, if the Python code references a user-defined function that
does not exist, the code will compile successfully. The Python code will fail only with an exception when the
code execution path does not exist.
48. Explain the use of try, except, raise and finally blocks.
Ans. The try, except and finally blocks are used in Python error-handling mechanism. Code is executed in the try
block until an error occurs. The except block is used to receive and handle all errors. Control is transferred to
the appropriate except block. In all cases, the finally block is executed. The raise may be used to raise your
own exceptions.
49. What is the purpose of PYTHONPATH environment variable?
Ans. PYTHONPATH has a role similar to PATH. This variable tells the Python interpreter where to locate the module
files imported into a program. It should include the Python source library directory and the directories
containing Python source code.
50. What is the difference between lists and tuples?
Ans.
Lists Tuples
Lists are mutable, i.e., they can be edited. Tuples are immutable (tuples are lists which can’t be edited).
Lists are slower than tuples. Tuples are faster than lists.
Syntax: Syntax:
list1 = [10, 'Python', 44.5] tup1 = (10, 'Python', 44.5)
51. How will you reverse a list?
Ans. list.reverse() − Reverses items of list in place.
52. What is a string in Python?
Ans. A string in Python is a sequence of alphanumeric characters enclosed in single and double quotes. They are
immutable objects. It means that they don’t allow modification once they are assigned a value. Python
provides several methods such as join(), replace() or split() to alter strings.
53. Why is the return keyword used in Python?
Ans. The purpose of a function is to receive the inputs and return some output. The return is a Python statement
which we can use in a function for sending a value back to its calling function.
Appendices A.31