字典
字典,本质是无序的键值对
1. 我们可以通过{}、dict()来创建字典对象
>>> a = {'name':'sxl'}
>>> b = dict(name = 'sxl')
>>> a.get('name')
'sxl'
>>> b.get('name')
'sxl'
2. 通过 zip()创建字典对
- 通过 fromkeys 创建值为空的字
>>> a['name']
'sxl'
>>> a.get('age','不存在')
'不存在'
>>> a.items()
dict_items([('name', 'sxl')])
>>> a.keys()
dict_keys(['name'])
>>> a.values()
dict_values(['sxl'])
>>> a['age']=21
>>> a
{'name': 'sxl', 'age': 21}
>>> b = dict(name = 'yq',age = 21)
>>> a.update(b)
>>> a
{'name': 'yq', 'age': 21}
>>> del(a['name'])
>>> a
{'age': 21}
t1 = dict(name = 'sxl', age = 21, sex = 'n')
t2 = dict(name = 'yq', age = 21, sex = 'l')
t = [t1,t2]
print(t1.get('name'))
for i in range(len(t)):
print(t[i].get('name'))
集合
元素不能重复
>>> a = {2,3,4}
>>> a.add(5)
>>> a
{2, 3, 4, 5}
>>> a.remove(4)
>>> a
{2, 3, 5}
结构
num = input("输入一个数字:")
if int(num)<10:
print(num)
else:
print('大于10')
|