Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active July 11, 2020 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/09a2f64f6f0cb04ce1f43f72805f6a8b to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/09a2f64f6f0cb04ce1f43f72805f6a8b to your computer and use it in GitHub Desktop.
#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