一.数字型 int
#显示数字二进制的位数a = '123'b = a.bit.length()print(b)
#int()转换为数字类型 type()查看是什么类型a = "20"b = int(a, base=16)print(b)print(type(a),type(b))
base=16 代表把字符串a中的数字看作是16进制进行转换
二.字符串 str
#!/usr/bin/env python# -*- coding:utf8 -*-test = 'yuzeYayu'test1 = 'i am {name}{age}'v = test.capitalize() #第一个字符大写 其余小写print(v)v1 = test.upper() #全部面为大写print(v1)v2 = test.lower() #转换为小写print(v2)v3 = test.casefold() #转换为小写print(v3)v4 = test.center(30,'6') #66666666666yuzeYANG66666666666 一共30个字符print(v4)v5 = test.count('ze',0,8) #查看有几个子字符 可以规定范围print(v5)v6 = test.startswith('yu',0,8) #查看开头的子字符 如果是返回trueprint(v6)v7 = test.endswith('ANG',0,8) #查看结束的子字符 如果是返回trueprint(v7)v8 = test.find('ze',0,8) #查看子字符的位置print(v8)v9 = test1.format(name = 'yuzeyang', age = '19') #替换大括号里面的内容print(v9)v10 = test1.format_map({ 'name':'yuzeyang', 'age':'19'}) #替换大括号里面的内容print(v10)v11 = test.isalnum() #字符串中只是字母汉字和数字返回trueprint(v11)v12 = test.isalpha() #字符串中全是字母汉字返回trueprint(v12)vv12 = test.islower() #字符串中全是小写字母汉字返回trueprint(vv12)v13 = test.isdecimal() #全是十进制数字返回trueprint(v13)v14 = test.isdigit() #全是数字返回true 支持①②特殊数字print(v14)vv14 = test.isnumeric() #全是数字支持①② 包括汉字print(vv14)v16 = test.isprintable() #查看字符串输出的是否可以全部显示print(v16)v17 = test.isspace() #查看字符串是否全是空格 \t \nprint(v17)v18 = test.istitle() #查看字符串是否是标题 每个单词首字母都大写 其余的小写print(v18)v19 = test.title() #转换成标题的样式print(v19)t = ' 'v20 = t.join(test) #把t插入到testprint(v20)v21 = test.rjust(20,'a') #填充print(v21)v22 = test.ljust(20,'a')print(v22)v23 = test.strip('yu') #去除两边的空格 \n \t 也可以指定字符print(v23)v24 = test.lstrip() #去除左边的空格 \n \tprint(v24)v25 = test.rstrip('yu') #去除右边的空格 \n \tprint(v25)v26 = 'yuzeyang'm = str.maketrans("y",'z') #把v26中的y换成znew_v26 = v26.translate(m)print(new_v26)v27 = test.partition('yu') #把字符串进行分割print(v27)v28 = test.rpartition('yu')print(v28)v29 = test.split('y',1) #删除y 可以规定找的个数print(v29)v30 = test.rsplit('ze')print(v30)a = "afef\nregfgr\nregreg" #按照换行分割v31 = a.splitlines(True)print(v31)v32 = test.swapcase() #大小写转换print(v32)test2 = 'username\temil\tpassword\nyu\t125@qq.com\t123\nyu\t125@qq.com\t123\nyu\t125@qq.com\t123\n' fewaf fewfv15 = test2.expandtabs(20) #6个一组寻找 找到\t 输出空格print(v15) # username emil password # yu 125@qq.com 123 # yu 125@qq.com 123 # yu 125@qq.com 123
#!/usr/bin/env python# -*- coding:utf8 -*-test = 'yuzeyang'# v = test[0:1] #>=0 <1# print(v)# v1 = len(test) #查看字符串的长度# print(v1)# for temp in test:# print(temp)# v2 = test.replace('y','z',2) #替换# v = range(0, 100, 5) #创建数字 每5个创建一个# print(v)# for temp in v:# print(temp)test1 = input('>>>')l = len(test1)r = range(0, l)for temp in r: print(temp+1, test1[temp])
三.列表 list
#!/usr/bin/env python# -*- coding:utf8 -*-li = [1,2,'fe',['雨泽',0],123456]print(li[3])print(li[0:2]) #通过切片取到到是列表for循环for temp in li: print(temp)while循环l = len(li)s = 0while s < l: print(li[s]) s += 1修改li[2] = 20print(li[2])切片修改li[0:3] = [10,20,30]print(li)in操作v = ['雨泽',0] in liprint(v)删除del li[0]print(li)切片删除del li[0:2]print(li)
#!/usr/bin/env python# -*- coding:utf8 -*-lie = [1,2,'fe',['雨泽',0,['19',20]],123456]# a = li[3][2][0]# print(a)#转换# s = 'ewafwaef'# ll = list(s)# print(ll)# ll = ['10','20','123','eff']# s = ''# for temp in ll:# s += str(temp)# print(s)## v = ''.join(ll) #这个方法必须列表中全是字符串# print(v)#列表魔法li = [11,22,33,44]# li.append(5) #在列表的后面添加# print(li)# li.clear() #清空列表# print(li)# ll = li.copy() #复制# print(ll)# v1 = li.count(11 #计算某个元素出现的次数# print(v1)# v1 = li.extend(['666',888]) #把多个元素添加到列表当中 迭代的方法# print(li)# v2 = li.index(11) #从左边开始索引元素位置# print(v2)# li.insert(0,0) #按照位置插入元素# print(li)# v3 = li.pop(1) #删除规定位置的值 并且可以获取# print(li)# print(v3)# li.remove(11) #从左边开始删除规定的值# print(li)# li.reverse() #反转# print(li)# li.sort(reverse=True) #排序# print(li)
四.元祖 tuple
#!/usr/bin/env python # -*- coding:utf8 -*- #元组的元组不可被修改 不可增加删除 可以查看 # li = [20,(20,'efw'),'ewff'] # print(li[1]) zu = (20,30,'qwqe',['ef',20],'666',) #索引 #print(zu[0]) #切片 #print(zu[0:2]) #for循环 # for temp in zu: # print(temp) #字符串 元组 列表之间转换 s = 'wefafwef' li = ['waef',20,['wef',30]] # v = tuple(s) #字符转元组 # print(v) # v1 = tuple(li) #列表转元组 # print(v1) # v2 = list(zu) #元组转列表 # print(v2) # v3 = str(zu) #元组转字符 # print(v3) # ss = '' #有字符有数字的时候需要用到for # for temp in zu: # ss += str(temp) # print(ss) # v4 = ''.join(zu) #元组当中全元组全是字符穿的时候 可以用join # print(v4) #元组中列表中的修改 tu = (11,22,[(33,22)],'rth') # tu[2].append(66) # print(tu) #元组类的使用方法 # v = tu.count(11) #次数 # print(v) # v1 = tu.index(22,0,2) #位置 # print(v1)
五.字典 dict
#!/usr/bin/env python# -*- coding:utf8 -*-infor = { 'ewfa':'wef', #键值对 'fe':'awd' }info= { 'k1':18, 'k2':'eff', 'k3':[11,22,33, { 'k1':20 } ], 'k4':(10,20), 1:20, (20):30}#print(info) #字典是无序的 布尔值 字典 列表不可以作为字典的key#索引# print(info['k1'])# for temp in info:# print(temp)# for temp in info.keys(): #获取字典当中key# print(temp,info[temp])# for temp in info.values(): #获取字典当中的values# print(temp)# for k,v in info.items(): #获取k和v# print(k,v)#删除# del info['k3'][3]['k1']# print(info)
#!/usr/bin/env python# -*- coding:utf8 -*-info= { 'k1':18, 'k2':'eff', 'k4':(10,20),}# v = dict.fromkeys(['qw','aa'],[123,456])# print(v) #根据序列创建字典 {'qw': [123, 456], 'aa': [123, 456]}# v1 = info.get('k1',1111) #根据key取值 如果不存在 把第二个参数赋值给v# print(v1)# v2 = info.pop('k1',111) #删除并且获取 若果不存在 赋值参数二# print(info,v2)# k,y = info.popitem() #随机删除# print(k,y,info)# info.setdefault('k3',123) #添加值# print(info)info.update({ 'k1':666,'k3':777}) #更新print(info)
六.布尔值 bool
True 真
False 假
v = 1 == 1
v = 1 < 2
user = 'alex'pwd = '123'v = user == 'alex' and pwd == '123' and pwd == '2123' or 2 == 1v1 = 'al' in userprint(type(v), type(v1))print(v, v1)