優點:可以避免重複性工作,替代如下**:name =
['first'
,'second'
,'third'
]
輸出第乙個值:name1 =
'first'
name2 =
'second'
name3 =
'third'
輸出全部值:(name[0]
)
需要注意的是,列印列表的過程是左閉右開的(name[0:
(name)
以下**列印的結果是第一和第二個元素
(name[0:
2])
可以通過索引對列表的資料項進行修改或更新[
'first'
,'second'
]
結果:name =
['first'
,'second'
,'third'
]name[0]
='i am first'
(name)
[
'i am first', 'second', 'third'
]
結果:name =
['first'
,'second'
,'third'
]'forth'
(name)
使用del函式可以在列表中刪除對應的索引處的值[
'first', 'second', 'third', 'forth'
]
name =
['first'
,'second'
,'third'
]del name[1]
(name)
[
'first', 'third'
]
name =
['first'
,'second'
,'third'
]name[0]
='i am first'
(name)
name =
['first'
,'second'
,'third'
]name[0]
='i am first'
(name)
# coding=utf-8
# 計算列表的長度
name =
['first'
,'second'
,'third'
(len
(name)
)# 列表相加
list1 =[1
,2,3
]list2 =[4
,5,6
(list1+list2)
# 列表相乘
(list1*4)
# 判斷是否在列表中
print(3
in list1)
print(4
in list1)
# 遍歷操作
sum=
0for i in list1:
sum+=i
(sum
)
Python 列表 學習筆記
序列是python中基本資料結構。序列中每個元素都分配到乙個數字 它的位置或索引值 第一位索引值是0,第二位是1,以此類推。python有6個序列的內建型別,但最常見的是列表和元組。序列都可以進行的操作包括索引,切片,加,乘,檢查成員。此外,python已經內建確定序列的長度以及確定最大和最小的元素...
Python學習筆記 列表
今天學習了head first python 中文版 這本書的第1章 人人都愛列表,很有意思。好,為了珍惜時間,下邊開始乾巴巴的筆記 1.檢視python版本 1 python v 大寫 檢視python2版本 2 python3 v 3 python3 v 使用小寫v會進入python直譯器,py...
python學習筆記 列表
1 列表 words hello world print words 0 print words 1 print words 2 大多數情況下,列表中的最後一項不會帶逗號。然而,在那裡放置乙個是完全有效的,在某些情況下是鼓勵的。2 列表也可以巢狀在其他列表中。things string 0,1,2,...