1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| >>> x, y, z = 1, 2, 3 >>> print(x, y, z) 1 2 3
>>> x, y = y, x >>> print(x, y, z) 2 1 3
>>> values = 1, 2, 3 >>> values (1, 2, 3) >>> x, y, z = values >>> x 1
>>> scoundrel = {'name': 'Robin', 'girlfriend': 'Marion'} >>> key, value = scoundrel.popitem() >>> key 'girlfriend' >>> value 'Marion'
>>> x, y, z = 1, 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 2 values to unpack >>> x, y, z = 1, 2, 3, 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: too many values to unpack
>>> a, b, *rest = [1, 2, 3, 4] >>> rest [3, 4]
>>> name = "Albus Percival Wulfric Brian Dumbledore" >>> first, *middle, last = name.split() >>> middle ['Percival', 'Wulfric', 'Brian']
>>> a, *b, c = "abc" >>> a, b, c ('a', ['b'], 'c')
|