列表
>>> member = ["蘋果","香蕉","士多啤梨","鴨梨"]
>>> member
['蘋果', '香蕉', '士多啤梨', '鴨梨']
>>> num = [1,2,3,4]
>>> num
[1, 2, 3, 4]
>>> mix = ["蘋果",true,[1,2,4]] #混合列表
>>> mix
['蘋果', true, [1, 2, 4]]
>>> mix = #空列表
>>> mix
>>> member
['蘋果', '香蕉', '士多啤梨', '鴨梨', '哈密瓜']
traceback (most recent call last):
>>> member.extend("菠蘿","水蜜桃") #extend(列表):擴充套件乙個列表,注意是列表,不是元素。
traceback (most recent call last):
file "", line 1, in member.extend("菠蘿","水蜜桃")
typeerror: extend() takes exactly one argument (2 given) #擴充套件元素會報錯
>>> member.extend(["菠蘿","水蜜桃"]) #擴充套件列表
>>> member
['蘋果', '香蕉', '士多啤梨', '鴨梨', '哈密瓜', '菠蘿', '水蜜桃']
>>> member.insert(0,"牡丹") #insert(插入位置,元素):在指定位置新增乙個元素
>>> member
['牡丹', '蘋果', '香蕉', '士多啤梨', '鴨梨', '哈密瓜', '菠蘿', '水蜜桃']
>>> member = ["蘋果","香蕉","士多啤梨","鴨梨"]
>>> member[0] #獲取指定位置的元素
'蘋果'
>>> member.remove("香蕉") #刪除元素
>>> member
['蘋果', '士多啤梨', '鴨梨']
>>> del member[1]
>>> member
['蘋果', '鴨梨'] #刪除元素
>>> del member #刪除列表
syntaxerror: invalid syntax
>>> del member
>>> member
traceback (most recent call last):
file "", line 1, in member
nameerror: name 'member' is not defined
>>> member = ["蘋果","香蕉","士多啤梨","鴨梨"]
>>> member.pop() #pop(索引值):刪除指定位置的元素,並返回該元素。如果pop不給值就刪除並返回最後乙個元素,
'鴨梨'
>>> member
['蘋果', '香蕉', '士多啤梨']
>>> member = ["蘋果","香蕉","士多啤梨","鴨梨"]
>>> member.pop(1)
'香蕉'
>>> member
['蘋果', '士多啤梨', '鴨梨']
>>> member = ["蘋果","香蕉","士多啤梨","鴨梨"]
>>> member[1:3] #列表分片:返回指定索引值之間的乙個新列表。原列表不變。[1:3]:1,2
['香蕉', '士多啤梨']
>>> member
['蘋果', '香蕉', '士多啤梨', '鴨梨']
>>> member[:3] #返回從0開始到索引值之間的乙個新列表。
['蘋果', '香蕉', '士多啤梨']
>>> member[1:] #返回從1開始到末尾的乙個新列表。
['香蕉', '士多啤梨', '鴨梨']
>>> member2 = member[:] #拷貝乙個列表,新增乙個位址,對member操作,member不改變;如果member3 = member,它倆指向同乙個位址,對member操作member3也改變
>>> member2
['蘋果', '香蕉', '士多啤梨', '鴨梨']
>>> list1 = [1,2,3] #列表比較
>>> list2 = [2,3,4]
>>> list1 > list2
false
>>> list1 = [123,456] #短路邏輯,比較完123>234為false,後面不用比較了
>>> list2 = [234,123]
>>> list1 > list2
false
>>> list3 = [123,456]
>>> (list1 < list2) and (list1 == list3)
true
>>> list4 = list1 + list2 #列表相加
>>> list4
[123, 456, 234, 123]
>>> list4 *2
[123, 456, 234, 123, 123, 456, 234, 123]
>>> list4 *=2
>>> list4
[123, 456, 234, 123, 123, 456, 234, 123]
>>> 123 in list4 #成員操作符
true
>>> 245 not in list4
true
>>> list5 = [123,["小甲魚","牡丹"],456] #成員操作符只能對一層進行操作。
>>> "小甲魚" in list5
false
>>> "小甲魚" in list5[1]
true
>>> list5[1][1] #索引
'牡丹'
>>> list1 = [123,456,123,123]
>>> list1.count(123) #count(元素):元素在列表裡出現的次數
3>>> list1 = [123,456]
>>> list1 *5
[123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
>>> list1 *=5
>>> list1
[123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
>>> list1.index(123) #index(元素,起始索引位址,結束索引位址)
0 #預設從起始開始查詢索引
>>> list2 =[1,4,7,2,9,0,3]
>>> list2.reverse() #列表翻轉
>>> list2
[3, 0, 9, 2, 7, 4, 1]
>>> list2.sort() #列表排序,預設列表從小到大
>>> list2
[0, 1, 2, 3, 4, 7, 9]
>>> list2.sort(reverse=true) #再次翻轉,從大到小排列
>>> list2
[9, 7, 4, 3, 2, 1, 0]
列表推導式(列表解析)
[有關a的表示式 for a in b]
>>> [i*i for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
list1 =
for i in range(10):
print(list1)
>>> list1 = [(x,y) for x in range(10) for y in range(10) if x%2!=0 if y%2!=0]
>>> list1
[(1, 1), (1, 3), (1, 5), (1, 7), (1, 9), (3, 1), (3, 3), (3, 5), (3, 7), (3, 9), (5, 1), (5, 3), (5, 5), (5, 7), (5, 9), (7, 1), (7, 3), (7, 5), (7, 7), (7, 9), (9, 1), (9, 3), (9, 5), (9, 7), (9, 9)] #目前沒看懂 (與下面**等價)
list1 =
for x in range(10):
for y in range(10):
if x%2 ==0:
if y%2 !=0:
print(list1)
零基礎入門學習Python
課程介紹 前半部分主要講解python3的語法特性,後半部分著重講解python3在爬蟲 tkinter pygame遊戲開發等例項上的應用。整個系列共16個章節,前邊13個章節從乙個小遊戲引入python,逐步介紹python的語法以及語言特色。最後3個章節為案例的演示,是前邊內容的總結和提高。課...
零基礎入門學習python
1.從idie啟動python idle是乙個python shell,shell的意思就是 外殼 從基本上說,就是乙個通過輸入本與程式互動的途徑。像windows的cmd的視窗,像linux那個黑乎乎的命令視窗,它們都是shell,利用它們,就可以給作業系統下達命令。同樣,可以利用idle這個sh...
《零基礎入門學習Python》學習筆記1
第一講 我和python的第一次親密接觸 python輸出語句 print i love python 注意後面沒有分號 python可以直接進行加法乘法等運算,沒有變數型別的限制,比如c語言規定了整型變數不能超過多少位 print i love you 8 自動列印8遍 第二講 用python設計...