python小白之路(特性语法三之列表)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python小白之路(特性语法三之列表)相关的知识,希望对你有一定的参考价值。

列表
一、列表的格式:变量为studentlist = [‘zhangsan’,‘male‘,29],元素可以是不同数据类型的。
二、打印列表:打印学生姓名print(studentlist[0])
三、列表的循环遍历
1、for student in studentlist:
print(studentlist)
2、i = 0
length = len(students)
while i < length:
print(studentslist[i])
i += 1
四、列表的增、删、改、排序
1、添加元素:append、extend、insert
append:通过append可以向列表尾追加元素。
extend:通过extend可以将另一个集合中元素逐一添加到表中。
insert:insert(index.object)在指定位置index前插入元素object。
2、修改元素
修改元素的时候,要通过下标来确定要修改的是哪个元素,然后才能进行修改。
3、查找元素:in、not in、index、count
查找就是看看看指定的元素是否存在。
in:存在返回True,否则返回False。
not in:与in相反。
index:A.index(‘a’,1,4)查找索引,不存在会报异常。
count:A.count(‘a’)统计个数。
4、删除元素:del、pop、remove
del:根据下标进行删除 del studentlist[0]。
pop:删除最后一个元素 studentlist.pop()。
remove:根据元素的值进行删除 studentlist.remove(‘name‘)
5、排序:sort、reverse
sort:将list按特定顺序重新排列,默认为由小到大,参数reverseTrue可改为倒序,由大到小。
studentlist.reverse() studentslist.sort(reverse=True)
五、列表嵌套
一个列表中的元素又是一个列表。

以上是关于python小白之路(特性语法三之列表)的主要内容,如果未能解决你的问题,请参考以下文章

python小白之路(特性语法三之遍历公共方法引用)

python小白之路(特性语法三之字符串)

python小白之路(特性语法三应用名片管理器项目)

新手小白 python之路 Day2 (列表应用)

Python小白学习之路—类和对象列表列表相关功能

python 小白(无编程基础,无计算机基础)的开发之路 day2