美文网首页
06 Python 的基本数据类型----元组tuple

06 Python 的基本数据类型----元组tuple

作者: zackxizi | 来源:发表于2018-05-14 16:40 被阅读0次

1. 元组tuple表达式

>>> (1,2,3,"Hello",True)[1]
2
>>> (1,2,3,"Hello",True)[-2]
'Hello'
>>> (1,2,3,"Hello",True)[2:4]
(3, 'Hello')

2. 元组tuple运算

>>> (1,2,3,"Hello",True) +  (1,2,3,"Hello",True)
(1, 2, 3, 'Hello', True, 1, 2, 3, 'Hello', True)
>>> (1,2,3)+(7,6,5)
(1, 2, 3, 7, 6, 5)
>>> (1,2,3)*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)

注意:

>>> type((1))
<class 'int'>

元组内一个只有一个元素的话 会被解析为()运算,就像上面一样的
那怎样显示一个元素的元组呢?如下:

>>> type((1,))
<class 'tuple'>
>>> type(())
<class 'tuple'>

相关文章

网友评论

      本文标题:06 Python 的基本数据类型----元组tuple

      本文链接:https://www.haomeiwen.com/subject/mbihdftx.html