Python Dictionary setdefault

Python Dictionary setdefault function is one of a Dictionary function used to print the value at a given key position. If there is no value at the given index, it prints None. Here, we discuss the use of this Dictionary setdefault function. The syntax of this Python dictionary setdefault function is:

dictionary_name.setdefault(key, None) - None is default and optional

Python Dictionary setdefault Example

The Dictionary setdefault function print value at a given key.

TIP: Please refer to theDictionary to understand Python Dictionaries.

# Python Dictionary setdefault Example

emp = {'name': 'Kevin', 'age': 25 , 'Sal': 725000}
print("Dictionary: ", emp)

# Print Value using Keys
print("\nDictionary Value: ", emp.setdefault('age', None))
print("\nDictionary Value: ", emp.setdefault('job', None))
print("\nDictionary Value: ", emp.setdefault('sex', None))
print("\nDictionary Value: ", emp.setdefault('name', None))
Python Dictionary setdefault Example