Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
76
7
元组和列表
人这种“动物”与低等灵长类动物的不同之处在于前者对于清单的热情。
——
H. Allen
Smith
在前几章中,我们接触了一些
Python
的基本数据类型
:布尔型、整型、浮点型和字符串
型。如果你将其视为原子,那么本章要介绍的数据结构就像是分子。也就是说,我们会将
这些基本类型以更复杂的方式组合起来。你每天都会用到这些数据结构。很多编程工作涉
及拆分数据,再以特定的形式将它们粘合起来,而本章就是教你如何使用钢锯和胶枪。
大多数计算机编程语言可以描述一系列条目,其中的各个条目由其所处的整数位置进行索
引:第
1
个、
2
个……直到最后一个。你之前已经见过了由字符序列组成的
Python
符串。
Python
还有另外两种序列结构
:元组和列表。两者包含
0
个或多个元素。不同于字符串,
元组和列表中的元素可以是不同的类型。事实上,每个元素可以是任意的
Python
对象
。这
允许你根据需要创建复杂的结构。
为什么
Python
既有列表
,又有元组呢?元组是不可变的,当你将元素分配(仅一次)给元
组时,它们就已经固定下来了,无法再被更改。列表是可变的,这意味着你可以随意插入
和删除列表元素。本章会分别展示大量关于这两种结构的示例,不过会将重点放在列表上。
7.1
元组
说一点儿题外话。你可能听到过“元组”(
tuple
)这个词的两种发音,究竟哪种是对的?
是不是担心如果自己念错了,会被别人当作是个
Python
冒牌货
?别担心。
Python
之父
Guido van Rossum
T
witter
上聊过这件事。
元组和列表
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
量子计算机编程:从入门到实践

量子计算机编程:从入门到实践

Eric R. Johnston, Nicholas Harrigan, Mercedes Gimeno-Segovia
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787115586223