8
8
9
9
- Python 的元组与列表类似,不同之处在于tuple被创建后就不能对其进行修改,类似字符串。
10
10
- 元组使用小括号,列表使用方括号。
11
+ - 元组与列表类似,也用整数来对它进行索引 (indexing) 和切片 (slicing)。
12
+
13
+ 【例子】
11
14
12
15
``` python
13
16
t1 = (1 , 10.31 , ' python' )
@@ -27,21 +30,21 @@ print(tuple2) # (1, 2, 3, 4, 5, 6, 7, 8)
27
30
```
28
31
29
32
- 创建元组可以用小括号 (),也可以什么都不用,为了可读性,建议还是用 ()。
30
- - 元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用:
33
+ - 元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用。
31
34
32
35
【例子】
33
36
34
37
``` python
35
- temp = (1 )
36
- print (type (temp )) # <class 'int'>
37
- temp = 2 , 3 , 4 , 5
38
- print (type (temp )) # <class 'tuple'>
39
- temp = []
40
- print (type (temp )) # <class 'list'>
41
- temp = ()
42
- print (type (temp )) # <class 'tuple'>
43
- temp = (1 ,)
44
- print (type (temp )) # <class 'tuple'>
38
+ x = (1 )
39
+ print (type (x )) # <class 'int'>
40
+ x = 2 , 3 , 4 , 5
41
+ print (type (x )) # <class 'tuple'>
42
+ x = []
43
+ print (type (x )) # <class 'list'>
44
+ x = ()
45
+ print (type (x )) # <class 'tuple'>
46
+ x = (1 ,)
47
+ print (type (x )) # <class 'tuple'>
45
48
```
46
49
47
50
【例子】
@@ -51,30 +54,24 @@ print(8 * (8)) # 64
51
54
print (8 * (8 ,)) # (8, 8, 8, 8, 8, 8, 8, 8)
52
55
```
53
56
54
- 【例子】当然也可以创建二维元组:
57
+ 【例子】创建二维元组。
58
+
55
59
``` python
56
- nested = (1 , 10.31 , ' python' ), (' data' , 11 )
57
- print (nested )
60
+ x = (1 , 10.31 , ' python' ), (' data' , 11 )
61
+ print (x )
58
62
# ((1, 10.31, 'python'), ('data', 11))
59
- ```
60
-
61
-
62
63
63
- 【例子】元组中可以用整数来对它进行索引 (indexing) 和切片 (slicing),不严谨的讲,前者是获取单个元素,后者是获取一组元素。接着上面二维元组的例子,先看看索引的代码。
64
-
65
- ``` python
66
- print (nested[0 ])
64
+ print (x[0 ])
67
65
# (1, 10.31, 'python')
68
- print (nested [0 ][0 ], nested [0 ][1 ], nested [0 ][2 ])
66
+ print (x [0 ][0 ], x [0 ][1 ], x [0 ][2 ])
69
67
# 1 10.31 python
70
- ```
71
68
72
- 【例子】再看看切片的代码。
73
- ``` python
74
- print (nested[0 ][0 :2 ])
69
+ print (x[0 ][0 :2 ])
75
70
# (1, 10.31)
76
71
```
77
72
73
+
74
+
78
75
## 2. 更新和删除一个元组
79
76
80
77
【例子】
@@ -85,36 +82,49 @@ week = week[:2] + ('Wednesday',) + week[2:]
85
82
print (week) # ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
86
83
```
87
84
88
- 【例子】
85
+ 【例子】元组有不可更改 (immutable) 的性质,因此不能直接给元组的元素赋值,但是只要元组中的元素可更改 (mutable),那么我们可以直接更改其元素,注意这跟赋值其元素不同。
89
86
``` python
90
87
t1 = (1 , 2 , 3 , [4 , 5 , 6 ])
91
88
print (t1) # (1, 2, 3, [4, 5, 6])
92
89
93
90
t1[3 ][0 ] = 9
94
91
print (t1) # (1, 2, 3, [9, 5, 6])
95
92
```
96
- 元组有不可更改 (immutable) 的性质,因此不能直接给元组的元素赋值,但是只要元组中的元素可更改 (mutable),那么我们可以直接更改其元素,注意这跟赋值其元素不同。
93
+
97
94
98
95
99
96
## 3. 元组相关的操作符
100
- - 比较操作符
101
- - 逻辑操作符
97
+
98
+ - 等号操作符: ` == `
102
99
- 连接操作符 ` + `
103
100
- 重复操作符 ` * `
104
101
- 成员关系操作符 ` in ` 、` not in `
105
102
106
- 【例子】元组拼接 (concatenate) 有两种方式,用「加号 +」和「乘号 * 」,前者首尾拼接,后者复制拼接。
103
+ 「等号 ==」,只有成员、成员位置都相同时才返回True。
104
+
105
+ 元组拼接有两种方式,用「加号 +」和「乘号 * 」,前者首尾拼接,后者复制拼接。
106
+
107
+ 【例子】
107
108
108
109
``` python
109
- t1 = (2 , 3 , 4 , 5 )
110
- t2 = (' 老马的程序人生' , ' 小马的程序人生' )
111
- t3 = t1 + t2
112
- print (t3)
113
- # (2, 3, 4, 5, '老马的程序人生', '小马的程序人生')
114
-
115
- t4 = t2 * 2
116
- print (t4)
117
- # ('老马的程序人生', '小马的程序人生', '老马的程序人生', '小马的程序人生')
110
+ t1 = (123 , 456 )
111
+ t2 = (456 , 123 )
112
+ t3 = (123 , 456 )
113
+
114
+ print (t1 == t2) # False
115
+ print (t1 == t3) # True
116
+
117
+ t4 = t1 + t2
118
+ print (t4) # (123, 456, 456, 123)
119
+
120
+ t5 = t3 * 3
121
+ print (t5) # (123, 456, 123, 456, 123, 456)
122
+
123
+ t3 *= 3
124
+ print (t3) # (123, 456, 123, 456, 123, 456)
125
+
126
+ print (123 in t3) # True
127
+ print (456 not in t3) # False
118
128
```
119
129
120
130
## 4. 内置方法
@@ -161,6 +171,7 @@ print(rest) # [3, 4]
161
171
【例子】如果你根本不在乎 rest 变量,那么就用通配符「* 」加上下划线「_ 」。
162
172
163
173
``` python
174
+ t = 1 , 2 , 3 , 4 , 5
164
175
a, b, * _ = t
165
176
print (a, b) # 1 2
166
177
```
0 commit comments