python基础--可变的数据类型:列表,字典
1、python列表list
# 列表 list = ['a', 'b', 'c'] for str in list: print(str) # append()方法来添加列表项 list2 = [] list2.append("hello") list2.append(" python") print(list2) # del:根据索引来删除列表的元素 del list2[1] print(list2) # len(列表):长度 print(len(list)) # 组合 print(list + list2) # 重复 print(list * 2) # 元素是否存在于列表中 if 'a' in list: print('list列表中包含有\'a\'')
pop:删除最后一个
remove(obj): 根据内容删除一个
del list[索引]: 根据索引删除
python列表的方法:https://www.runoob.com/python/python-lists.html

2、字典
字典:字典是另一种可变容器模型,格式 {key1 : value1, key2 : value2 }
# 字典
# 键一般是唯一的,如果重复最后的一个键值对会替换前面的,值不需要唯一。
# 值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
dict = {'a': 1, 'b': 2, 'b': '3'}
print(dict)
# 访问字典的值
print(dict['b'])
# 如果用字典里没有的键访问数据,出错
dict['a'] = 10 # 更新
print("dict['a']=%d"%dict['a'])
dict['c'] = 20
print("dict['c']=%d"%dict['c']) # 添加
del dict['a'] # 删除键是'Name'的条目
print(dict)
dict.clear() # 清空字典所有条目
print(dict)
del dict # 删除字典
字典内置的方法:


posted on 2019-10-08 00:02 wenbin_ouyang 阅读(185) 评论(0) 收藏 举报
浙公网安备 33010602011771号