Exercise: Python

Questions for: Standard Libraries

What is the purpose of the zlib module?
A:
Sorting elements in a list
B:
Handling exceptions and errors
C:
Compressing and decompressing data using the zlib format
D:
Parsing JSON data
Answer: C
import zlib

# Example usage of zlib module for compressing and decompressing data
data = b'This is some data to be compressed.'
compressed_data = zlib.compress(data)
decompressed_data = zlib.decompress(compressed_data)
print(decompressed_data.decode('utf-8'))
How can you use the tempfile module in Python to create a temporary file?
A:
tempfile.create_temp()
B:
tempfile.new()
C:
tempfile.mktemp()
D:
tempfile.TemporaryFile()
Answer: D
import tempfile

# Example usage of tempfile module for creating a temporary file
with tempfile.TemporaryFile() as temp_file:
    temp_file.write(b'This is a temporary file content.')
    temp_file.seek(0)
    content = temp_file.read()
    print(content)
What does the codecs module in Python provide support for?
A:
Working with binary data
B:
Handling exceptions and errors
C:
Encoding and decoding text in various formats
D:
Sorting elements in a list
Answer: C
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)
How can you use the platform module in Python to get the Python version information?
A:
platform.version_info()
B:
platform.python_version()
C:
platform.info_python()
D:
platform.get_python_version()
Answer: B
import platform

# Example usage of platform module for getting Python version information
python_version = platform.python_version()
print(f"Python version: {python_version}")
How can you use the xml.etree.ElementTree module in Python to parse an XML file?
A:
xml.etree.ElementTree.parse(file_path)
B:
xml.etree.ElementTree.load(file_path)
C:
xml.etree.ElementTree.read(file_path)
D:
xml.etree.ElementTree.fromfile(file_path)
Answer: A
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)
Ad Slot (Above Pagination)
Quiz