list 取第幾個元素
阿新 • • 發佈:2019-02-05
#!/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
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