列表与for循环
列表(list):
python基礎(chǔ)數(shù)據(jù)類型之一:其他語言中也有列表的概念。可索引,可切片,可加長。
列表可以儲存大量數(shù)據(jù)。
?
#作用:多個裝備,多個愛好,多門課程,多個女朋友等#定義:[]內(nèi)可以有多個任意類型的值,逗號分隔 my_girl_friends=['alex','wupeiqi','yuanhao',4,5] #本質(zhì)my_girl_friends=list([...]) 或 l=list('abc')?
1、索引: li = ['alex',100,True,[1,2,3],{'name':'li'},(22,33)] print(li[1])2、切片:Print (li[::2]) 3、增List.append()追加到最后List.insert(1,'baoyuan‘)插入到位置list.extend() #iterable可迭代的,以最小元素迭代追加 4、刪除Pop 按照索引刪除List.pop(0)#索引唯一一個有返回值的:print(list.pop())Remove 按照元素去刪除List.remove()Clear 清空保存空列表List.clear()Del 刪除Del list可以按照索引刪除Del list[]可以按照切片刪除Del list[:2]可以在內(nèi)存級別刪除整個列表Del list 5、改按照索引去修改List[] = ' '按照切片去修改 :1、先把列表原內(nèi)容清空 2、把新內(nèi)容迭代添加List[:2]=[ , , , ,]按照切片加步長 #刪除幾個添加幾個List[:::] = 6、查按照索引切片,切片加步長差For 循環(huán)For I in list:print(I) 7、其他方法len()查詢總個數(shù)List.count() 某個元素的出現(xiàn)次數(shù)Index 通過元素找索引List.index(‘’) 列表都是數(shù)字:List.sort() 從小到大排序List.sort(reverse = True) 從大到小排序List.reverse() 反轉(zhuǎn) 8、嵌套元組:
只讀列表,只允許查詢,不允許更改索引 切片 切片加步長For 循環(huán)的查Index len count 應(yīng)用場景:一些非常重要的數(shù)據(jù),不允許修改的,放在元組中。元組里的列表能增刪改For 循環(huán):
for循環(huán):用戶按照順序循環(huán)可迭代對象的內(nèi)容。
1 迭代式循環(huán):for,語法如下
for i in range(10):
縮進的代碼塊
2 break與continue(同上)
3 循環(huán)嵌套
for i in range(1,10):for j in range(1,i+1):print('%s*%s=%s' %(i,j,i*j),end=' ')print() #分析 '''#max_level=5* #current_level=1,空格數(shù)=4,*號數(shù)=1*** #current_level=2,空格數(shù)=3,*號數(shù)=3***** #current_level=3,空格數(shù)=2,*號數(shù)=5******* #current_level=4,空格數(shù)=1,*號數(shù)=7 ********* #current_level=5,空格數(shù)=0,*號數(shù)=9#數(shù)學表達式 空格數(shù)=max_level-current_level *號數(shù)=2*current_level-1'''#實現(xiàn) max_level=5 for current_level in range(1,max_level+1):for i in range(max_level-current_level):print(' ',end='') #在一行中連續(xù)打印多個空格for j in range(2*current_level-1):print('*',end='') #在一行中連續(xù)打印多個空格print()?
轉(zhuǎn)載于:https://www.cnblogs.com/buchiyudeyang/p/9437215.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
- 上一篇: 诺基亚n82评测(诺基亚n82上市时间)
- 下一篇: 洛谷 P1939 【模板】矩阵加速(数列