Exercise: Python

Questions for: Standard Libraries

What is the purpose of the socket module?
A:
Sorting elements in a list
B:
Handling exceptions and errors
C:
Working with cryptographic hash functions
D:
Implementing network communication
Answer: D
import socket

# Example usage of socket module for creating a simple server
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 8080))
server_socket.listen(1)
connection, address = server_socket.accept()
print(f"Connected by {address}")
How can you use the sysconfig module in Python to get information about the Python installation?
A:
sysconfig.get_info()
B:
sysconfig.info()
C:
sysconfig.get_config()
D:
sysconfig.config()
Answer: C
import sysconfig

# Example usage of sysconfig module for getting information about the Python installation
config_info = sysconfig.get_config()
print(config_info)
How can you use the subprocess module in Python to run an external command and capture its output?
A:
subprocess.run(command, capture_output=True)
B:
subprocess.execute(command, capture=True)
C:
subprocess.call(command, capture=True)
D:
subprocess.execute(command, capture_output=True)
Answer: A
import subprocess

# Example usage of subprocess module for running an external command and capturing its output
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print(result.stdout)
How can you use the unicodedata module in Python to get the name of a Unicode character?
A:
unicodedata.char_name(char)
B:
unicodedata.get_char_name(char)
C:
unicodedata.name(char)
D:
unicodedata.get_name(char)
Answer: C
import unicodedata

# Example usage of unicodedata module for getting the name of a Unicode character
char_name = unicodedata.name('A')
print(char_name)
What does the zoneinfo module in Python provide support for?
A:
Handling time zones and daylight saving time
B:
Generating random numbers
C:
Sorting elements in a list
D:
Parsing JSON data
Answer: A
from datetime import datetime
from zoneinfo import ZoneInfo

# Example usage of zoneinfo module for handling time zones
dt = datetime(2022, 1, 1, tzinfo=ZoneInfo("America/New_York"))
print(dt)
Ad Slot (Above Pagination)
Quiz