``````

In [1]:

d = {'k1': 1, 'k2': 2}

``````
``````

In [2]:

d.setdefault('k3', 3)
print(d)

``````
``````

{'k1': 1, 'k2': 2, 'k3': 3}

``````
``````

In [3]:

d.setdefault('k4')
print(d)

``````
``````

{'k1': 1, 'k2': 2, 'k3': 3, 'k4': None}

``````
``````

In [4]:

d.setdefault('k1', 100)
print(d)

``````
``````

{'k1': 1, 'k2': 2, 'k3': 3, 'k4': None}

``````
``````

In [5]:

print(d.setdefault('k5', 5))

``````
``````

5

``````
``````

In [6]:

print(d)

``````
``````

{'k1': 1, 'k2': 2, 'k3': 3, 'k4': None, 'k5': 5}

``````
``````

In [7]:

print(d.setdefault('k6'))

``````
``````

None

``````
``````

In [8]:

print(d)

``````
``````

{'k1': 1, 'k2': 2, 'k3': 3, 'k4': None, 'k5': 5, 'k6': None}

``````
``````

In [9]:

print(d.setdefault('k1', 100))

``````
``````

1

``````
``````

In [10]:

print(d.setdefault('k1', -100))

``````
``````

1

``````
``````

In [11]:

print(d.setdefault('k1'))

``````
``````

1

``````
``````

In [12]:

print(d)

``````
``````

{'k1': 1, 'k2': 2, 'k3': 3, 'k4': None, 'k5': 5, 'k6': None}

``````