Page 104 - PYTHON-12
P. 104
54. When should you use the ‘break’ statement in Python?
Ans. Python provides a break statement to exit a loop. Whenever the break hits the code, the control of the
program immediately exits from the body of the loop. The break statement in a nested loop causes the
control to exit from the inner iterative block.
55. What is a tuple in Python?
Ans. A tuple is a collection of type data structure in Python which is immutable. Tuples are similar to sequences
just like the lists. However, there are some differences between a tuple and a list—the former doesn’t allow
modifications but the latter does.
Also, tuples use parentheses for enclosing but lists have square brackets in their syntax.
56. Explain the following results retrieval methods with examples:
(i) fetchone() (ii) fetchall() (iii) fetchmany()
Ans. (i) fetchone(): The fetchone() method will return only one row from the result set in the form of tuple
containing a record.
(ii) fetchall(): The fetchall() method will return all the rows from the result set in the form of a tuple containing
the records.
(iii) fetchmany(n): The fetchmany(n) method will return only the specified n number of rows from the result
set in the form of a tuple containing the records.
57. Explain the use of ‘with’ statement.
Ans. In Python, generally ‘with’ statement is used to open a file, process the data present in the file, and also to
close the file without calling a close() method. The ‘with’ statement makes exception handling simpler by
providing cleanup activities.
The General form of with statement:
with open("filename", "mode") as file_var:
<processing statements>
58. Differentiate between append() and extend() methods.
Ans. Both append() and extend() methods are methods of list. These methods are used to add elements at the end
of the list.
• append(element) – adds the single element at the end of the list which has called this method.
• extend(another-list) – adds the elements of another list at the end of the list which is called the extend
method.
59. To make the changes made by any SQL Queries permanently in database, which function is used after
execution of the query?
Ans. <connectionobject>.commit()
60. How can we get current directory using Python?
Ans. To get current directory in Python, we need to use os module. Then, we can get the location of the current
directory by using getcwd() function.
61. What is a CSV file?
Ans. CSV (Comma Separated Values) is a file format used to store tabular data, such as spreadsheet or database
in plain text where values are separated by comma.
62. Differentiate between file mode ‘w’ and ‘a’ mode with respect to Python.
Ans. ‘w’ mode opens a file for writing only. It overwrites the file if the file exists, otherwise it creates a new file
for writing.
‘a’ mode opens a file for appending at the end of the existing file, otherwise it creates a new file for writing.
63. What is the difference between del keyword and clear() function?
Ans. The difference between del keyword and clear() function is that while del keyword removes one element at
a time, clear function removes all the elements.
64. Give the syntax of seek() method while working randomly with Python Files.
Ans. file_object.seek(offset [, reference_point])
A.32 Computer Science with Python–XII