python学习-16 列表list

2019-07-24 09:07:20来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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']
我爱你
True

Process 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 0

3.判断是否在列表里(嵌套的元素是一个整体)

li = [123,"love",['a',132,'abc'],"我爱你",True]
a = "abc" in li
print(a)

运行结果:

False

Process finished with exit code 0

 


原文链接:https://www.cnblogs.com/liujinjing521/p/11098894.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python信用卡操作

下一篇:反射