`

Python---Tuple special characteristics

 
阅读更多
A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective. For example:

>>> empty = ()
>>> singleton = 'hello',    # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)


The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:

>>> x, y, z = t


This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
分享到:
评论

相关推荐

    beginning-python-3ed-master_python教程_python教程_

    此外,还有列表(list)、元组(tuple)、字典(dict)和集合(set)等复合类型。 - 控制流程:包括条件语句(if-elif-else)、循环(for、while)以及异常处理(try-except)。 - 函数:Python中函数是一等公民,...

    Python-3.5.9.tgz

    此外,这个版本还添加了新的数据类型,如集合(Set)和字典(Dictionary)的新的方法,以及对元组解包(Tuple Unpacking)的扩展,使得代码更加灵活和易读。 Python 3.5.9的发布修复了许多已知的bug,增强了语言的...

    Python代码-set-list-tuple三者类型间转换.py

    ——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!

    Python34中文手册(官方文档).docx

    - **元组(Tuple)**:不可变的有序元素集合。 - **字典(Dictionary)**:无序的键值对集合。 - **集合(Set)**:无序且不重复的元素集合。 #### 十二、流程控制 - **if语句**:用于条件判断。 - **for循环**:遍历序列...

    python-3.6.2-docs-pdf-a4

    此外,还有列表(list)、元组(tuple)、字典(dict)和集合(set)等复合数据结构。 - **语法结构**:Python采用缩进作为代码块的分隔,提供了if-else语句、for循环、while循环、函数定义(def)以及类定义...

    The-Python-3-Standard-Library-by-Example.rar

    包括数据结构如列表(list)、元组(tuple)、字典(dict)和集合(set),以及基本的数学运算和类型转换。例如,`math` 模块提供了数学函数,如平方根、对数和三角函数;`random` 模块则提供了随机数生成的功能,可...

    python-2.7.15.zip

    10. **丰富的数据结构**:Python 内置了列表(list)、元组(tuple)、字典(dict)等高效的数据结构,方便处理各种复杂的数据。 在Python 2.7.15版本中,包含了一些重要的改进和修复,比如对一些库的更新、性能...

    python-3.10.7-amd64.zip

    - **结构化赋值(Tuple Unpacking)**:现在可以在赋值语句中使用更多的结构化模式,使得解构更简洁。 - **匹配语句(Match Statement)**:这是一种新的控制流语句,用于模式匹配,类似switch-case语句,但更强大...

    Basic-Python-Tuple-Parser

    "Basic-Python-Tuple-Parser"项目可能是一个用来解析和处理Python元组的工具或教程,旨在帮助初学者理解和操作Python中的元组。 【描述】:“#Basic-Python-Interpreter” 描述中提到的“#Basic-Python-...

    python-3.86 文档 手册 中文版.zip

    1. 数据类型:Python支持多种数据类型,如整型(int)、浮点型(float)、字符串(str)、布尔型(bool)、列表(list)、元组(tuple)、集合(set)和字典(dict)。了解这些类型及其操作方法是Python编程的基础。...

    征服Python--语言基础与典型应用

    此外,还有列表(list)、元组(tuple)、字典(dict)和集合(set)等复合数据结构,这些是Python编程中的基本元素。 2. 控制流:Python的控制流语句包括条件语句(if...else...)、循环语句(for...in...,while...

    Python-Docs-2.5

    内置类型如列表(list)、元组(tuple)、字典(dict)等,它们构成了Python的基础数据结构。模块是组织代码的基本单位,通过导入机制,你可以复用和组合代码。函数是可重复使用的代码块,而类则是面向对象编程的...

    Python-一些Python示例

    其次,Python支持多种数据类型,包括基本的整型(int)、浮点型(float)、字符串(str)以及复合类型如列表(list)、元组(tuple)、字典(dict)和集合(set)。例如,列表允许动态大小调整和元素的增删,而元组则为不可变序列...

    Python-Func_python_

    还有列表(list)、元组(tuple)、字典(dict)和集合(set)等复合类型。了解如何创建、操作和转换这些类型对于编写有效的Python代码至关重要。 4. **模块与导入**:Python的模块系统允许将代码组织在不同的文件...

    python-3.9.6-amd64.rar

    例如,`a, *b, c = some_tuple`。 - **字典合并操作:** 字典现在支持`|`操作符,允许直接合并两个字典,如`dict1 | dict2`。 - **增强的类型注解:** 类型注解更加强大,支持对函数参数和返回值的类型限制,有助...

    python-3.7.4-docs-html.zip

    5. **标准类型**:详细介绍了Python中的各种数据类型,包括序列(list、tuple、bytearray)、映射(dict)、集合(set、frozenset)以及异步I/O模型。 6. **标准模块**:除了标准库参考外,这里还单独列出了标准...

    python-2.7.rar

    2. **数据类型**:Python 2.7提供了丰富的数据类型,如整型(int)、浮点型(float)、字符串(str)、列表(list)、元组(tuple)、字典(dict)等。其中,字符串在2.7中区分单引号和双引号,但2.7不支持Python 3...

    零基础学Python-1.pdf

    - **元组 (tuple):** 不可变序列。 - **序列操作:** - **成员关系操作:** `in`, `not in`。 - **连接操作:** `+`。 - **重复操作:** `*`。 - **切片操作:** `[start:end]`。 #### 计算生肖和星座案例 - ...

    python 基础教程 第三版随书附赠源代码文件

    Python内置了多种数据结构,包括列表(list)、元组(tuple)、集合(set)和字典(dictionary)。这些数据结构在源代码文件中会被广泛使用,例如,你可以看到如何通过索引访问列表元素,如何使用元组的不可变性,以及如何...

    最新版 python-3.9.0-amd64.zip

    - **新增语法**:Python 3.9引入了可选类型注解的空元组类型`tuple[]`,它等价于`tuple()`,用于明确表示无元素的元组。 - **字典合并**:字典现在支持通过`|`运算符进行合并,如`dict1 | dict2`,这简化了合并多...

Global site tag (gtag.js) - Google Analytics