码迷,mamicode.com
首页 > 编程语言 > 详细

Python内置函数(64)——tuple

时间:2016-12-03 18:47:11      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:rac   error:   不能   span   cal   trace   erro   uil   ons   

英文文档:

  The constructor builds a tuple whose items are the same and in the same order as iterable‘s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged. For example, tuple(‘abc‘) returns (‘a‘, ‘b‘, ‘c‘) and tuple( [1, 2, 3] ) returns (1, 2, 3). If no argument is given, the constructor creates a new empty tuple, ().

 

说明:

  1. 函数功能创建一个新的元组。

  2. 不传入任何参数函数将创建一个空的元组。

#不传入参数,创建空元组
>>> tuple() 
()

  3. 函数可以接收1个可迭代对象作为参数,将使用可迭代对象的每个元素创建一个新的元组。

#传入不可迭代对象,不能创建新的元组
>>> tuple(121)
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    tuple(121)
TypeError: int object is not iterable

#传入可迭代对象。使用其元素创建新的元组
>>> tuple(121)
(1, 2, 1)
>>> tuple([1,2,1])
(1, 2, 1)
>>> tuple((1,2,1))
(1, 2, 1)

  4. 创建新的元组还可以使用一对括号的方式:

  4.1 使用一对括号来创建空的元组。

>>> a= ()
>>> a
()

  4.2 创建单个元素的元组时必须尾随逗号。

>>> a = (1,)
>>> a #a是元组
(1,)
>>> a = (1)
>>> a #a是数值
1

  4.3 创建多个元素的元组,依次用逗号隔开。  

>>> a = (1,2,3)
>>> a
(1, 2, 3)

 

Python内置函数(64)——tuple

标签:rac   error:   不能   span   cal   trace   erro   uil   ons   

原文地址:http://www.cnblogs.com/sesshoumaru/p/6129153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!