程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

0 basic learning python (17)

編輯:Python

Delete key value pair

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 .

Use get To access the value

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved