一、for循环
# 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=0 # while 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) #break #continue # for i in range(10): # if i == 4: # # break # continue # print(i) #for+else # for i in range(10): # print(i) # break # else: # print('===>')
二、python赋值补充:
x=10 #链式赋值 # a=b=c=d=e=f=10 # print(a,b,c,d,e,f) #增量赋值 x=10 y='a' # temp=x # x=y # y=temp # print(x,y) # x,y=y,x # print(x,y) #值的解压 msg='hello' l=[1,2,3] # a,b,c,d,e=msg # print(a,b,c,d,e) # a,_,_,_,e=msg # a,*_,e=msg # print(a) # print(e) # dic={'a':1,'b':2,'c':3} # x,y,z=dic # print(x,y,z)
三、列表
# 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)) # 包含in # hobbies=['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)
四、元组
#为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(1,[1,3],'sss',(1,2)) #t=tuple((1,[1,3],'sss',(1,2))) # print(type(t)) # #元组可以作为字典的key # d={(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']=10 # print(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)