list 取第几个元素

#!/usr/bin/python


word=['a','b','c','d','e','f','g']


# 取第二个元素
a=word[2]
print "a is: "+a


# 取第2~3个元素
b=word[1:3]
print "b is: "
print b 


# 取前2个元素
c=word[:2]
print "c is: "
print c


# 取所有元素
d=word[0:]
print "d is: "
print d


# 同上
e=word[:2]+word[2:]
print "e is: "
print e


# 取最后一个元素
f=word[-1]
print "f is: "
print f


# 取倒数第4~3个元素
g=word[-4:-2]
print "g is: "
print g


# 取最后2个元素
h=word[-2:]
print "h is: "
print h


# 取除最后2个元素之外的元素
i=word[:-2]
print "i is: "
print i


# 取其长度
l=len(word)
print "Length of word is: "+ str(l)
print "Adds new element"


# 追加元素
word.append('h')
print word


# 追加(合并)列表
word.extend(['a', 'b'])
print word
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值