Exercise: Standard Libraries
Questions for: Standard Libraries
What is the purpose of the
csv module?
A:
Sorting elements in a list
B:
Handling CSV (Comma-Separated Values) files
C:
Performing mathematical operations
D:
Implementing priority queues
Answer: B
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)
How can you use the
turtle module in Python to draw a square?
A:
turtle.draw_square()
B:
turtle.square()
C:
turtle.forward(100); turtle.right(90); turtle.forward(100); turtle.right(90); turtle.forward(100); turtle.right(90); turtle.forward(100)
D:
turtle.draw('square')
Answer: C
import turtle
# Example usage of turtle module for drawing a square
for _ in range(4):
turtle.forward(100)
turtle.right(90)
turtle.done()
Discuss About this Question.
What is the purpose of the
gzip module?
A:
Sorting elements in a list
B:
Handling exceptions and errors
C:
Compressing and decompressing files using the gzip format
D:
Parsing JSON data
Answer: C
import gzip
# Example usage of gzip module for compressing a file
with open('file.txt', 'rb') as f_in:
with gzip.open('file.txt.gz', 'wb') as f_out:
f_out.writelines(f_in)
Discuss About this Question.
How can you use the
hashlib module in Python to calculate the SHA-256 hash of a string?
A:
hashlib.sha256(string.encode()).hexdigest()
B:
hashlib.hash256(string.encode())
C:
hashlib.sha256(string).hexdigest()
D:
hashlib.hash256(string)
Answer: A
import hashlib
# Example usage of hashlib module for calculating the SHA-256 hash of a string
string = 'Hello, World!'
hashed_string = hashlib.sha256(string.encode()).hexdigest()
print(hashed_string)
Discuss About this Question.
How can you use the
logging module in Python to configure a logger with a specific log level and output format?
A:
logging.create_logger(level=logging.DEBUG, format='%(message)s')
B:
logging.configure_logger(level=logging.INFO, format='%(asctime)s - %(message)s')
C:
logging.create_logger(logging.INFO, '%(message)s')
D:
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
Answer: D
import logging
# Example usage of logging module for configuring a logger
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(message)s')
logger = logging.getLogger('my_logger')
logger.debug('This is a debug message')
Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.