1.整形int
定义方式:age=10 #age=int(10)
# 类型转换 # print(int(3.1)) # res=int('1111111') # print(res,type(res)) # res=float('111111.1') # print(res,type(res))
*十进制转换
print(bin(13))#转成2进制
print(oct(13))#转成8进制
print(hex(13))#转成16进制
2.浮点型float
3定义方式:
salary=10.1# salary=float(10.1)
# 类型转换 # print(float(10)) # print(float(1.1)) # print(float('1.1'))
3.字符串类型
定义方式:
msg='hello world' #msg=str('hello world')
# 类型转换: 可以把任意类型专场字符串类型 # res1=str(10) # res2=str(10.3) # res3=str([1,2,3]) # res4=str({'x':1}) #res4="{'x':1}" # # print(type(res1)) # print(type(res2)) # print(type(res3)) # print(type(res4))
常用操作+内置方法
1.按索引取值(正向+反向):只能取
msg='hello world' print(type(msg[0])) print(msg[-1]) #mag[0]='H' 不能修改
2.切片(顾头不顾尾、步长)
msg='hello world' print(msg[0]+msg[1]+msg[2]) print(msg[0:5]) print(msg[0:5:2]) #0 2 4 print(msg[0:]) print(msg[:]) print(msg[-1:-5:-1])#-1 -2 -3 -4 print(msg[::-1]) #取反
3.长度len:统计的是字符的个数
# msg='h你d' # print(len(msg))
4.成员运算in和not in:判断一个子字符是否存在于一个大字符串中
msg='hello world' print('ho' in msg) print('ho' not in msg)
5.移除空白strip:移除字符串左右两边的某些字符
msg=' hello ' print(msg.strip(' ')) print(msg.strip()) print(msg) #name=input('name>>>: ').strip() #pwd=input('pssword>>>:').strip() msg='***h**ello***' print(msg.strip('*'))#中间的去除不了 #msg='*-=H/ello)(' #print(msg.strip('*-=/)('))
6.切分split:吧有规律的字符串切呈列表从而方便取值
info='egon:18:180:150' res=info.split(':',1) print(res) print(res[1]) res1=info.spilt(':') print(res1) s1=' ' for item in res: s1+=item print(s1) s1=':'.join(res) print(s1) #':'.join([1,2,3,4,5])
7.循环
for i in 'hello': print(i)
#1、strip,lstrip,rstrip # msg='*****hello****' # print(msg.strip('*')) # print(msg.lstrip('*')) # print(msg.rstrip('*')) #2、lower,upper # msg='AaBbCc123123123' # print(msg.lower()) # print(msg.upper()) #3、startswith,endswith # msg='alex is dsb' # print(msg.startswith('alex')) # print(msg.endswith('sb')) #4、format的三种玩法 # msg='my name is %s my age is %s' %('egon',18) # print(msg) # msg='my name is {name} my age is {age}'.format(age=18,name='egon') # print(msg) # 了解 # msg='my name is {} my age is {}'.format(18,'egon') # msg='my name is {0}{0} my age is {1}{1}{1}'.format(18,'egon') # print(msg) #5、split,rsplit # cmd='get|a.txt|33333' # print(cmd.split('|',1)) # print(cmd.rsplit('|',1)) #6、replace # msg='kevin is sb kevin kevin' # print(msg.replace('kevin','sb',2)) #7、isdigit #当字符串内为纯数字时结果为True # res='11111' # print(res.isdigit()) # int(res) # age_of_bk=18 # inp_age=input('your age: ').strip() # if inp_age.isdigit(): # inp_age=int(inp_age) #int('asdfasdfadfasdf') # if inp_age > 18: # print('too big') # elif inp_age < 18: # print('to small') # else: # print('you got it') # else: # print('必须输入纯数字') # 了解(**) #1、find,rfind,index,rindex,count # print('xxxkevin is sb kevin'.find('kevin')) # print('xxxkevin is sb kevin'.index('kevin')) # print('xxxkevin is sb kevin'.rfind('kevin')) # print('xxxkevin is sb kevin'.rindex('kevin')) # # res='xxxkevin is sb kevin'.find('kevasdfsadfin') # print(res) # res='xxxkevin is sb kevin'.index('kevasdfsadfin') # print('kevin is kevin is kevin is sb'.count('kevin')) #2、center,ljust,rjust,zfill # print('egon'.center(50,'*')) # print('egon'.ljust(50,'*')) # print('egon'.rjust(50,'*')) # print('egon'.zfill(50)) #3、captalize,swapcase,title # print('my name is kevin'.capitalize()) # print('AaBbCc'.swapcase()) # print('my name is kevin'.title()) #4、is其他 # name='egon123' # print(name.isalnum()) #字符串由字母或数字组成 # print(name.isalpha()) #字符串只由字母组成 # print(name.islower()) # print(name.isupper()) # name=' ' # print(name.isspace()) msg='I Am Egon' print(msg.istitle())
列表类型
定义方式:
在[]内用逗号分隔开多个任意类型的值
l=['a','b','c'] #l=list(['a','b','c'])
l=list('hello') l=list({'x':1,'y':2}) print(l)
常用操作+内置方法
l=['a','b','c','d','e'] #1 按索引存取值 print(l[0]) print(l[-1]) print(id(l)) l[0]='A' print(id(l)) #切变(顾头不顾尾) l=['a','b','c','d','e'] print(l[1:4]) print(l) #长度 l=['a','b','c','d','e'] print(len(l)) #成员运算in 和not in print('a' in l) print('aaa' not in l) #追加、插入 l=['a','b','c','d','e'] l.append(333) l.append(4444) print(l) l.insert(0,111111) print(l) #删除 l=['a','b','c','d','e'] del l[0] res=l.remove('b') print(l) print(res) res=l.pop(0) print(l) print(res) #循环 l=['a','b','c','d','e'] for item in l: print(item)
l=['a','b','a','c','d','e'] print(l.count('a')) l=['a','b','a','c','d','e'] items=[1,2,3,4,5] #for item in items: # l.append(item) l.extend(items) print(l) l=['a','b','a','c','d','e'] print(l.index('a',2,5)) l=['a','b','a','c','d','e'] l.reverse() print(1) l=[10,-1,3,11,9] l.sort(reverse=True) print(l)#从大往小排列
该类型总结:
有序 村多个值 可变
l.append('first') l.append('second') l.append('third') print(l) print(l.pop(0)) print(l.pop(0)) print(l.pop(0))
l.append('first') l.append('second') l.append('third') print(l) print(l.pop()) print(l.pop()) print(l.pop())
元组:一个不可变的列表
定义:在()内用逗号分隔开多个任意类型的元素
# t=(1,2.2,'aa',('b','c'),['a','b','c']) # t=tuple(...) # print(type(t))
t=('a',) print(type(t)) print(t)
t1=tuple('hello') t2=tuple([1,2,3]) print(t1) print(t2)
常用操作+内置的方法
#按照索引取值(只能取) t=(1,2.2,'aa',('b','c'),['a','b','c']) print(t[0]) print(t(-1)) #切片 t=('a','b','c','e','f') print(t[1:4]) #长度 print(len(t)) #成员运算 print('a' in t) #循环 for item in t: print(item)
t=('a','b','c','e','a','f') print(t.index('a',1,5)) print(t.count('a'))
该类型总结:存多个值 有序 不可变
字典类型dict
定义方式:在{}用逗号分隔开多个元素,每个元素都是key:value的形式,其中key可以不可变类型,通常是字符串类
d={1:'aaa',2.2:'bbb','xxx':'ccc',(1,2,3):'dddd'} #d=dict(...) print(d[(1,2,3)])
d=dict(x=1,y=2,z=3) print(d) items=[('name','egon'),('age',18),('gender','male')] d={} for item in items: d[item[0]]=item[1] d=dict(items) print(d) # 了解 keys=['name','age','gender','height','weight'] d={} for key in keys: d[key]=None d={}.fromkeys(keys,None) print(d,type(d))
常用操作+内置的方法
#按key存取值:可存可取 dic={'name':'egon','age':18} dic['name'] print(dic.get('name')) dic['name']='EGON' dic['gender']='male' print(dic) #长度len dic={'name':'egon','age':18} print(len(dic)) #成员运算:是以字典的key为准的 dic={'name':'egon','age':18} print('name' in dic) print('egon' in dic) #删除 dic={'name':'egon','age':18} del dic['name'] print(dic) res=dic.pop('name') print(dic) print(res) res=dic.popitem()#随机删除 print(res) #键keys(),值values(),键值对items() # >>> dic={'name':'egon','age':18} # >>> # >>> dic.keys() # dict_keys(['name', 'age']) # >>> dic.values() # dict_values(['egon', 18]) # >>> dic.items() # dict_items([('name', 'egon'), ('age', 18)]) #循环 dic={'name':'egon','age':18} for k in dic: print(k) for k in dic.keys(): print(k) for k in dic.values(): print(v) for k,v in dic.items(): #k,v=('name', 'egon') print(k,v)
dic={'name':'egon','age':18} dic.update({'age':19,'gender':'male'}) print(dic) dic={'name':'egon','age':18} #当key存在时,不改原值,返回原值 res=dic.setdefault('name','EGON') print(dic) print(res) # 当key不存在时,增加新值 res=dic.setdefault('gender','male') print(dic) print(res)
该类型总结:
存多个值 无序 可变
pythons=['张铁蛋','李铜淡','王金蛋','赵银胆','alex','kevin'] linuxs=['oldboy','张铁蛋','赵银胆','alex','wxx'] res=[] for stu in python: if stu in linuxs: print(res)
集合类型set
定义方式:在{}内用逗号分隔开多个元素,但是元素的特点是
集合内的元素必须是不可变类型
集合内元素无序
集合内元素不能重复
s={1,'aaa',2,} #s=set(...) print(s,type(s)) s=set() print(s,type(s))
s={1,1,1,1,1,1,1,1,1,1,1,1,'a','a','a'} print(s)
res=set('hello') print(res) # res=set([1,'a','b']) # print(res)
常用操作+内置的方法
#len长度 #成员运算 pythons={'张铁蛋','李铜淡','王金蛋','赵银胆','alex','kevin'} linuxs={'oldboy','张铁蛋','赵银胆','alex','wxx'} #3、|合集:求所有报名的学生 print(pythons | linuxs) print(pythons.union(linuxs)) #4、&交集:求同时报名两门课程的学生 print(pythons&linuxs) #5、-差集: 求只报名python课程的学员 print(pythons-linuxs) print(linuxs-pythons) #6、^对称差集:求没有同时报名两门课程的学生 #res=(pythons-linuxs) | (linuxs-pythons) res=pythons^linuxs print(res) #7、== s1={1,2,3} s2={3,2,1} print(s1 == s2) # 注意:父子集描述的是一种包含与被包含的关系 #8、父集:>= #9、子集:<= s1={1,2,3} s2={1,2,3,4} print(s2>=s1) print(s1<=s2)
s1={1,2,3} s1.update({3,4,5,6}) print(s1) s1={,'aa','bb',3} print(s1.pop()) res=s1.remove('bbbbbb') print(s1) print(res) s1.discard('bbb') s1.add(4) print(s1)
该类型总结 存多个值 无序 可变
集合的去重
局限性
1. 只能针对不可变类型
2. 不能保证原来的顺序
names=['egon','egon','egon','alex','alex','kevin']
new_names=list(set(names))
print(new_names)
l=[ {'name':'egon','age':18,'sex':'male'}, {'name':'alex','age':73,'sex':'male'}, {'name':'kevin','age':20,'sex':'female'}, {'name':'egon','age':18,'sex':'male'}, {'name':'egon','age':18,'sex':'male'}, ] new_l=[] for dic in l: if dic not in new_l; new_l.append(dic) print(new_l)