String模块中的常量:
string.digits:数字0~9
string.letters:所有字母(大小写)
string.lowercase:所有小写字母
string.printable:可打印字符的字符串
string.punctuation:所有标点
string.uppercase:所有大写字母
list的常见操作
创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];
list拷贝
list1 = list2
- 真正的copy,通过
slice
切片的方式进行copy
>>> list2 = list1[:]
>>> list2
[789, 555]
>>> list1
[789, 555]
>>> list1.pop()
555
>>> list1
[789]
>>> list2
[789, 555]
sort
>>> list1
[123, 555, 789, 123]
>>> list1.sort()
>>> list1
[123, 123, 555, 789]
>>> list1.sort(reverse = True)
>>> list1
[789, 555, 123, 123]
>>>
reverse逆转前后元素顺序倒过来
>>> list1
[123, 555, ['hello', 'world'], 789, 123]
# 删除元素
>>> list1.remove(list1[2])
>>> list1
[123, 555, 789, 123]
>>> list1.reverse()
>>> list1
[123, 789, 555, 123]
index
- 某个元素在list中第一次出现的位置
>>> list1
[123, 555, ['hello', 'world'], 789, 123]
>>> list1.index(123)
0
- 指定某个区间出现的位置
>>> list1
[123, 555, ['hello', 'world'], 789, 123]
>>> list1.index(789, 2, 4)
3
count
- 某个元素在list中出现的频率次数
[123, 555, ['hello', 'world'], 789, 123]
>>> list1.count(123)
2
列表拼接 +只能用来两个列表拼接
类似extend
方法
>>> ls1
[3, 4, 5]
>>> ls2
[3, 4]
>>> ls3 = ls1 + ls2
>>> ls3
[3, 4, 5, 3, 4]
列表中常见得增,删除,修改,查询
1:增加
name = [1,2,3,4]
append:添加到列表得末尾 name.append(5)
insert:按下标增加 name.insert(3)
2:删除
pop:默认删除最后一位 name.pop()
del:按下标删 del name[1]
3:修改
按下标修改 name[0] = 6
4:查询
name[0]
>>>1
查询下标
name.index(1)
>>>0
清空列表操作
name.clear()
字符串常见操作
print(file_name.endswith('.png'))#是否以x结尾
name.format(name='niuniu',age=18)#这个是格式字符串
print('122'.isdigit())#是否是数字
print(''.join(['hehe','haha','ee']))#拼接字符串
1、把list转成字符串
2、把list里面每个元素连起来
print('adbefF'.lower())#变成小写
print('adbefF'.upper())#变成大写
print('\nmysql \n'.strip())#
print('mysql is db.'.replace('mysql', 'oracle'))
print('1+2+3+4'.split('+')) # 切割字符串,返回一个list
print('1+2+3\n1+2+3+4'.splitlines()) # 按照换行符分割,返回一个listpython字典操作
字典操作
字典,一种key-value的数据类型,使用就行我们上学用的字典,通过笔划、字母来查对应页的详细内容。
字典是无序的,它没有下标
字典操作
语法:
info = {
'stu1101':"TengLan Wu",
'stu1102':"LongZe LuoLa",
'stu1103':"XiaoZe MaLiya",
}
修改:
info["stu1101"] ="武藤兰" #修改
info["stu1104"] ="CangJinkong" #如果key不存在就创建
删除:
del info["stu1101"] #删除
info.pop("stu1101") #删除
info.popitem() #随机删除
查找:
D.clear() #移除D中的所有项
D.copy() #返回D的副本
D.fromkeys(seq[,val]) #返回从seq中获得的键和被设置为val的值的字典。可做类方法调用
D.get(key[,default]) #如果D[key]存在,将其返回;否则返回给定的默认值None
D.has_key(key) #检查D是否有给定键key
D.items() #返回表示D项的(键,值)对列表
D.iteritems() #从D.items()返回的(键,值)对中返回一个可迭代的对象
D.iterkeys() #从D的键中返回一个可迭代对象
D.itervalues() #从D的值中返回一个可迭代对象
D.keys() #返回D键的列表
D.pop(key[,d]) #移除并且返回对应给定键key或给定的默认值D的值
D.popitem() #从D中移除任意一项,并将其作为(键,值)对返回
D.setdefault(key[,default]) #如果D[key]存在则将其返回;否则返回默认值None
D.update(other) #将other中的每一项加入到D中。
D.values() #返回D中值的列表
字典,文件常见操作补充:
1、flush()可以理解为刷新内存
2、setdefault()在字典中添加字典
3、truncate()清空字典
Python算术运算符
以下假设变量: a=10,b=20:
运算符 | 描述 | 实例 |
+ | 加 - 两个对象相加 | a + b 输出结果 30 |
- | 减 - 得到负数或是一个数减去另一个数 | a - b 输出结果 -10 |
* | 乘 - 两个数相乘或是返回一个被重复若干次的字符串 | a * b 输出结果 200 |
/ | 除 - x除以y | b / a 输出结果 2 |
% | 取模 - 返回除法的余数 | b % a 输出结果 0 |
** | 幂 - 返回x的y次幂 | a**b 为10的20次方, 输出结果 100000000000000000000 |
// | 取整除 - 返回商的整数部分(向下取整) | 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0 |