Python--字典、赋值符号的辨析、复合数据结构

沉潇先生 2019-01-24 PM 214℃ 2条

字典

  • 结构--字典名={字:解释,…}
  • 查字典--print(字典名[字])
>>> names={'Tom':'Tom is very tall','William':'William is very handsome'}
>>> names
{'Tom': 'Tom is very tall', 'William': 'William is very handsome'}
>>> print(names['Tom'])
Tom is very tall

也可用.get()

>>> names
{'Tom': 'Tom is very tall', 'William': 'William is very handsome'}
>>> print(names.get('Tom'))
Tom is very tall
>>> print(names.get('Mary'))
None

.get()自定义返回值

>>> names
{'Tom': 'Tom is very tall', 'William': 'William is very handsome'}
>>> print(names.get('Mary','No here'))
No here

===is辨析

  • =--赋值,给数据添加一个地址
  • ==--对数据进行比较
  • is--对数据地址进行比较
>>> a=[1,2,3,4,5,6]
>>> b=a
>>> c=[1,2,3,4,5,6]
>>> print(a==b)
True
>>> print(a==c)
True
>>> print(a is b)
True
>>> print(a is c)
False

复合数据结构

>>> people={'Tom':{'height':175,'weight':'80kg','color':'blue'},'Mary':{'height':180,'weight':'85kg','color':'black'},'William':{'height':165,'weight':'50kg','color':'green'}}
>>> Tom=people['Tom']
>>> Tom_color=people['Tom']['color']
>>> print(Tom)
{'height': 175, 'weight': '80kg', 'color': 'blue'}
>>> print(Tom_color)
blue

非特殊说明,本博所有文章均为博主原创。

评论啦~



已有 2 条评论


  1. 异星软件空间
    异星软件空间

    膜拜学习下!

    回复 2019-01-25 12:54
    1. 猫抓草
      猫抓草 博主

      你可是软件资源大佬啊

      回复 2019-01-25 13:28