Python 入门系列——17. tuple 简介

yumo6665个月前 (05-16)技术文章55

tuple

tuple 常用来将多个 item 放在一个变量中,同时tuple也是python 4个集合类型之一,其他的三个是:List,Set,Dictionary,它们都有自己的用途和场景。

tuple 是一个有序但不可更改的集合,用 () 来表示,如下代码所示:


thistuple = ("apple", "banana", "cherry")
print(thistuple)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
('apple', 'banana', 'cherry')

tuple 项

tuple中的项是有序的,不可更改的,并且可以重复的值,同时tuple中的项也是索引过的,也就是说你可以使用类似 [0][1] 的方式对 tuple 进行访问。

排序

需要注意的是,之所以说 tuple 是有序的,指的是 tuple item 是按照顺序定义的,并且这个顺序是不可更改的。

不可修改

所谓 tuple 不可修改,指的是不可对 tuple 中的item 进行变更。

允许重复

因为 tuple 是索引化的,意味着不同的索引可以具有相同的值,比如下面的代码。


thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
('apple', 'banana', 'cherry', 'apple', 'cherry')

Tuple 长度

要想知道 tuple 中有多少项,可以使用 len() 函数,如下代码所示:


thistuple = ("apple", "banana", "cherry")
print(len(thistuple))

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
3

创建单值的 tuple

要想创建一个单值 tuple,需要在 item 之后添加 , ,否则 python 不会认为这个单值的集合为 tuple,如下代码所示:


thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
<class 'tuple'>
<class 'str'>

Tuple 中的数据类型

tuple中的项可以是任意类型,比如说:string,int,boolean 等等,如下代码所示:


tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)

又或者 tuple 中的 item 是混杂的,比如下面这样。


tuple1 = ("abc", 34, True, 40, "male")

type()

从 python 的视角来看,tuples 就是一个 tuple class 类,如下代码所示:


mytuple = ("apple", "banana", "cherry")
print(type(mytuple))

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
<class 'tuple'>

tuple() 构造函数

尽可能的使用 tuple() 构造函数来生成一个 tuple。


thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
('apple', 'banana', 'cherry')

译文链接:
https://www.w3schools.com/python/python_tuples.asp

更多高质量干货:参见我的 GitHub: python

相关文章

Python语言的12个基础知识点小结

python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序、去重、字典排序、字典、列表、字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进...

全国计算机等级考试二级Python易错真题详解-流程控制-单选题

流程控制(单选题)说明:本文中的题目,全都来自全国计算机等级考试二级Python语言程序设计考试的真题,且都为易错题。题干最后如有编号,则是 python123 平台上的题号,以方便学生查找和索引。在...

零基础入门 Python 内置函数:从基础到进阶的实用指南

一、基础操作函数:编程世界的“瑞士军刀”1. 输入输出的核心:print()与input()print():最常用的输出函数,支持多参数拼接与格式控制:print("Hello, Python...

8-Python内置函数

Python 提供了丰富的内置函数,这些函数可以直接使用而无需导入任何模块。以下是一些常用的内置函数及其示例:1-print()1-1-说明输出指定的信息到控制台。1-2-例子2-len()2-1-说...

避坑!Python函数设计避坑指南:90%新手不知道的高阶技巧

一、开发中的血泪教训场景:函数参数失控导致代码维护困难# 错误案例:参数混杂引发灾难 def process_order(item, price, discount=0.1, tax_rate=0...

一文学会Python中的运算规则!

目录一、基本赋值运算符二、数值运算函数三、数字类型的关系四、附小知识一、基本赋值运算符a +=b => a=a+ba -=b => a=a-ba *=b => a=a*ba /=b...