list是Python中的一种数据类型,也就是"列表"。在Python中我们可以对list类型进行插入,删除,修改等操作。
##新建list类型 >>> ball = ['volleyball','basketball','football','baseball'] ##可以直接打印出list内容 >>> ball ['volleyball', 'basketball', 'football', 'baseball'] ##也可以使用下标列出,注意下标是从0开始的,负数表示从后往前数 >>> ball[0] 'volleyball' >>> ball[1] 'basketball' >>> ball[2] 'football' ##使用append函数,在list最后追加内容 >>> ball.append('ping-pong') >>> ball ['volleyball', 'basketball', 'football', 'baseball', 'ping-pong'] ##选择位置插入,比如在'volleyball'后面插入'badminton' >>> ball.insert(1,'badminton') >>> ball ['volleyball', 'badminton', 'basketball', 'football', 'baseball', 'ping-pong'] ##替换list中的某一个元素,比如把'badminton'替换成'bowling' >>> ball[1]='bowling' >>> ball ['volleyball', 'bowling', 'basketball', 'football', 'baseball', 'ping-pong'] ##使用pop()删除元素,比如删除最后的'ping-pong',删除'bowling' >>> ball.pop() 'ping-pong' >>> ball ['volleyball', 'bowling', 'basketball', 'football', 'baseball'] >>> ball.pop(1) 'bowling' >>> ball ['volleyball', 'basketball', 'football', 'baseball'] ##使用len()查询list中元素个数 >>> ball ['volleyball', 'basketball', 'football', 'baseball'] >>> len(ball) 4