| Python 表达式 | 结果 | 描述 |
|---|---|---|
| len((1, 2, 3)) | 3 | 计算元素个数 |
| (1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | 连接 |
| ('Hi!',) * 4 | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | 复制 |
| 3 in (1, 2, 3) | True | 元素是否存在 |
| for x in (1, 2, 3): print x, | 1 2 3 | 迭代 |
元组:L = ('Google', 'Taobao', 'Runoob')
| Python 表达式 | 结果 | 描述 |
|---|---|---|
| L[2] | 'Runoob!' | 读取第三个元素 |
| L[-2] | 'Taobao' | 反向读取;读取倒数第二个元素 |
| L[1:] | ('Taobao', 'Runoob!') | 截取元素,从第二个开始后的所有元素。 |
Python元组包含了以下内置函数
| 方法 | 描述 | 实例 |
|---|---|---|
| len(tuple) | 计算元组元素个数。 |
tuple1 = ('Google', 'Runoob', 'Taobao') len(tuple1) 3 |
| max(tuple) | 返回元组中元素最大值。 |
tuple2 = ('5', '4', '8') max(tuple2) '8' |
| min(tuple) | 返回元组中元素最小值。 |
tuple2 = ('5', '4', '8') min(tuple2) '4' |
| tuple(seq) | 将列表转换为元组。 |
list1= ['Google', 'Taobao', 'Runoob', 'Baidu'] tuple1=tuple(list1) tuple1 ('Google', 'Taobao', 'Runoob', 'Baidu') |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24851054/viewspace-2147792/ ,如需转载,请注明出处,否则将追究法律责任。