第1章 基本数据类型
1.1 字符串类-str
- 1. captialize首字母大写
name = 'alex'
v=name.capitalize()
print(v)
- 2. casefold 所有字母大写变小写,支持各种语言
name = 'Alex'
v=name.casefold()
print(v)
3. lower 所有字母大写变小写
name = 'Alex'
v=name.lower()
print(v)
- 3. center 文本居中
参数1:20表示总长度
name = 'Alex'
v=name.center(20)
print(v)
参数2:填充的字符Y
name = 'Alex'
v=name.center(20,'Y')
print(v)
- 4. count 传入值在字符串中出现的次数
参数1:要查找的值(子序列)
name = 'Alexaaaaafsssswwww'
v=name.count('a')
print(v)
参数2:起始位置(索引)
name = 'Alexaaaaafsssswwww'
v=name.count('a',6)
print(v)
参数3:结束位置(索引)
name = 'Alexaaaaafsssswwww'
v=name.count('a',6,8)
print(v)
- 5. endwith 是否以...结尾,输出True或false
name = 'Alexaaaaafsssswwww'
v=name.endswith('w')
print(v)
- 6. startswith 是否以...开头,输出True或false
name = 'Alexaaaaafsssswwww'
v=name.startswith('A')
print(v)
- 7. expandtabs找到制表符\t.进行替换(包含前面的值),20为空格数量
name = 'al\te\tx\ntuu\tkkk'
v = name.expandtabs(20)
print (v)
- 8. find找到制定子序列的索引位置:不存在返回-1,不会报错
name = 'Alex'
v = name.find('A')
print (v)
- 9. index找到制定子序列的索引位置:不存在则报错
name = 'Alex'
v = name.index('A')
print (v)
10. format字符串格式化
tpl = '我是:{name};年龄:{age};性别{gender}'
v = tpl.format (name='杨旭',age='31',gender='男')
print (v)
11. isalnum判断是否包含数字汉字等普通字符
name = 'alex9汉字'
v = name.isalnum()
12. isalpha 判断是否全是字母
name = 'alex'
v = name.isalpha()
print (v)
13. isdecimal,isdigit,isnumeric 判断是否为数字
num = '2'
v1 = num.isdecimal()
v2 = num.isdigit()
v3 = num.isnumeric()
print (v1,v2,v3)
14. isidentifier是否是表示符
n = 'name'
v = n.isidentifier()
print (v)
15. encode格式化文字编码
name = 'alex'
v = name.encode(encoding='utf-8')
print (v)
16. islower,isupper判断是否全都是大写,小写字母
n = 'name'
v = n.islower()
print (v)
n = 'name'
v = n.isupper()
print (v)
17. upper,lower全部变大写,小写字母
n = 'name'
v = n.lower()
print (v)
18. isprintable查看是否包含隐含的字符串,如果没有,则True
name = '钓鱼要掉刀鱼,刀鱼要到岛上掉'
v = name.isprintable()
print (v)
19. isspace是否全都是空格
name = ' 钓鱼要掉刀鱼,刀鱼要到岛上掉'
v = name.isspace()
print (v)
20. join元素拼接 #内部循环每个元素
字符串拼接:
name = 'Alex'
v = '_'.join(name)
print (v)
列表拼接:
name_list = ['海风','岗酿','理解']
v = '搞'.join(name_list)
print (v)
21. rjust,ljust左右填充 #20表示一共占位20个字符
name = 'alex'
v = name.rjust(20,'#')
print (v)
22. maketrans对应关系转换
m = str.maketrans('abcdef','123456')
name = 'asdjnkjhcasdqwsadawdfg'
v = name.translate(m)
print (v)
23. partition分割并保留分割的元素
content = '李泉SB刘康SB刘一'
v = content.partition('SB')
print (v)
24. replace替换
content = '李泉SB刘康SB刘一'
v = content.replace('SB','LOVE')
print (v)
25. 移除空白,\n,\t,自定义
name = 'alex\t'
v = name.strip()
print (v)
26. swapcase大小写转换
name = 'alex'
v = name.swapcase()
print (v)
27. zfill填充0
name = 'alex'
v = name.zfill(20)
print (v)
28. 额外功能
name = "alex"
name[0]
name[0:3]
name[0:3:2]
len(name)
#for循环,每个元素是字符
1.2 整数类- int
- 1. bit_length 当前整数的二进制表示的最少位数
age = 4
print (age.bit_length())
- 2. to_butes 获取当前数据的字节表示
age = 15
v = age.to_bytes(10,byteorder='big')
# v = age.to_bytes(10,byteorder='little')
print (v)
1.3 布尔值-bool
v = 0 # 1,-1
v = ""
v = []
--> 空内容:False
1.4 列表类-list:可变类型
user_list = ['李泉','刘一','刘康','豆豆','小龙'] # 可变类型
- 1. append追加
user_list = ['李泉','刘一','刘康','豆豆','小龙']
user_list.append('杨旭')
print (user_list)
- 2. copy浅拷贝
user_list = ['李泉','刘一','刘康','豆豆','小龙']
v = user_list.copy()
print (v)
- 3. clear清空列表-原列表被清空
user_list = ['李泉','刘一','刘康','豆豆','小龙']
v = user_list.clear()
print (v)
print (user_list)
- 4. count计数
user_list = ['李泉','刘一','刘康','豆豆','小龙']
v = user_list.count('刘一')
print (v)
- 5. extend扩展原列表
user_list = ['李泉','刘一','刘康','豆豆','小龙']
user_list.extend(['杨旭','金颖'])
print (user_list)
- 6. index查找元素索引
user_list = ['李泉','刘一','刘康','豆豆','小龙']
v = user_list.index('刘一')
print (v)
- 7. pop删除并获取元素 - 利用索引
user_list = ['李泉','刘一','刘康','豆豆','小龙']
v = user_list.pop(1)
print (v)
print (user_list)
- 8. reverse翻转
user_list = ['李泉','刘一','刘康','豆豆','小龙']
user_list.reverse()
print (user_list)
- 9. sort排序
从小到大:
nums = [11,12,33,43,28,7]
nums.sort()
print (nums)
从大到小:
nums = [11,12,33,43,28,7]
nums.sort(reverse=True)
print (nums)
10. 额外
user_list = ['李泉','刘一','李泉','刘康','豆豆','小龙']
user_list[0]
user_list[1:5:2]
del user_list[3]
for i in user_list:
print(i)
user_list[1] = '姜日天'
user_list = ['李泉','刘一','李泉','刘康','豆豆',['日天','日地','泰迪'],'小龙']
1.5 range和enumrate
1.5.1 range
实例1-1 1. 请输出1-10
2.7: 立即生成所有数字
range(1,11)
3.x: 不会立即生成,只有循环迭代时,才一个一个生成
for i in range(1,11): #生成1-10的序列
print(i)
for i in range(1,11,2): #生成1-10的序列,步长为2
print(i)
for i in range(10,0,-1): #生成10-1的序列,从大到小
print(i)
实例1-2 range的应用
li = ['eric','alex','tony']
for i in range(0,len(li)):
ele = li[i]
print(ele)
等价于
li = ['eric','alex','tony']
for i in li:
print(i)
1.5.2 enumrate
# enumerate额外生成一列有序的数字
li = ['eric','alex','tony']
for i,ele in enumerate(li,1):
print(i,ele)
等价于:
li = ['eric','alex','tony']
for i in li:
print(i)
for i in range(0,len(li)):
print(i+1,li[i])
1.6 元组类-tuple:不可被修改的列表,不可变类型 ***元组最后,加逗号***
user_tuple = ('alex','eric','seven','alex',)
- 1. count 获取元素个数
user_tuple = ('alex','eric','seven','alex')
v = user_tuple.count('alex')
print (v)
- 2. index获取索引
user_tuple = ('alex','eric','seven','alex')
v = user_tuple.index('alex')
print (v)
- 额外
user_tuple = ('alex','eric','seven','alex')
for i in user_tuple:
print(i)
v = user_tuple[0:2]
print(v)
user_tuple = ('alex','eric','seven',['陈涛','刘浩','赵芬芬'],'alex')
user_tuple[0] = 123 x
user_tuple[3] = [11,22,33] x
user_tuple[3][1] = '刘一'
print(user_tuple)
1.7 字典类-dict:可变类型
- 清空
dic = {'k1':'v1','k2':'v2'}
dic.clear()
print(dic)
- 浅拷贝
dic = {'k1':'v1','k2':'v2'}
v = dic.copy()
print(v)
- 根据key获取制定的value:不存在不报错
dic = {'k1':'v1','k2':'v2'}
v = dic.get('k1')
print (v)
- 删除并获取对应的value值
dic = {'k1':'v1','k2':'v2'}
v = dic.pop('k1')
print(dic)
print(v)
- 随机删除键值对,并获取到删除的键值
dic = {'k1':'v1','k2':'v2'}
v = dic.popitem()
print(dic)
print(v)
- 增加,如果存在则不做操作
dic = {'k1':'v1','k2':'v2'}
dic.setdefault('k3','v3')
print(dic)
dic.setdefault('k1','1111111')
print(dic)
- 批量增加或修改
dic = {'k1':'v1','k2':'v2'}
dic.update({'k3':'v3','k1':'v24'})
print(dic)
- 额外
- 字典可以嵌套
- 字典key: 必须是不可变类型
1.8 集合类-set:可变类型,不可重复的列表
s1 = {"alex",'eric','tony','李泉','李泉11'}
s2 = {"alex",'eric','tony','刘一'}
- 1. difference找不同 s1中存在,s2中不存在
v = s1.difference(s2)
print(v)
# #### s1中存在,s2中不存在,然后对s1清空,然后在重新复制
s1.difference_update(s2)
print(s1)
- 2. symmetric_difference 找s1和s1两个的不同
v = s1.symmetric_difference(s2)
print(v)
- 3. intersection交集
v = s1.intersection(s2)
print(v)
- 4. union 并集
v = s1.union(s2)
print(v)
- 5. discard 移除
s1 = {"alex",'eric','tony','李泉','李泉11'}
s1.discard('alex')
print(s1)
- 6. 额外
s1 = {"alex",'eric','tony','李泉','李泉11',(11,22,33)}
for i in s1:
print(i)
来源:https://www.cnblogs.com/yangxu1229/p/6832690.html