- 三元运算
result = 值1 if 条件 else 值2
如果条件为真:result = 值1
如果条件为假:result = 值2
- 列表、元组操作
定义列表和访问元素
>>> lists = ['wang', 'yu', 'fu'] >>> lists[0] 'wang' >>> lists[2] 'fu' >>> lists[-1] 'fu'
切片
>>> numbers = [1, 2, 3, 4, 5, 6] >>> numbers[1: 4] #顾头不顾尾 [2, 3, 4] >>> numbers[1:-1] [2, 3, 4, 5] >>> numbers[:3] [1, 2, 3]
追加
>>> numbers = [1, 2, 3, 4, 5, 6] >>> numbers.append(7) >>> numbers [1, 2, 3, 4, 5, 6, 7]
插入
>>> numbers [1, 2, 3, 4, 5, 6, 7] >>> numbers.insert(3,'插入') #下标前面插入 >>> numbers [1, 2, 3, '插入', 4, 5, 6, 7]
修改
>>> numbers [1, 2, 3, '插入', 4, 5, 6, 7] >>> numbers[3] = '修改' >>> numbers [1, 2, 3, '修改', 4, 5, 6, 7]
删除
>>> numbers [1, 2, 3, '修改', 4, 5, 6, 7] >>> del numbers[3] #删除下标 >>> numbers [1, 2, 3, 4, 5, 6, 7] >>> numbers [1, 2, 3, 4, 5, 6, 7] >>> numbers.remove(7) #删除值,只删除第一个匹配到的 >>> numbers [1, 2, 3, 4, 5, 6]
扩展
>>> numbers [1, 2, 3, 4, 5, 6] >>> numbers2=[7,8] >>> numbers.extend(numbers2) >>> numbers [1, 2, 3, 4, 5, 6, 7, 8]
拷贝
>>> numbers [1, 2, 3, 4, 5, 6] >>> numbers2=[7,8] >>> numbers.extend(numbers2) >>> numbers [1, 2, 3, 4, 5, 6, 7, 8]
统计
>>> numbers [1, 2, 3, 4, 5, 6, 7, 8] >>> numbers.count(7) 1
排序,方向排序
>>> numbers [1, 2, 3, 4, 5, 6, 7, 8] >>> numbers.sort() >>> numbers.reverse() >>> numbers [8, 7, 6, 5, 4, 3, 2, 1]
查看下标
>>> numbers [8, 7, 6, 5, 4, 3, 2, 1] >>> numbers.index(8) #只能匹配第一个查找到的 0
- 字符串
特性:不可修改
>>> name = 'wangyufu' >>> name.capitalize() #首字母大写 'Wangyufu' >>> name.casefold() #所以小写 'wangyufu' >>> name.center(15,'*') #居中,不足补齐 '****wangyufu***' >>> name.count('u') #统计出现过几次 2 >>> name.encode() #将字符串编码成bytes格式 b'wangyufu' >>> name.endswith('fu') #字符串的结尾是以fu True >>> name.find('w') #查找w,找到返回其索引, 找不到返回-1 0 format : >>> msg = "my name is {}, and age is {}" >>> msg.format("alex",22) 'my name is alex, and age is 22' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("alex",22) 'my name is 22, and age is alex' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=22,name="ale") 'my name is ale, and age is 22' format_map >>> msg.format_map({'name':'alex','age':22}) 'my name is alex, and age is 22' >>> '-1'.isdigit() #是否是数字 False >>> ','.join(['1','2']) #列表以字符格式化输出 '1,2' >>> 'Ab'.swapcase() #大小写转换 'aB' >>> 'a'.ljust(10,'-') #左对齐 'a---------' >>> 'a'.rjust(10,'-') #右对齐 '---------a'
- 字典
无序的,key唯一
>>> dics = {'name':'wang','age':18,'sex':'man'} >>> dics['zhuzhi'] = 'qiqihaer' #添加 >>> dics {'age': 18, 'sex': 'man', 'zhuzhi': 'qiqihaer', 'name': 'wang'} >>> dics['zhuzhi'] = 'beijing' #修改 >>> dics {'age': 18, 'sex': 'man', 'zhuzhi': 'beijing', 'name': 'wang'} >>> dics.pop('zhuzhi') #删除并打印 'beijing' >>> del dics['sex'] #删除 >>> dics.popitem() #随机删除 ('age', 18)
字典查找
>>> dics = {'name':'wang','age':18,'sex':'man'} >>> 'sex' in dics #字符是否在这个可迭代对象中 True >>> dics.get('name') #取值并打印 'wang' >>> dics['age'] #通过key为下标查找 18
迭代对象
>>> dics.values() #所有值 dict_values([18, 'man', 'wang']) >>> dics.keys() #所有key dict_keys(['age', 'sex', 'name']) >>> dics.items() #所以key和values dict_items([('age', 18), ('sex', 'man'), ('name', 'wang')]) >>> dics.fromkeys([1,3,4],'wang') #转换字典 {1: 'wang', 3: 'wang', 4: 'wang'}
- 文件操作
f=open('py3.txt', 'wt', encoding='utf8') #创建并打开写入 f.write('你好,我爱编程。') f.close() f=open('py3.txt', 'r', encoding='utf8') s = f.read() print(s) 你好,我爱编程。
w覆盖写入
r读
a追加
"U"是将换行符转成
"b"是打开二进制文件