GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python 元组 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 基础教程
Python 基础教程 Python 简介 Python 环境搭建 Python 中文编码 Python VS Code Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循环语句 Python While 循环语句 Python for 循环语句 Python 循环嵌套 Python break 语句 Python continue 语句 Python pass 语句 Python Number(数字) Python 字符串 Python 列表(List) Python 元组 Python 字典(Dictionary) Python 日期和时间 Python 函数 Python 模块 Python 文件I/O Python File 方法 Python 异常处理 Python OS 文件/目录方法 Python 内置函数

Python 高级教程

Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3​​.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验
Python 列表(List)
Python 字典(Dictionary)

Python 元组

Python 的元组与列表类似,不同之处在于元组的元素不能修改。

元组使用小括号,列表使用方括号。

元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

如下实例:

实例(Python 2.0+)

tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", "b", "c", "d"

创建空元组

tup1 = ()

元组中只包含一个元素时,需要在元素后面添加逗号

tup1 = (50,)

元组与字符串类似,下标索引从0开始,可以进行截取,组合等。


访问元组

元组可以使用下标索引来访问元组中的值,如下实例:

实例(Python 2.0+)

#!/usr/bin/python tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print "tup1[0]: ", tup1[0] print "tup2[1:5]: ", tup2[1:5]

以上实例输出结果:

tup1[0]:  physics
tup2[1:5]:  (2, 3, 4, 5)

修改元组

元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下实例:

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # 以下修改元组元素操作是非法的。 # tup1[0] = 100 # 创建一个新的元组 tup3 = tup1 + tup2 print tup3

以上实例输出结果:

(12, 34.56, 'abc', 'xyz')

删除元组

元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例:

实例(Python 2.0+)

#!/usr/bin/python tup = ('physics', 'chemistry', 1997, 2000) print tup del tup print "After deleting tup : " print tup

以上实例元组被删除后,输出变量会有异常信息,输出如下所示:

('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print tup
NameError: name 'tup' is not defined

元组运算符

与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。

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 = ('spam', 'Spam', 'SPAM!')
Python 表达式结果 描述
L[2]'SPAM!'读取第三个元素
L[-2]'Spam'反向读取,读取倒数第二个元素
L[1:]('Spam', 'SPAM!')截取元素

无关闭分隔符

任意无符号的对象,以逗号隔开,默认为元组,如下实例:

实例(Python 2.0+)

#!/usr/bin/python print 'abc', -4.24e93, 18+6.6j, 'xyz' x, y = 1, 2 print "Value of x , y : ", x,y

以上实例运行结果:

abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2

元组内置函数

Python元组包含了以下内置函数

序号方法及描述
1cmp(tuple1, tuple2)
比较两个元组元素。
2len(tuple)
计算元组元素个数。
3max(tuple)
返回元组中元素最大值。
4min(tuple)
返回元组中元素最小值。
5tuple(seq)
将列表转换为元组。
AI 思考中...
Python 列表(List)
Python 字典(Dictionary)

8 篇笔记 写笔记

  1. #0

       python

      hah***63.com

    169
    >>> tup1 = ("all")
    >>> print tup1
    all
    
    输出字符串 all,这是因为括号()既可以表示tuple,又可以表示数学公式中的小括号。

    所以,如果元组只有1个元素,就必须加一个逗号,防止被当作括号运算:

    >>> tup1 = ("all",)
    >>> print tup1
    ('all',)
    >>> 
    
    python

       python

      hah***63.com

    10年前 (2017-04-24)
  2. #0

       destiny

      759***[email protected]

    62

    元组与列表的区别,元组它的关键是不可变性。

    如果在程序中以列表的形式传递一个对象的集合,它可能在任何地方改变;

    如果使用元组的话,则不能。

    元组提供了一种完整的约束。

    destiny

       destiny

      759***[email protected]

    10年前 (2017-04-25)
  3. #0

       月将沉

      635***[email protected]

    93

    进阶,可以用切片的方式更新元组:

    >>> temp=(1, 2, 4, 5)
    >>> temp=temp[:2]+(3,)+temp[2:]
    >>> temp
    (1, 2, 3, 4, 5)
    >>>
    月将沉

       月将沉

      635***[email protected]

    9年前 (2018-02-13)
  4. #0

       嘉

      284***[email protected]

    78

    元组的一级元素不可被修改增加删除但可以修改二级后的。

    如修改元祖中列表,字典等内容:

    >>> tu = ("alex", [11, 22, {"k1": 'v1', "k2": ["age", "name"], "k3": (11,22,33)}, 44])
    >>> tu[1][2]["k2"].append("seven")
    >>> print(tu[1][2]["k2"])
    ['age', 'name', 'seven']
    >>>

    添加元祖中的字典中的列表。

    嘉

       嘉

      284***[email protected]

    9年前 (2018-03-18)
  5. #0

       国服第一小乔

      115***[email protected]

    53

    切片虽然可以重新组成新的元组,但是要注意截取一个元素时候不能和新的元组相 +:

    比如:

    >>> a=(1,2,3,4,5,6)
    >>> c=a[1:4]+a[5]    # 报错, a[5] 被当成了整型
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate tuple (not "int") to tuple
    
    >>> c=a[1:4]+a[2:4]   # 这样可以
    >>> c
    (2, 3, 4, 3, 4)
    国服第一小乔

       国服第一小乔

      115***[email protected]

    9年前 (2018-05-08)
  6. #0

       Anofanog

      277***[email protected]

    42

    如果截取的是一个元素,则可以写成:

    >>> T = ('aa','bb','cc','dd','ee')
    >>> T = T[0:3]+T[4:]
    >>> print(T)
    ('aa', 'bb', 'cc', 'ee')
    
    Anofanog

       Anofanog

      277***[email protected]

    8年前 (2018-09-10)
  7. #0

       YsoSeriousSo

      331***[email protected]

    58

    关于元组的截取(补充 @Anofanog 的说明):

    T = ('aa','bb','cc','dd','ee')

    T[4] 得到的是字符串 'ee', 而 T[4:] 得到的是新元组 ('ee',),所以元组拼接时用 T[4] 会报错。

    ps:T[4:4] 获取的值为空。

    YsoSeriousSo

       YsoSeriousSo

      331***[email protected]

    8年前 (2018-09-21)
  8. #0

       又是一个我

      747***[email protected]

    60

    切片更新的不再是原来的元祖,而是生成了一个新的元祖副本:

    >>> temp = (1, 2, 4, 5)
    >>> id(temp)
    43125640L
    >>> temp = temp[:2] + (3,)
    >>> id(temp)
    43308160L
    又是一个我

       又是一个我

      747***[email protected]

    8年前 (2018-12-10)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.