()
或 tuple()
建立()
為 False
,
,a = (1,)
a = (1)
a = (1,)
a = ([1, 2],)
a = ({"a":1},)
# 1 # <class 'int'>
# (1,) # <class 'tuple'>
# ([1, 2],) # <class 'tuple'>
# ({"a":1},)# <class 'tuple'>
_
*
打包
x = (1, 2, 3) # tuple packing
解包
(a, b, c) = x # tuple unpacking
a, b, c = [1, 2, 3]
print(a, b, c)
# 1 2 3
a, b, *c = [1, 2, 3, 4]
print(a, b, c)
# 1 2 [3, 4]
(x, y) = (y, x)
def get_error_details():
return (2, 'details')
(errnum, errstr) = get_error_details()
set()
建立{}
會變空 dictset
為 False
某值 in set()
、某值 not in set()
# 單一字串要改成元組
s = set("apple")
# {"l", "e", "p", "a"}
s = set(("apple",))
# {"apple"}
s = set(("apple", "banana"))
# {"apple", "banana"}
a = set([1, 2, 3, 3, 2])
# {1, 2, 3}
a = set({"x":1,"y":2,"z":3})
# {"x", "y", "z"}
來聊聊 dict 吧!