Page 131 - PYTHON-12
P. 131

59.  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.
            60.  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.
            61.  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.
            62.  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. “with” statement makes exception handling simpler by
               providing cleanup activities.
                General form of with:
                with open(“filename”, “mode”) as file-var:
                processing statements
            63.  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.
            64.  To make the changes made by any SQL Queries permanently in database, which function is used after
               execution of the query?
           Ans.  <connectionobject>.commit()
            65.  What do you understand by Python modules?
           Ans.  A file containing Python definitions and statements is called a Python module. So naturally, the filename is
               the module name which is appended with the suffix .py.
            66.  What do you understand by Python package?
           Ans.  Python package is a collection of modules in directories that gives a package hierarchy. More elaborately,
               Python packages are a way of structuring Python’s module by using “dotted module names”. So A.B actually
               indicates that B is a sub-module which is under a package named A.
            67.  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.
            68.  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.
            69.  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.

          A.26                                                                     Computer Science with Python–XII
   126   127   128   129   130   131   132   133   134