This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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