Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈

加入好友
加入社群
Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

Python的數據類型(Arrays)

List []

Tuple ()

Set {}

Dictionary {key:value,key:value}

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

  List [] Tuple () Set {} Dictionary {}
序號
更改資料 不可    
重複 允許 允許 沒有 沒有
索引     無索引 可變更索引

 

Python編輯器中輸入

#listMember1 = [“MA”,”MB”,”MC”]

print(Member1)

print(Member1[2]) #第一個序號為0

Member2 = list((“MD”,”ME”,”MF”)) #雙括弧

print(Member2)

print(Member2[2])

M2 = [“A”,”B”,”C”,”D”,”E”]

for x in M2: #最後有一個冒號, for in :
print(x)

 

Num = len(M2)

print(“M2共有” + str(Num) +”筆資料”) #要加str把Num轉為字串

 

用Anaconda的Spyder

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#建立list的方法:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

b=list(“123”) 

#list後面使用(),不是[]

 

#append, insert為list加資料

M2 = [“A”,”B”,”C”,”D”,”E”]

for x in M2: #最後有一個冒號, for in :
print(x)

Num = len(M2)

print(“M2共有” + str(Num) +”筆資料”) #要加str把Num轉為字串

M2.append(“F”) #append(),用小括弧,記得””

print(M2)

M2.insert(1,”B1″) #順序是從0開始算

print(M2)

 

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#list remove del
M2 = [“A”,”B”,”C”,”D”,”E”]

M2.remove(“B”)

print(M2)

M2 = [“A”,”B”,”C”,”D”,”E”]

del M2[2] #順序從0開始算,使用中括弧

print(M2)

 

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#list clear
M2 = [“A”,”B”,”C”,”D”,”E”]

M2.clear()

print(M2)

執行結果:[]

(M2變成空list)

 

Tuple跟List類似

差別在於不可更改組的資料

#tuple

IT3C = (“mouse”, “computer”, “camera”, “NB”)

#tuple使用(), list使用[]

print(IT3C)

print(IT3C[3])

for x in IT3C:
  print(x) #前面要多一個空白,才能跟上一行的for連結

MyClass = tuple((“python”,”JAVA”,”MySQL”,”PS”))

print(MyClass)

print(MyClass[3])

Num = len(MyClass)

print(Num)

 

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#SET
#無序號
#無索引
#沒有重複

IT3C = {“computer”,”NB”,”Camera”,”mouse”}
#set使用{}, list使用[] , tuple使用()

print(IT3C)

print(“COMPUTER” in IT3C)
#回傳True or False,大寫的”COMPUTER會回傳False

 

Class = set((“Python”,”MySQL”,”PS”,”DW”))

print(Class)

for x in Class:
print(x) #前方要多一個空白,才知道是跟上一行的for連結

Class.add(“AutoCAD”)

#add一次只能新增一筆,不能用Class2 = Class.add(“AutoCAD”)
print(Class)

Class.update([“aaa”,”bbb”,”ccc”]) #.update使用([])
print(Class)

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#SET
#無序號
#無索引
#沒有重複

IT3C = {“computer”,”NB”,”Camera”,”mouse”}
#set使用{}, list使用[] , tuple使用()

print(IT3C)

IT3C.discard(“NB”)
print(IT3C)

 

Class = set((“Python”,”MySQL”,”PS”,”DW”))

print(Class)

Class.remove(“PS”)

print(Class)

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#SET
#無序號
#無索引
#沒有重複

IT3C = {“computer”,”NB”,”Camera”,”mouse”}
#set使用{}, list使用[] , tuple使用()

print(IT3C)

POP = IT3C.pop()
#.pop刪除最後一筆資但最後一筆資料,但是SET無順序,最後一筆不一定是那一筆

print(POP)

print(IT3C)

 

Class = set((“Python”,”MySQL”,”PS”,”DW”))

print(Class)

 

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#SET
#無序號
#無索引
#沒有重複
#Clear
#Del

IT3C = {“computer”,”NB”,”Camera”,”mouse”}
#set使用{}, list使用[] , tuple使用()
print(IT3C)

IT3C.clear() #回傳空SET
print(IT3C)

 

Class = set((“Python”,”MySQL”,”PS”,”DW”))

print(Class)

del Class  #完全消失,沒有這個Set
print(Class)

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#dictionary
#key(索引), value(值)

MyData = {
“Name” : “SavingKing”,
“address” : “Taipei”,
“update year”: “2022”}

print(MyData)

Name = MyData[“Name”]

print(Name)

MyData[“update_year”] = “2023”
print(MyData)

 

stock = dict( item = “2881a”, price = 60, amount = 1)

print(stock)

prc = stock.get(“price”)

print(prc)

for x in stock: #依序讀出key
print(x)

for y in stock: #依序讀出key
print(stock[y]) #印出value

for z in stock.values()
print(z)

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

#dictionary
#key(索引), value(值)

MyData = {
“Name” : “SavingKing”,
“address” : “Taipei”,
“update year”: “2022”}

for x,y in MyData.items(): #item後面要加s
print(x,y)

print(len(MyData))

MyData[“Gender”] = “Male”
MyData[“Birthday”] = “09/25”
print(MyData)

del MyData[“address”]
print(MyData)

stock = dict( item = “2881a”, price = 60, amount = 1)
print(stock)

stock.pop(“amount”)
print(stock)

執行結果:

Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

 

加入好友
加入社群
Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *