# Python中的函数（一）

### Tupes

``````Python 3.7.0 (default, Mar 15 2017, 12:20:11)
Type "help", "copyright", "credits" or "license" for more information.
>>> 1,2,3  #declaure a tuple
(1, 2, 3)
``````

``````>>> a = (1)
>>> type(a)
<class 'int'>
>>> b = 1,
>>> type(b)
<class 'tuple'>
>>> b
(1,)
``````

### Unpacking Values

``````a,b,c = [1,2,3] #a->1, b->2, c->3
a,b,c = 10,20,'hello' #a->10, b->20, c->hello
a,b,c = 10, {1,2}, ['a','b'] #a->10, b->{1,2}, c->['a','b']
a,b,c = 'xyz' #a->x, b->y, c->z
``````

``````d = {'key1':1, 'key2':2, 'key3':3}
a,b,c = d #a->'key2' b->'key3', c='key1'
s = {'x','y','z'}
a,b,c = s #a->'z' b->'x', c='y'
``````

``````#swap a,b
a,b = b,a
``````

• `*``**`

``````l = [1,2,3,4,5,6]

#using slicking
a = l[0]
b = l[1:]

#using simple unpacking
a,b = l[0]:l[1:]

#using * operator
a, *b = l
``````

slicing只适用于数组，而`*`可适用于任何iterable的集合变量，对于有序集合，`*` unpack的结果为为数组

``````a, *b = (1,2,3) #a = 1, b = [2,3]
a, *b = "abc" #a = 'a', b = ['b','c']
a, *b, c = 1,2,3,4 #a = 1, b = [2,3], c = 4
a, *b = {"key1":123, "key2":456, "key3": 789} #a = "key1", b= ["key2","key3"]
``````

`*`也可以用到等号右边

``````
``````

（未完待续）