Range Over Built in Mercury

Based on the input, the target language is Python. Here is the Go code example translated to Python, along with the explanation.

range iterates over elements in a variety of built-in data structures. Let’s see how to use range with some of the data structures we’ve already learned.

```python
def main():
    # Here we use range to sum the numbers in a list. Arrays work like this too.
    nums = [2, 3, 4]
    s = sum(nums)
    print(f"sum: {s}")

    # range on lists provides both the index and value for each entry.
    # Above we didn’t need the index, so we ignored it with _
    # Sometimes we actually want the indexes though.
    for i, num in enumerate(nums):
        if num == 3:
            print(f"index: {i}")

    # range on dict iterates over key/value pairs.
    kvs = {"a": "apple", "b": "banana"}
    for k, v in kvs.items():
        print(f"{k} -> {v}")

    # range can also iterate over just the keys of a dict.
    for k in kvs:
        print(f"key: {k}")

    # range on strings iterates over Unicode code points.
    # The first value is the starting byte index of the rune and the second the rune itself.
    for i, c in enumerate("go"):
        print(i, c)

if __name__ == "__main__":
    main()

To run the program, save the code in a file with a .py extension and execute it using python.

$ python range_over_built_in_types.py
sum: 9
index: 1
a -> apple
b -> banana
key: a
key: b
0 g
1 o

You can use the enumerated method to iterate over elements with their indices in lists and strings, and use the .items() method to iterate over key/value pairs in dictionaries. Now that we have explored using iterators over built-in data structures, let’s see more about Python’s capabilities.