Python 列表List 的使用
生活随笔
收集整理的這篇文章主要介紹了
Python 列表List 的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# 定義一個list
str_a = ["a", "b", "c", "d"]# 打印整個list
print(str_a)
# 打印結果 ['a', 'b', 'c', 'd']# 打印list的長度
print(len(str_a))
# 打印結果4# list獲取指定元素
print("list第一個元素="+str_a[0], "list第二個元素="+str_a[1],"list第三個元素="+str_a[2], "list第四個元素="+str_a[3])
# 打印結果 list第一個元素=a list第二個元素=b list第三個元素=c list第四個元素=d# 打印list前2個元素
print(str_a[0:2])
# 打印結果['a', 'b']# 打印從二個到結尾
print(str_a[1:])
# 打印結果['b', 'c', 'd']
print(str_a[1])# 打印list前3個元素 測試發現不包含最后一個
print(str_a[0:3])
# 打印結果['a', 'b', 'c']# 遍歷list
for str_b in str_a:print(str_b)"""
打印結果
a
b
c
d
"""
# list 添加元素
str_a.append("e")
print(str_a)
# 打印結果['a', 'b', 'c', 'd', 'e']# 刪除元素
del str_a[2]
print(str_a)
# 打印結果['a', 'b', 'd', 'e']# 數組相加
str_c = ["x", "y", "z"]
print(str_a + str_c)
# 打印結果['a', 'b', 'd', 'e', 'x', 'y', 'z']# 數組重復
print([str_a]*2)
# 打印結果[['a', 'b', 'd', 'e'], ['a', 'b', 'd', 'e']]# 判斷是否在list里面
if "a" in str_a:print("在list里面")
else:print("不在list里面")
# 打印結果:在list里面
總結
以上是生活随笔為你收集整理的Python 列表List 的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在去眼袋多少钱啊?
- 下一篇: Python 元组的使用