Python Dictionary setdefault

Python setdefault function is one of the Dictionary methods 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 and the syntax of this dictionary setdefault function is:

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

Dictionary setdefault Example

It prints value at a given key. Please refer to the Dictionary to understand them in language.

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))
Dictionary setdefault function Example