Dictionary Keys

Dictionaries
Β 
do
Β 
not
Β 
haveΒ 
indexing,Β 
butΒ 
theyΒ 
haveΒ 
keys,Β 
whichΒ 
areΒ 
veryΒ 
similarΒ 
toΒ 
indexing.Β 
AddingΒ 
valuesΒ 
toΒ 
aΒ 
dictionaryΒ 
canΒ 
beΒ 
doneΒ 
inΒ 
theΒ 
sameΒ 
wayΒ 
usingΒ 
keysΒ 
likeΒ 
itΒ 
isΒ 
doneΒ 
withΒ 
indexing.Β 
IfΒ 
youΒ 
useΒ 
theΒ 
sameΒ 
keyΒ 
againΒ 
andΒ 
giveΒ 
anotherΒ 
value,Β 
itΒ 
willΒ 
remove
Β 
theΒ 
oldΒ 
valueΒ 
andΒ 
storeΒ 
theΒ 
newΒ 
valueΒ 
forΒ 
theΒ 
key.
dict_name[key] = value
dict_1 = {}
#empty dictionary
dict_1['mobile'] = 'iPhone'
print(dict_1)
dict_1['mobile'] = 'OnePlus'
print(dict_1)

Check if a Key is present

The
Β 
'in'
Β 
featureΒ 
canΒ 
beΒ 
usedΒ 
toΒ 
checkΒ 
whetherΒ 
aΒ 
keyΒ 
isΒ 
presentΒ 
inΒ 
aΒ 
Dictionary
.
AsΒ 
itΒ 
isΒ 
aΒ 
conditionalΒ 
statement,Β 
ifΒ 
theΒ 
keyΒ 
isΒ 
presentΒ 
itΒ 
returnsΒ 
TrueΒ 
elseΒ 
itΒ 
returnsΒ 
False
.
print(key in dict_name)
dict_1 = { 'Theo' : 7717234567, 'Zo' : 8211132111 }
print('Zo' in dict_1)
print('Tyra' in dict_1)