Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active Jul 11, 2020
Embed
What would you like to do?
#setdefault - if key is not there , will add the key with value none
person = {'name': 'karthi', 'age': 7}
city = person.setdefault('city')
print(person)
#Output:{'name': 'karthi', 'age': 7, 'city': None}
#setdefault- if key is not there , will add key and value specified
person = {'name': 'karthi', 'age': 7}
city = person.setdefault('city','chennai')
print(person)
#Output:{'name': 'karthi', 'age': 7, 'city': 'chennai'}
#setdefault-> if key exists, returns the value of that specified key
person = {'name': 'karthi', 'age': 7}
age=person.setdefault('age',10)
print(person.setdefault('age',10)) #Output:7
print (person)
#Output:{'name': 'karthi', 'age': 7}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment