数据类型
数字 int:
1、将字符串转换为整形
a = "789"
print(type(a))
b = int(a)
print(type(b))
#将字符串转换为数字类型(设a为字符串类型)
new_a = int(a)
2、当前数字的二进制至少需要多少位表示
-bit_lenght
num = "0011"
v = int(num,base = 16)
print(v)
r = age.bit_lenght()
字符串 str:
# 字符串一旦创建,不可修改
# 一旦修改或者拼接,系统都会重新生成字符串,不会在源字符串中添加等操作。
#字符串之间合并只需要 + 运算符就可以实现
1、大写小写
#首字母大写
-capitalize
test = "harvey"
v = test.capitalize()
print(v)
#全部变大写
-swapcase
test = "harvey"
v = test.swapcase()
print(v)
#全部变小写,casefold更全面更完美
-casefold
test = "HARVEY"
v = test.casefold()
print(v)
-lower
test = "HARVEY"
v = test.lower()
print(v)
#######################################
#判断是否大写/小写;全部转换为大写/小写(用于登陆时用户输入的验证码)
-upper/lower
test = "haRvey"
v1 = test.islower()
v2 = test.lower()
print(v1,v2)
test = "haRvey"
v1 = test.isupper()
v2 = test.upper()
print(v1,v2)
#######################################
2、设置宽度
#设置宽度(20)并将内容居中,两边空白可加仅一个字符
-center
test = "harvey"
v = test.center(20,"-")
print(v)
-ljust/rjust
test = "harvey"
v = test.ljust(20,"*")
v = test.rjust(20,"x")
v = test.zfill(20)
print(v)
3、子序列次数
#字符串中寻找某子序列出现的次数
-count
test = "harveyHarvey"
v = test.count('H')
print(v)
#可设置寻找范围,起始位x和结尾位y,范围是从x+1位开始到y位结束(范围型基本都为x+1)
test = "harveyHarvey"
v = test.count('ar',7,9)
print(v)
4、判断开始结束
#判断以...开始/结束
-startswith/-endswith
test = "harvey"
v = test.startswith('r')
print(v)
v = test.endswith('y')
print(v)
5、寻找
######################################
#寻找字符串中子序列是否存在及存在的位置(从左到右),不存在结果为-1。index功能与find相同,区别为找不到会报错,故一般不使用-find/-index
-find
test = "harveyHarvey"
v = test.find('8')
print(v)
v = test.find('H')
print(v)
######################################
6、替换指定值(后有replace更好用)
#将一个字符串中的占位符替换为指定的值
-format/format_map
test = 'I am {name},age is {a}'
print(test)
v = test.format(name = 'harvey',a = '21')
print(v)
test = 'I am {0},age is {1}'
v = test.format('harvey','21')
print(v)
#-format与-format_map用法的区别
test = 'I am {name},age {a}'
v1 = test.format(name = 'harvey',a = '21')
v2 = test.format_map({"name":'harvey',"a":'21'})
print(v1,v2)
7、判断
#判断字符串中是否只包含数字和字母
-isalnum
test = 'harvey0414'
v = test.isalnum()
print(v)
thing = 'utf-8_789'
v = thing.isalnum()
print(v)
#判断字符串中是否只有字母和汉字
-isalpha
test = 'qwer'
v = test.isalpha()
print(v)
#判断字符串中是否只是数字
-isdecimal
-isdigit
-isnumeric
test = "二" # 1,②
v1 = test.isdecimal() #针对范围最小
v2 = test.isdigit() #可支持绝大部分语言中的数字
v3 = test.isnumeric() #最全面,可支持汉字
print(v1,v2,v3)
#是否存在不可显示的字符(如\n换行符,\t制表符)
-isprintable
test = "asfagsdg/tasafg"
v = test.isprintable()
print(v)
#判断是否全是空格
#expandtabs:指定空格数
-isspace
test = ""
v = test.isspace()
print(v)
#判断是否为标题(every word首字母大写)
-istitle
test = "you are my uniqueness"
v = test.istitle()
print(v)
#转换成标题
-title
test = "you are my uniqueness"
v = test.title()
print(v)
#检测字符串是否是字母开头
-isidentifier
test = "class"
v = test.isidentifier()
print(v)
8、拼接
########################
#将字符串中每一个元素按照指定分隔符拼接
-join
test = "刹那芳华"
v = "_".join(test)
print(v)
刹_那_芳_华
########################
9、移除
#################################
#移除字符串头尾指定的字符(默认为空格或换行符)或字符序列,从左/右开始
#移除空格或\t,\n,从左/右开始
-strip
test = "harvey"
v1 = test.strip('ar')
v2 = test.lstrip('har')
v3 = test.rstrip('ey')
print(v1,v2,v3)
harvey vey harv
#################################
10、对应关系替换
#对应关系替换
-makestrans,-translate
test1 = "aeiou"
test2 ="12345"
v = "asddsge;wtreodf;hkukljuio"
n = makestrans("aeiou","12345") #n = makestrans(test1,test2)
new_v = translate(n)
print(new_v)
11、分割为三部分
#将字符串分割为三部分,从左/右开始
-partition
test = "ouiohtnagfdhsfgh"
v = test.partition('s')
print(v)
v = test.rpartition('s')
print(v)
12、分割
######################################
#分割指定前几个,从左/右开始
-split
test = "ousgnfjknosnkfspiu"
v = test.split('s',2)
print(v)
v = test.rsplit('s',2)
print(v)
['ou', 'gnfjkno', 'nkfspiu']
['ousgnfjkno', 'nkf', 'piu']
######################################
13、
#分割,根据True和False确定是否保留换行符\n
-splitlines
test = 'qwertyu\nasdfg\nzxvb'
v = test.splitlines(False)
print(v)
14、替换
#########################
#将指定字符串替换为指定字符串,并可以指定具体前几个
-replace
test = "harveyharveyharvey"
v = test.replace("vey",'bbbbb')
print(v)
harbbbbbharbbbbbharbbbbb
test = "harveyharveyharvey"
v = test.replace("vey",'bbbbb',2)
print(v)
harbbbbbharbbbbbharvey
#########################
重点:join添加, replace代替, split分割, upper变大写, lower变小写, find寻找字符串中字符, strip移除
四个超重点:
一、for循环
for 变量名 in 字符串;
countinue #跳出当前循环,当前循环不做操作,若有上一级循环则进入,如没有则什么都不输出
break #中止整个循环
二、test[3]索引,下标,获取字符串中的某一个字符
#test[3]获取字符串中的某一个字符
test = "你怕是个小傻子吧"
item = 0
while item < len[test]:
v = test[item]
print(v)
item += 1
for m in test:
print(m)
break
输出:你
for m in test:
continue
print(m)
输出:
三、切片
test = "你怕是个小傻子吧"
v = test[0:5]#从第0位取到第4位
v = test[0:-1]#-1代表取到倒数第二位
print(v)
你怕是个小
你怕是个小傻子
四、获取长度
#python3,获取当前字符串中由几个字符组成
test = "你怕是个小傻子吧"
v = len(test)
print(v)
五、获取连续或不连续的数字
#可设置步长,取不连续的数
v = range(1,100,5)#步长为5,每5个数取一次 v = range(100,0,-5)从后往前,每次减5
for item in v:
print(item)
1,5,10,15,20......
练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置
test = input("...")
l = len(test) #计算用户输入字符串长度
for item in range(0,l): #让item在字符串长度中循环,即索引位置
print(item,test[item]) #用test[]的表示方法表示出在 item 位置的字符
列表list:
1、列表格式:
li = [1,8,888,"test",["harvey", "你是猪",["18",8],"康师傅"],"qwer",True]
2、列表中可以嵌套任意类型
3、索引取值
print(li[3])
test
4、切片
print(li[4:-1])
[["harvey", "你是猪",["18",8],"康师傅"],"qwer"]
5、for循环,while循环
6、删除(del),结合切片或索引
li = [1,8,888,"test",["harvey", "你是猪",["18",8],"康师傅"],"qwer",True]
del li[4:-2]
del li[4]
print(li)
[1,8,888,'test','qwer',True]
7、修改,结合切片或索引
li = [1,8,888,"test",["harvey", "你是猪",["18",8],"康师傅"],"qwer",True]
li[1] = 120
li[1:2]
print(li)
[1,120,888,["harvey", "你是猪",["18",8],"康师傅"],True]
8、判断是否有某一字符串(in的用法)
li = [1,8,888,"harvey",["今麦郎",["19",8],"康师傅"],"qwer"]
v = "今麦郎" in li
print(v)
False
9、取值
li = [1,8,888,"harvey",["今麦郎",["19",8],"康师傅"],"qwer"]
#取"9"
print(li[4][1][0][1])
10、转换
#转换为列表类型时,系统会用for循环每一个元素,int类型不能被for循环,故不能通过此方法转换为list
#将列表转换成字符串
#当列表中有数字时
li = [123,465,"123","harvey"]
s = ""
for i in li:
s = s + str(i)
print(s)
123465123harvey
#当列表中只有字符串时
li = ["123","456","harvey"]
v = "".join(li)
print(v)
11、添加
-append
-extend 他的参数必须为可迭代对象,内部要进行for循环
li = [12,34,56,78,56]
li.append(["猴哥猴哥","你真了不得"])
print(li)
[12, 34, 56, 78, 56, ['猴哥猴哥', '你真了不得']]
li.extend(["猴哥猴哥","你真了不得"])
li.extend("了不得")
print(li)
[12,34,56,78,56,"猴哥猴哥","你真了不得"]
[12,34,56,78,56,'不','得','了']
12、清除
-clear
li = [12,23,45,56]
li.clear()
print(li)
[]
13、浅拷贝
li = [12,34,56]
li.copy()
print(li)
[12,34,56]
14、判断元素出现次数
-count
li = [12,23,45,56,12]
li.count(12)
print(li)
2
15、获取索引位置
#获取当前值索引位置(从左到右,只获取第一个)可设置起始位置
-index
li = [12,22,33,22,44]
v = li.index(22,2,4)
print(v)
3
16、插值
-insert
li = [12,22,33,22,44]
li.insert(想插入的位置,想插入的值)
print(li)
17、删除
#删除一个值,通过索引指定,默认删除最后一个,也可获取删除的值
-pop
li = [12,22,33,22,44]
v = li.pop(1)
print(li)
print(v)
[12,33,22,44]
#删除一个指定值,左边优先
-remove
li = [12,22,33,22,44]
v = li.remove(22)
print(li)
li = [12,33,22,44]
(上面还有del)
18、反转
#将当前列表反转
-reverse
li = [12,22,33,22,44]
li.reverse()
print(li)
[44,22,33,22,12]
19、排序
#给列表排序,从小到大或从大到小
-sort
li = [12,22,33,22,44]
li.sort()
print(li)
[12,22,22,33,44]
li.sort(reverse = True)
print(li)
[44, 33, 22, 22, 12]
元组 tuple:
1、元素一级不可被修改,不能增加或删除,但元组中嵌套列表,则可以,例如
tu =(123,"harey",[(33,44)],(8,9),"今麦郎",True,)
tu[2] = 8
print(tu) #报错
tu[2][0] = 8
print(tu)
(123, 'harey', [8], (8, 9), '今麦郎', True)
tu[2][0][0] = 8
print(tu) #报错,因为(33,44)仍是元组
2、一般在元组最后加“,”,以区分其他类 tu =(123,"harey",[(33,44)],(8,9),"今麦郎",True,)
3、索引 v = tu[0] print(v) 123
4、切片 v = tu[0:2] print(v)
5、可被for循环,为可迭代对象(join,extend) for item in tu: print(item)
6、转换 字符串,列表,元组可相互转换 s = tuple(s),li = tuple(li)
#获取某一元素出现次数
-count
#寻找某一元素,可指定起始索引位置与结束索引位置
-index
字典 dict:
1、基本结构:
{“k1”:v1 , "k2":v2 , "k3":v3}#键 值 对,key不能重复,否则只打印一个,布尔值视为1,0,注意不要重复
2、字典的value可以是任意值
info = {"k1":23 , "k2":True , "k3":[23 , [] , () , {"kk1":'vv1' , "kk2":'vv2'' , "kk2":(88,89)} ] , "k4":(88,89,90)}
3、列表,字典不能作为字典的key,因为他们都可以修改。
4、字典无序(每次打印结果会不同)
5、索引,不可切片,因为字典为无序
info = {"k1":23 , "k2":True , "k3":[23 , [] , () , {"kk1":'vv1' , "kk2":'vv2'' , "kk3":(88,89)} ] , "k4":(88,89,90)}
找到88:
v = info["k3"][3]["kk3"][0]
print(v)
6、删除del
info = {"k1":23 , "k2":True , "k3":[23 , [] , () , {"kk1":'vv1' , "kk2":'vv2' , "kk3":(88,89)} ] , "k4":(88,89,90)}
删除88:
del info["k3"][3]["kk3"][0]
print(info)
7、for循环
info = {"k1":23 , "k2":True , "k3":[23 , [] , () , {"kk1":'vv1' , "kk2":'vv2' , "kk3":(88,89)} ] , "k4":(88,89,90)}
for item in info:
print(item)
k1
k2
k3
k4
for item in info.keys():
print(item)
k1
k2
k3
k4
for item in info.values():
print(item)
23
True
[23, [], (), {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (88, 89)}]
(88, 89, 90)
for item in info:
print(item,info[item])
k1 23
k2 True
k3 [23, [], (), {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (88, 89)}]
k4 (88, 89, 90)
#最标准方式
for k,v in info.items():
print(k,v)
k1 23
k2 True
k3 [23, [], (), {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (88, 89)}]
k4 (88, 89, 90)
8、根据序列创建字典,并可设置统一值,不设置则默认为None
-formkeys
v = dict.fromkeys([k1,132,"888"])
print(v)
{'k1': None, 132: None, '888': None}
v = dict.fromkeys(["k1",132,"888"],666)
print(v)
{'k1': 666, 132: 666, '888': 666}
9、根据key获取value
dic = {"k1":v2 , "k2":v2}
#索引,不存在时会报错
v = dic["k1"]
print(v)
v2
v = dic["k6"]
#报错
#key不存在时,可以指定默认值(None)
-get
v = dic.get("v6",6)
print(v)
10、删除
#指定某一个key删除
#可获取被删除的value
-pop
dic = {"k1":"v1" , "k2":"v2"}
v = dic.pop("k1")
print(dic,v)
{'k2': 'v2'} v1
#若删除的k不存在,则输出默认值(None),或指定值
v = dic.pop("k6",6)
print(dic,v)
{"k1":"v2" , "k2":"v2"} 6
#随机删除一个,不可传参数
-popitem
dic = {"k1":"v1" , "k2":"v2"}
k,v = dic.popitem()
print(dic,k,v)
{'k1': 'v1'} k2 v2
11、设置值
#若指定的key已经存在,则获取该值
-stdefault
dic = {"k1":"v1" , "k2":"v2"}
v = dic.setdefault("k1",123)
print(dic,v)
{'k1': 'v1', 'k2': 'v2'} v1
#若不存在,则添加指定的key,可设置value,没有则默认
v = dic.setdefault("k6",123)
print(dic,v)
{'k1': 'v1', 'k2': 'v2', 'k6': 123} 123
12、更新
#存在则修改,不存在则添加,两种表达方式
-update
dic = {"k1":"v1" , "k2":"v2"}
v = dic.update({"k1":'888',"k6":123})
v = dic.update(k1 = '888',k6 = 123)
print(dic)
{'k1': '888', 'k2': 'v2', 'k6': 123}
整理:
一、int
int()
二、str
find,replace,strip,split,join,upper/lower,format
三、列表
append,extend,insert,索引,切片,循环
四、元组
一级元素不能修改,索引,切片,循环
五、字典
get,update,keys,values,items
for循环,索引
六、布尔值
bool()
记住False:0,None,"",[],(),{}
其他均为True
集合set:
1、书写格式
s = {1,2,3,4,5,6},创建一个不可变集合:s = frozenset(...)
2、由不同元素组成
3、无序
4、集合中元素必须是不可变类型
5、添加
-add
s = {1,2,3,4,5,6}
s.add(3)
print(s)
{1,2,3,4,5,6}
s.add('3')
print(s)
{1,2,3,4,5,6,'3'}
6、清空
-clear
s = {1,2,3,4,5,6}
s.clear()
print(s)
set()
7、拷贝
-copy
s = {1,2,3,4,5,6}
s.copy()
print(s)
s = {1,2,3,4,5,6}
8、删除
-pop
#随机删除
s = {1,2,3,4,5,6}
s.pop()
-remove
#指定删除,若不存在会报错,故不常使用
s.remove('1')
print(s)
s = {2,3,4,5,6}
-discard
#指定删除,不会报错
s.discard('1')
print(s)
s = {2,3,4,5,6}
9、求交集
-intersection
&
s1 = {1,2,3,4,5,'harvey'}
s2 = {4,5,6,7,8,'harvey'}
s = s1.intersection(s2)
s = s1 & s2
print(s)
{'harvey', 4, 5}
{'harvey', 4, 5}
10、求并集
-union
|
s1 = {1,2,3,4,5,'harvey'}
s2 = {4,5,6,7,8,'harvey'}
s = s1.union(s2)
s = s1 | s2
print(s)
{1, 2, 3, 4, 5, 'harvey', 6, 7, 8}
11、求差集
-difference
-difference_update
s1 = {1,2,3,4,5,'harvey'}
s2 = {4,5,6,7,8,'harvey'}
s = s1.difference(s2) #s1 - s2
print(s)
{1, 2, 3}
s1 = s1 - s2相当于s1.difference_update(s2)
12、交差补集
-symmetric_difference
^
s1.symmetric_difference(s2)
s1^s2
13、判断有无交集,没有为True
-isdisjoint
s1={1,2}
s2={2,3,5}
print(s1.isdisjoint(s2))
False
14、判断子集,父集
-issubset
-issuperset
s1={1,2}
s2={1,2,3}
print(s1.issubset(s2))
s1 是s2 的子集
print(s2.issubset(s1))
False
print(s2.issuperset(s1))
s1 是s2 的父集
15、修改
-add #只能加入一个值
-update #可以加入多个值
s1={1,2}
s2={1,2,3}
s1.update(s2)#将s1变为s2
print(s1)
{1,2,3}