本系列Python基礎教學共四篇,本文為第二篇。
tuple和list十分相似,但是tuple是不可變的,即不能修改tuple,元組透過圓括號中用逗號分割的項定義。
優點:tuple比list速度快;對不需要修改的資料進行'寫保護',可以是程式碼更安全
tuple與list可以互相轉換,使用內建的函數list()和tuple()。
l = [1, 2, 3] print( l )# [1, 2, 3]t = tuple(l) print(t) # (1, 2, 3)l = list(t)print (l) #[1, 2, 3]复制代码
元組最通常的用法是用在列印語句,如下例:
name = "Runsen"age = 20print( "Name: %s; Age: %d") % (name, age)# Name: Runsen; Age: 20复制代码
函數如下:
傳回元組中值為value的元素的個數
t = (1, 2, 3, 1, 2, 3)print (t.count(2) )# 2复制代码
#傳回清單中第一個出現的值為value的索引,如果沒有,則例外ValueError
t = (1, 2, 3, 1, 2, 3) print( t.index(3) )# 2try: print (t.index(4))except ValueError as V: print(V) # there is no 4 in tuple复制代码
字典由鍵值對組成,鍵必須是唯一的;
eg: d = {key1:value1, key2:value2};
空字典用{}表示;字典中的鍵值對是沒有順序的,如果想要一個特定的順序,那麼使用前需要對它們排序;
d[key] = value
,如果字典中已有key
,則為其賦值為value
,否則新增新的鍵值對key/value
;
del d [key] 可以刪除鍵值對;判斷字典中是否有某鍵,可以使用in 或not in;
d = {} d["1"] = "one"d["2"] = "two"d["3"] = "three"del d["3"]for key, value in d.items(): print ("%s --> %s" % (key, value))#1 --> one#2 --> two复制代码
dict函數如下:
d1 = {"1":"one", "2":"two"} d1.clear()print (d1 )# {}复制代码
d1 = {"1":"one", "2":"two"} d2 = d1.copy() print( d2 )#{'1': 'one', '2': 'two'}print(d1 == d2) # Trueprint(d1 is d2) # False复制代码
l = [1, 2, 3] t = (1, 2, 3) d3 = {}.fromkeys(l)print (d3) #{1: None, 2: None, 3: None}d4 = {}.fromkeys(t, "default") print(d4) #{1: 'default', 2: 'default', 3: 'default'}复制代码
d5 = {1:"one", 2:"two", 3:"three"}print (d5.get(1) )#oneprint (d5.get(5)) #Noneprint (d5.get(5, "test") )#test复制代码
d6 = {1:"one", 2:"two", 3:"three"} print( d6.has_key(1) ) #Trueprint (d6.has_key(5)) #False复制代码
d7 = {1:"one", 2:"two", 3:"three"}for item in d7.items(): print (item)#(1, 'one')#(2, 'two')#(3, 'three')for key, value in d7.items(): print ("%s -- %s" % (key, value))#1 -- one#2 -- two#3 -- three复制代码
d8 = {1:"one", 2:"two", 3:"three"}for key in d8.keys(): print (key)#1#2#3复制代码
d8 = {1:"one", 2:"two", 3:"three"}for value in d8.values(): print( value)#one#two#three复制代码
d9 = {1:"one", 2:"two", 3:"three"}print (d9.pop(1) )#oneprint( d9) #{2: 'two', 3: 'three'}print( d9.pop(5, None)) #Nonetry: d9.pop(5) # raise KeyErrorexcept KeyError, ke: print ( "KeyError:", ke) #KeyError:5复制代码
d10 = {1:"one", 2:"two", 3:"three"}print (d10.popitem() ) #(1, 'one')print (d10) #{2: 'two', 3: 'three'}复制代码
d = {1:"one", 2:"two", 3:"three"}print (d.setdefault(1)) #oneprint (d.setdefault(5)) #Noneprint( d) #{1: 'one', 2: 'two', 3: 'three', 5: None}print (d.setdefault(6, "six")) #sixprint (d) #{1: 'one', 2: 'two', 3: 'three', 5: None, 6: 'six'}复制代码
d = {1:"one", 2:"two", 3:"three"} d2 = {1:"first", 4:"forth"} d.update(d2)print (d) #{1: 'first', 2: 'two', 3: 'three', 4: 'forth'}复制代码
d = {1:"one", 2:"two", 3:"three"}for key, value in d.viewitems(): print ("%s - %s" % (key, value))#1 - one#2 - two#3 - three复制代码
d = {1:"one", 2:"two", 3:"three"}for key in d.viewkeys(): print( key)#1#2#3复制代码
d = {1:"one", 2:"two", 3:"three"}for value in d.viewvalues(): print (value)#one#two#three复制代码
#索引運算子和切片運算子
numbers = ["zero", "one", "two", "three", "four"] print (numbers[1] )# oneprint (numbers[-1] )# four#print( numbers[5]) # raise IndexErrorprint (numbers[:]) # ['zero', 'one', 'two', 'three', 'four']print (numbers[3:]) # ['three', 'four']print (numbers[:2]) # ['zero', 'one']print (numbers[2:4] )# ['two', 'three']print (numbers[1:-1] )# ['one', 'two', 'three'] 复制代码
相關免費學習推薦:python影片教學
以上是給小白整理的第二篇Python知識點的詳細內容。更多資訊請關注PHP中文網其他相關文章!