一个专注于计算机编程和硬件学习的博客主
24/1
2019

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

字典

  • 结构--字典名={字:解释,…}
  • 查字典--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. 膜拜学习下!

    1. 你可是软件资源大佬啊

添加新评论