Python基础之列表

列表的打印与查看

bicycles=[‘trek’,‘cannondale’,‘redline’,‘specialized’]
print(bicycles)
print(bicycles[1])
print(bicycles[-1])
print(bicycles[-1].title())
print(bicycles[0].upper())
print(bicycles[3].lower())

结果为

列表的增删改

修改列表元素值:

指定该元素的新值

name=[‘zhangsan’,‘lisi’,‘wangermazi’,‘xdd’]
name[0]=‘111’

结果为

列表增加元素:

.append(‘元素值’)方法,该方法只能把元素添加在列表最后

motorcycles[‘honda’,‘yamaha’,‘suzuki’]
motorcycles.append(‘zhongguo’)
print(motorcycles)

结果为

.insert( 元素位置,‘元素’)

colorful=[‘green’,‘blue’]
colorful.insert(0,‘red’)
print(colorful)

结果为

列表删除元素:

del 元素位置 语句

colorful=[‘green’,‘green’,‘blue’]
del colorful[2]
print(colorful)

结果为

.remove(‘元素值’),可以将要删除的元素值存入变量中,方便以后对该元素进行说明。值得注意的是方法remove()只删除第一个指定的值,如果要删除多个重复的值,需要用到循环语句。

colorful=[‘green’,‘blue’,‘green’]
colorful.remove(‘green’)
print(colorful)

结果为

.pop(‘元素位置’),pop术语弹出,可以把删除的元素探入到另一个列表中,以便后来使用。如果()里边没有东西,默认为删除弹出最后一个元素。

colorful=[‘green’,‘blue’,‘green’,‘red’]
colorful_pop=[colorful.pop()]
print(colorful)
print(colorful_pop)

结果为

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值