python基础 二月红
真心觉得python这门语言是业界良心:
- 列表、元组操作
- 字符串操作
- 字典操作
- 集合操作
- 文件操作
- 字符编码与转码
一、列表、元组操作
列表是最常用的数据类型之一,可实现最方便的存储和修改等操作。
定义列表:
>>> your_name=["xzx","mm","xmm"]
通过下标访问:
>>> your_name[0]
'xzx'
>>> your_name[2]
'xmm'
>>> your_name[-1]
'xmm'
>>> names[-2] #还可以倒着取
'mm'
切片:取多个元素:
>>> your_name=["xzx","mm","xmm","xiaoxia","miaoer","xiaxia"]
>>> your_name[1:4]#取下标1至4之间的数字,包括下标1,但不包括下标4
["mm","xmm","xiaoxia"]
>>> your_name[1:-1]
["mm","xmm","xiaoxia","miaoer"]
>>> your_name[:3]#从头开始可不用写0
["xzx","mm","xmm"]
>>> your_name[1:]#取到最后就什么都不写
["mm","xmm","xiaoxia","miaoer","xiaxia"]
>>> your_name[::2]#全部取只用写一个:不用写下标,最后的2表示取数下标每次加2
["xzx","xmm","miaoer"]
追加:
>>> your_name=["xzx","mm","xmm","xiaoxia","miaoer","xiaxia"]
>>> your_name.append("superxia")
>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia"]
插入:
>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia"]
>>> your_name.insert(2,"cutexia")#插入在2的位置
>>> your_name
["xzx","mm","cutexia","xmm","xiaoxia","miaoer","xiaxia","superxia"]
修改:
>>> your_name
["xzx","mm","cutexia","xmm","xiaoxia","miaoer","xiaxia","superxia"]
>>> your_name[2]="no_facexia"
>>> your_name
["xzx","mm","no_facexia","xmm","xiaoxia","miaoer","xiaxia","superxia"]
删除:
>>> your_name
["xzx","mm","no_facexia","xmm","xiaoxia","miaoer","xiaxia","superxia"]
>>> del your_name[2]
>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia"]
扩展:
>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia"]
>>> add_name=["haha","hehe","heiheihei"]
>>> your_name.extend(add_name)
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia","haha","hehe","heiheihei"]
复制:
>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer",