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

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

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.