The setdefault() method gets a value, or sets it if missing:
d = {"a": 1}
x = d.setdefault("a", 10) # Key exists, returns 1
y = d.setdefault("b", 20) # Key missing, sets it to 20, returns 20
print(d) # {"a": 1, "b": 20}
This is useful when building dictionaries where you need a default value but also want to modify it.
Compare to get(): get() doesn't modify the dictionary, setdefault() does.