Python入门(4)-列表LIST(强调几点)

image

Python列表

我没有写Number和字符串,这两个大家应该非常容易看懂,我就不直接列出来了。下面直接讲一下列表:

Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。


list=[1,'hello',3,'world']

下面就拿上面例子来实现增删改和访问

1.增加

可以通过append()方法来增加


list=[1,'hello',3,'world']
list.append("新增")
print(list)

输出:

image

还可以在指定的位置插入数据insert:


list=[1,'hello',3,'world']
list.insert(1,"插入")
print(list)

输出:

image

还可以通过“*”和“+”进行重复和组合


list=[1,'hello',3,'world']
print(list*2)

输出:

image


list=[1,'hello',3,'world']
list2=[4,5]
print(list+list2)

输出:

image

2.删除

全部清除直接list.clear()就可以了,如果是删除某个元素用del,如下:


list=[1,'hello',3,'world']
del list[3]
print(list)

输出:

image

也可以用pop(i)来实现


list=[1,'hello',3,'world']
list.pop(3)
print(list)

3.改

改数据直接去赋值就好了:


list=[1,'hello',3,'world']
list[3]=4
print(list)

输出:

image

4.访问


list=[1,'hello',3,'world']

print('list的第四个元素:',list[3])
print('list的前三个元素:',list[0:3])

另外LIST还有一些函数方法我就不一一写不出来了,都是很容易理解的。

image

大家可以关注我的微信公众号:「安卓干货铺」一个有质量、有态度的公众号!


还有我的新公众号:
image

猜你喜欢

转载自blog.csdn.net/qq_34908107/article/details/79204450