《毫無障礙學Python》鄧文淵著 學習筆記
1.列表結構(List) (又稱清單,即爲其他語言的數組Array)
列表的使用:列表名 = [元素1,元素2,…] 中括號隔開,元素數據類型可同可不同,獲取元素值方法:下標訪問(下標從0開始,可爲負值表示倒數第幾個元素)
list1 = [23,'Apple',True]
print(list1[1])
print(list1[-1])
.
多維列表
list2 = [["Harry",1234],["Marry",4567],["Tonny",9876]]
print(list2[0])
print(list2[2][0])
1.1.range函數
range函數 功能:創建整數有序列表
range函數語法三種:1、2、3個參數
1參:變量列表 = range(整數型) 元素:0~整數型-1
list3 = range(5) #即 list3 = [0,1,2,3,4]
.
2參:變量列表 = range(起始值,終止值)(若起始值>終止值->產生空列表) 元素:起始值~終止值-1
list4 = range(-5,5) #即 list4 = [-5,-4,-3,-2,-1,0,1,2,3,4]
.
3參:變量列表 = range(起始值,終止值,間隔值)
list5 = range(-1,5,3) # list5 = [-1,2]
list6 = range(4,-1,-2) #間隔值可負,此時必要起始值>終止值且此時元素:起始值~終止值+1
print(list5[-1])
print(list6[-1])
2.循環
##循環命令:
- for命令執行固定次數循環
- while命令執行不固定次數循環
2.1 for循環(通常用於固定次數循環)
.
for循環 通常用於固定次數循環
語法結構 for 變量 in 列表:
程序塊
listFor = ["Apple","Banana","Watermelon"]
for f in listFor: #系統會將列表元素依次作爲變量值,每次改變變量值後執行一次程序塊
print(f,end=",")
print("\n")
for i in range(1,11):
print(i,end=",")
print("\n")
#計算正整數的和
sum = 0
n = int(input("Please input a number:"))
for n in range(1,n+1):
sum+=n
print("The sum of 1 to %d is %d"%(n,sum))
##for嵌套循環
#九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print("%d*%d=%-2d"%(j,i,i*j),end=" ")
print()
##break命令(跳出循環),continue命令(跳出本次循環,進行下次循環)
for i in range(1,6):
if(i==3):
continue
print(i)
print()
for i in range(1,6):
if(i==3):
break
print(i)
2.1.1 for…if…else循環
for 變量 in 列表:
程序塊1
if(條件表達式):
程序塊2
break
else:
程序塊3
#判斷是否質數
n = int(input("Please input a number can judge whether a prime number(質數):"))
if(n==2):
print("prime number")
else:
for i in range(2,n):
if(n%i==0):
print("not prime number")
break
else:
print("prime number")
2.2while循環
while循環 通常用於沒有固定次數的循環
while(條件表達式): #括號可省略
程序塊
total = i = 0
while(i<10):
i+=1
total+=i
print(total)
3.高級列表操作
令 list1 = [1,2,3,4,5,6],x = [8,9] |
---|
list1*n 列表重複n次 |
list1[n1:n2] 取出n1~n2-1元素 n1、n2爲下標! |
list1[n1:n2:n3] 取出n1~n2-1元素,間隔n3 |
del list1[n1:n2] 刪除n1~n2-1元素 |
del list1[n1:n2:n3] 刪除n1~n2-1元素,間隔n3 |
n = len(list1) 返回列表中元素數量 |
n = min(list1) 返回元素最小值 |
n = max(list1) 返回元素最大值 |
n = list1.index(n1) 取首個值爲n1元素的下標 |
n = list1.count(n1) n1元素出現的次數 |
list1.append(n1) 將n1作爲元素加到列表最後 |
list1.extend(x) 將列表x中元素逐一加至列表最後 |
list1.insert(n,n1) 在位置n加入n1元素 |
n = list1.pop() 取列表最後元素並刪除(括號內數值爲n,則對下標爲n的元素操作) |
list1.remove(n1) 刪除首次出現的n1元素 |
list1.sort() 將列表升序排列 |
#append 輸入成績,-1結束,計算總分和平均分
score = []
total = inscore = 0
while(inscore != -1):
inscore = int(input("Please input student score:"))
score.append(inscore)
score.pop(-1) #刪除列表最後元素“-1”
print("The sum of students numer is %d"%(len(score)))
for i in range(0,len(score)):
total +=score[i]
average = total/len(score)
print("The sum of score is %d and average is %.2f"%(total,average))
4.元組(Tuple)
.
元組結構與列表完全相同,不同 元組個數及元素值不可改變 而列表可變
元組語法: 元組名 = (元素1,元素2,…) #元組小括號,列表中括號
#列表高級方法除改變元素個數及元素值得方法皆可用於元組
#列表和元組的相互轉換:
tuple1 = (1,2,3)
list1 = list(tuple1) #元組->列表
list2 =[9,8,7,6]
tuple2 = tuple(list2) #列表->元組
5.字典Dict
語法: 字典名稱 = {鍵1:值1,鍵2:值2,鍵3:值3,…}
dict1 = {"Banana":15,"Watermelon":32,"Apple":45} #元素在字典中排列隨機,不可dict1[0]訪問
print(dict1["Apple"]) ##"鍵"作爲下標返回"值",因此"鍵"必須唯一,"值"可重複
dict1["Orange"] = 25 ##新增元素,即設定新“鍵”,新“值”
##刪除字典
del dict1["Banana"] ##刪除字典中特定元素 del 字典名 [鍵]
dict1.clear() ##刪除字典中所有元素 字典名.clear()
del dict1 ##刪除字典,後字典變量不存在了 del 字典名
5.1 高級字典操作
若 dict1 = {“Joy”:15,“Lisa”:21} |
---|
len(dict1) 返回字典元素數量 |
dict1.clear() 刪除字典中所有元素 |
dict1.copy 複製字典 dict2 = dict1.copy() |
dict1.get(鍵,值) 返回鍵對應的值 ,若鍵不存在,則返回參數中的值 |
鍵 in dict1 檢驗“鍵”是否存在 b = “Joy” in dict1 #b=True |
dict1.items() 返回以 “鍵——值” 組爲元素的組合 |
dict1.keys() 返回以“鍵”爲組合的組合 |
dict1.setdefault(鍵,值) 與get()類似,若鍵不存在,則以參數創建新元素 |
dict1.values() 返回以“值”爲元素的組合 |
(1)字典Dict中:keys 鍵組合、values值組合、items鍵-值組合
dict2 = {"Lily":70,"Tom":65,"Jery":89}
dict2["Jone"] = 83 #新增“鍵-值”
listkey = list(dict2.keys())
listvalue = list(dict2.values())
for i in range(len(listkey)):
print("%s's score is %d"%(listkey[i],listvalue[i]))
(2)字典Dict的get、setdafault方法
dict1 = {"Lily":70,"Tom":65,"Jery":89}
dict1["Jon"] = 90 #返回鍵對應的值 ,若鍵不存在,則返回參數中的值
n = dict1.get("Lily",100) #n = 70
print(n)
n = dict1.setdefault("Mary") #dict1 = {"Lily":70,"Tom":65,"Jery":89,"Marry":Null}
n = dict1.setdefault("Hary",76) #dict1 = {"Lily":70,"Tom":65,"Jery":89,"Harry":76}