-
Notifications
You must be signed in to change notification settings - Fork 0
/
var_multi_assignment.py
81 lines (64 loc) · 2.45 KB
/
var_multi_assignment.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-04-19 22:00:31
# @copyright by hoojo@2018
# @changelog Added python3 `varables -> var multi assignment` example
#===============================================================================
# 示例:多变量赋值
#===============================================================================
# 描述:唯一的前提就是变量的数量必须跟序列元素的数量是一样的
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
### tuple
#-------------------------------------------------------------------------------
data = ('a', 2, ('b', 10.2))
a, b, c = data
print('a: ', a) # a
print('b: ', b) # 2
print('c: ', c) # ('b', 10.2)
print()
#-------------------------------------------------------------------------------
###list
#-------------------------------------------------------------------------------
data = [ 'a', 2, ('c', 2.2) ]
a, b, c = data
print('a: ', a) # a
print('b: ', b) # 2
print('c: ', c) # ('b', 10.2)
print()
#-------------------------------------------------------------------------------
###进一步赋值
#-------------------------------------------------------------------------------
a, b, (c, d) = data
print('a: ', a) # a
print('b: ', b) # 2
print('c: ', c) # c
print('d: ', d) # 2.2
print()
#-------------------------------------------------------------------------------
### 字符串赋值
#-------------------------------------------------------------------------------
data = '1234'
a, b, c, d = data
print('a: ', a) # 1
print('b: ', b) # 2
print('c: ', c) # 3
print('d: ', d) # 4
print()
#-------------------------------------------------------------------------------
### 占位符赋值;解压一部分,丢弃其他的值
#-------------------------------------------------------------------------------
data = [ 'ACME', 50, 91.1, (2012, 12, 21) ]
_, a, b, _ = data
print('a: ', a) # 50
print('b: ', b) # 91.1
#-------------------------------------------------------------------------------
# 当赋值的变量数量比被赋值的对象数据长度多,将发生异常
#-------------------------------------------------------------------------------
a, b, c = (2, 3)
print('a: ', a)
print('b: ', b)
print('c: ', c) # ValueError: not enough values to unpack (expected 3, got 2)