Automation and Artificial Intelligence

A script runs the code below. What does it print? devices = {"R1": "10.0.0.1", "R2": "10.0.0.2"} for name in devices: print(name)

Show the answer

Correct answerD. R1 and R2, each on its own line

AWhy not this: Key-value pairs need devices.items() and unpacking; plain iteration gives keys only.

BWhy not this: The addresses are values; they print only if the loop uses devices.values().

CWhy not this: Dictionaries are iterable in Python; they do not raise an error in a for loop.

DWhy this is right: Looping over a dictionary yields its keys, so only the device names are printed.

Iterating over a Python dictionary with a for loop yields its keys, so name takes the values R1 and R2 and each is printed. Printing the IP addresses would require devices.values(), and printing pairs would require devices.items().