Python Programming - Standard Libraries

Exercise : Standard Libraries - General Questions
  • Standard Libraries - General Questions
86.
What is the purpose of the csv module?
Sorting elements in a list
Handling CSV (Comma-Separated Values) files
Performing mathematical operations
Implementing priority queues
Answer: Option
Explanation:
import csv

# Example usage of csv module for reading from a CSV file
with open('data.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)

87.
How can you use the xml.etree.ElementTree module in Python to parse an XML file?
xml.etree.ElementTree.parse(file_path)
xml.etree.ElementTree.load(file_path)
xml.etree.ElementTree.read(file_path)
xml.etree.ElementTree.fromfile(file_path)
Answer: Option
Explanation:
import xml.etree.ElementTree as ET

# Example usage of xml.etree.ElementTree module for parsing an XML file
tree = ET.parse('data.xml')
root = tree.getroot()
for element in root:
    print(element.tag, element.attrib)

88.
What is the purpose of the collections.defaultdict class?
Creating a dictionary with default values for keys
Sorting elements in a list
Performing arithmetic operations on collections
Implementing priority queues
Answer: Option
Explanation:
from collections import defaultdict

# Example usage of collections.defaultdict
default_dict = defaultdict(int)
default_dict['a'] += 1
default_dict['b'] += 2
print(default_dict)

89.
How can you use the platform module in Python to get the Python version information?
platform.version_info()
platform.python_version()
platform.info_python()
platform.get_python_version()
Answer: Option
Explanation:
import platform

# Example usage of platform module for getting Python version information
python_version = platform.python_version()
print(f"Python version: {python_version}")

90.
What does the codecs module in Python provide support for?
Working with binary data
Handling exceptions and errors
Encoding and decoding text in various formats
Sorting elements in a list
Answer: Option
Explanation:
import codecs

# Example usage of codecs module for encoding and decoding text
text = "Hello, World!"
encoded_text = codecs.encode(text, 'rot_13')
decoded_text = codecs.decode(encoded_text, 'rot_13')
print(decoded_text)