Maps in Fortress
Based on the provided input, the target language specified in fortress
is Python. Here is the translated code and explanation formatted in Markdown suitable for Hugo:
Maps are Python’s built-in associative data type (sometimes called dictionaries).
To create an empty dictionary, use the dict
constructor:
Set key/value pairs using typical name[key] = val
syntax.
Printing a dictionary with the print()
function will show all of its key/value pairs.
Get a value for a key with name[key]
.
If the key doesn’t exist, a KeyError
is raised.
The built-in len()
function returns the number of key/value pairs when called on a dictionary.
To remove a key/value pair from a dictionary, use the del
statement.
To remove all key/value pairs from a dictionary, use the clear
method.
The optional second return value when getting a value from a dictionary using the get
method indicates if the key was present in the dictionary by returning None
if it wasn’t found. Alternatively, you can check for the key using the in
keyword.
You can also declare and initialize a new dictionary in the same line with this syntax.
Python’s collections
module provides a number of useful utility functions for dictionaries.
Note that dictionaries appear in the form {'k1': 7, 'k2': 13}
when printed with print()
.