循环
# msg=‘hello‘# msg=[1,2,3,4,5,6]msg=(1,2,3,4,5,6)msg_dic={‘apple‘:10,‘tesla‘:1000000,‘mac‘:3000,‘lenovo‘:30000,‘chicken‘:10,}index=0while index < len(msg): print(msg[index]) index+=1 msg=‘hello‘# msg=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]# msg=(‘a‘,‘b‘,‘c‘,‘d‘,‘e‘)for i in range(len(msg)): print(i,msg[i]) 不依赖索引的取值for item in msg_dic: print(item,msg_dic[item])msg=‘hello‘msg=[1,2,3,4,5,6]msg=(1,2,3,4,5,6)for item in msg: print(item)补充range:顾头不顾尾,默认从0开始print(type(range(1,2)))for i in range(10): print(i,type(i))for i in range(1,10,2): print(i,type(i))for i in range(10,1,-2): print(i)for i in range(1,10,1): print(i)for i in range(9,0,-1): print(i)breakcontinuefor i in range(10): if i == 4: # break continue print(i)for+elsefor i in range(10): print(i) breakelse: print(‘===>‘) 元组
为何要有元组,存放多个值,元组不可变,更多的是用来做查询t=(1,[1,3],‘sss‘,(1,2)) #t=tuple((1,[1,3],‘sss‘,(1,2)))print(type(t)) #元组可以作为字典的keyd={(1,2,3):‘egon‘}print(d,type(d),d[(1,2,3)]) 索引 切片goods=(‘iphone‘,‘lenovo‘,‘sanxing‘,‘suoyi‘)print(goods[1:3]) 长度 in:字符串:子字符串列表:元素元组:元素字典:key goods=(‘iphone‘,‘lenovo‘,‘sanxing‘,‘suoyi‘)print(‘iphone‘ in goods) d={‘a‘:1,‘b‘:2,‘c‘:3}print(‘b‘ in d) 掌握goods=(‘iphone‘,‘lenovo‘,‘sanxing‘,‘suoyi‘)print(goods.index(‘iphone‘))print(goods.count(‘iphone‘)) 补充:元组本身是不可变的,但是内部的元素可以是可变类型t=(1,[‘a‘,‘b‘],‘sss‘,(1,2)) #t=tuple((1,[1,3],‘sss‘,(1,2))) t[1][0]=‘A‘print(t)t[1]=‘aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa‘
字典
info=[‘egon‘,‘male‘,18,180,75] info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}常用操作: 存/取info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}print(info_dic[‘name11111111‘])print(info_dic.get(‘name‘,None)) pop:key存在则弹出值,不存在则返回默认值,如果没有默认值则报错print(info_dic.pop(‘nam123123123123123123e‘,None))print(info_dic) print(info_dic.popitem())print(info_dic) info_dic[‘level‘]=10print(info_dic) 删除info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}info_dic.pop()info_dic.popitem() del info_dic[‘name‘] 键s,值s,键值对info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}print(info_dic.keys())print(info_dic.values())print(info_dic.items()) for k in info_dic: # print(k,info_dic[k]) print(k)print(‘========>‘)for k in info_dic.keys(): print(k)for val in info_dic.values(): print(val)for k,v in info_dic.items(): #k,v=(‘name‘, ‘egon‘) print(k,v) 长度info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}print(len(info_dic)) 循环# 包含in info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}print(‘name‘ in info_dic)print(‘name‘ in info_dic.keys())print(‘egon‘ in info_dic.values())print((‘name‘,‘egon‘) in info_dic.items()) 掌握info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}info_dic.update({‘a‘:1,‘name‘:‘Egon‘})print(info_dic) info_dic[‘hobbies‘]=[]info_dic[‘hobbies‘].append(‘study‘)info_dic[‘hobbies‘].append(‘read‘)print(info_dic) setdefault:key不存在则设置默认值,并且放回值默认值key存在则不设置默认,并且返回已经有的值 info_dic.setdefault(‘hobbies‘,[1,2])print(info_dic)info_dic.setdefault(‘hobbies‘,[1,2,3,4,5])print(info_dic) info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘]}info_dic.setdefault(‘hobbies‘,[]).append(‘study‘) {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘,‘read‘]}info_dic.setdefault(‘hobbies‘,[]).append(‘read‘) {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘,‘read‘,‘sleep‘]}info_dic.setdefault(‘hobbies‘,[]).append(‘sleep‘)l=info_dic.setdefault(‘hobbies‘,[])print(l,id(l))print(id(info_dic[‘hobbies‘])) print(info_dic) 了解d=info_dic.copy()print(d)info_dic.clear()print(info_dic) d=info_dic.fromkeys((‘name‘,‘age‘,‘sex‘),None)print(d)d1=dict.fromkeys((‘name‘,‘age‘,‘sex‘),None)d2=dict.fromkeys((‘name‘,‘age‘,‘sex‘),(‘egon‘,18,‘male‘))print(d1)print(d2) info=dict(name=‘egon‘,age=18,sex=‘male‘)print(info) info=dict([(‘name‘,‘egon‘),(‘age‘,18)])print(info) 列表
l=[1,2,3] #l=list([1,2,3])print(type(l)) pat1===》优先掌握部分 索引 切片l=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘] print(l[1:5])print(l[1:5:2])print(l[2:5])print(l[-1]) 了解print(l[-1:-4])print(l[-4:])l=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘]print(l[-2:]) 追加hobbies=[‘play‘,‘eat‘,‘sleep‘,‘study‘]hobbies.append(‘girls‘)print(hobbies) 删除hobbies=[‘play‘,‘eat‘,‘sleep‘,‘study‘]x=hobbies.pop(1) #不是单纯的删除,是删除并且把删除的元素返回,我们可以用一个变量名去接收该返回值print(x)print(hobbies) x=hobbies.pop(0)print(x) x=hobbies.pop(0)print(x) 队列:先进先出queue_l=[]#入队queue_l.append(‘first‘)queue_l.append(‘second‘)queue_l.append(‘third‘)print(queue_l)出队print(queue_l.pop(0))print(queue_l.pop(0))print(queue_l.pop(0)) 堆栈:先进后出,后进先出l=[]#入栈l.append(‘first‘)l.append(‘second‘)l.append(‘third‘)#出栈print(l)print(l.pop())print(l.pop())print(l.pop()) 了解del hobbies[1] #单纯的删除hobbies.remove(‘eat‘) #单纯的删除,并且是指定元素去删除 长度hobbies=[‘play‘,‘eat‘,‘sleep‘,‘study‘]print(len(hobbies)) 包含inhobbies=[‘play‘,‘eat‘,‘sleep‘,‘study‘]print(‘sleep‘ in hobbies) msg=‘hello world egon‘print(‘egon‘ in msg) #pat2===》掌握部分hobbies=[‘play‘,‘eat‘,‘sleep‘,‘study‘,‘eat‘,‘eat‘]hobbies.insert(1,‘walk‘)hobbies.insert(1,[‘walk1‘,‘walk2‘,‘walk3‘])print(hobbies) print(hobbies.count(‘eat‘))print(hobbies)hobbies.extend([‘walk1‘,‘walk2‘,‘walk3‘])print(hobbies) hobbies=[‘play‘,‘eat‘,‘sleep‘,‘study‘,‘eat‘,‘eat‘]print(hobbies.index(‘eat‘)) pat3===》了解部分hobbies=[‘play‘,‘eat‘,‘sleep‘,‘study‘,‘eat‘,‘eat‘]hobbies.clear()print(hobbies) l=hobbies.copy()print(l) l=[1,2,3,4,5]l.reverse()print(l) l=[100,9,-2,11,32]l.sort(reverse=True)print(l)
时间: 2024-10-15 21:55:16