python学习-16 列表list
生活随笔
收集整理的這篇文章主要介紹了
python学习-16 列表list
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
list
1.由[ ]括住,中括號內各元素由逗號隔開,各元素可以是數字,字符串,列表,布爾值等等。
例如:
li = [521,"love",["john","boy",12],True]print(li)
ps:列表是可以嵌套的
2.取值
-索引取值:
li = [123,"love",['a',132,'abc'],"我愛你",True] print(li[3])輸出結果:
我愛你Process finished with exit code 0-切片取值:
li = [123,"love",['a',132,'abc'],"我愛你",True] print(li[1:5])輸出結果:
['love', ['a', 132, 'abc'], '我愛你', True]Process finished with exit code 0-循環
li = [123,"love",['a',132,'abc'],"我愛你",True] for a in li :print(a)輸出結果:
123 love ['a', 132, 'abc'] 我愛你 TrueProcess finished with exit code 0?
2.列表元素是可以修改的
li = [123,"love",['a',132,'abc'],"我愛你",True] li[0] = 'cao' print(li)運行結果:
['cao', 'love', ['a', 132, 'abc'], '我愛你', True]Process finished with exit code 0也可以通過切片的方式修改:
li = [123,"love",['a',132,'abc'],"我愛你",True] li[0:3] = ['cao','ni','ma'] print(li)運行結果:
['cao', 'ni', 'ma', '我愛你', True]Process finished with exit code 0?
2.列表元素的刪除
第一種方法:
li = [123,"love",['a',132,'abc'],"我愛你",True] del li[0] print(li)運行結果:
['love', ['a', 132, 'abc'], '我愛你', True]Process finished with exit code 03.判斷是否在列表里(嵌套的元素是一個整體)
li = [123,"love",['a',132,'abc'],"我愛你",True] a = "abc" in li print(a)運行結果:
FalseProcess finished with exit code 0?
轉載于:https://www.cnblogs.com/liujinjing521/p/11098894.html
總結
以上是生活随笔為你收集整理的python学习-16 列表list的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 撰写新闻的心得体会
- 下一篇: nodeJs实现文件上传,下载,删除