For unimportant information in the dictionary , We can delete it , In general use del sentence , Use del Statement must specify the dictionary name and the key to delete .
for example
alien_0={
'color':'green','points':5}
print(alien_0)
del alien_0['points']
print(alien_0)
Code will key ‘points’ Delete from the dictionary , Also delete the value associated with this key , After output :
{
'color':'green','ponits':5}
{
'color':'green'}
If we have a large number of surveys, we have to output them to the dictionary , We'd better make it uniform . When we ask different people what language they like, the results will be different , So there will be a large amount of data .
favourite_language={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil';:'python',
}
language=favourite_languages['sarah'].title()
print(f"sarah's favorite language is {
language}.")
Sarah'sfavorite language is c
This format is very convenient and concise , We entered Sarah Then he gave his favorite language .
If the key we specified does not exist , Then something goes wrong .
for example
alien_0={
'color':'green'}
print(alien_0['ponits'])
This will display an error indicating that the key value is wrong .
We can use get() Returns a default value when the specified key does not exist .
for example
alien_0={
'color':'green'}
point=alien_0.get('points','no points')
print(ponit)
If there is in the dictionary points And you get the relevant value , without , Then you will get his default value , It doesn't cause mistakes .
no points