Maps in Logo

Based on the input provided, I’ll extract the relevant information and translate the Go code example to Python.


Maps are the built-in associative data type (sometimes called hashes or dicts in other languages).

# Creating an empty dictionary
m = {}

# Setting key/value pairs using typical syntax
m["k1"] = 7
m["k2"] = 13

# Printing a dictionary will show all of its key/value pairs
print("map:", m)

# Getting a value for a key
v1 = m["k1"]
print("v1:", v1)

# If the key doesn’t exist, the zero value of the value type is returned
v3 = m.get("k3", 0)
print("v3:", v3)

# Getting the number of key/value pairs
print("len:", len(m))

# Removing key/value pairs from a dictionary
del m["k2"]
print("map:", m)

# Clearing a dictionary
m.clear()
print("map:", m)

# The optional second return value indicates if the key was present in the dictionary
prs = "k2" in m
print("prs:", prs)

# Declaring and initializing a new dictionary in the same line
n = {"foo": 1, "bar": 2}
print("map:", n)

# Using a dictionary utility function
n2 = {"foo": 1, "bar": 2}
if n == n2:
    print("n == n2")

To run the program, save the code in a file, say maps_example.py and use the Python interpreter to execute it.

$ python maps_example.py
map: {'k1': 7, 'k2': 13}
v1: 7
v3: 0
len: 2
map: {'k1': 7}
map: {}
prs: False
map: {'foo': 1, 'bar': 2}
n == n2

Note that dictionaries appear in the form {'k': 'v', 'k': 'v'} when printed with the print function.

Now that we can run and build basic Python programs, let’s learn more about the language.

Next example: Functions.