Skip to Content
Python实用技能学习指南
book

Python实用技能学习指南

by Posts & Telecom Press, Robert Smallshire, Austin Bingham
February 2024
Intermediate to advanced
316 pages
3h 47m
Chinese
Packt Publishing
Content preview from Python实用技能学习指南

第2章 字符串与集合类型

Python包含丰富的内置集合类型,它们一般可以满足非常复杂的程序,而无须依靠自定义的数据结构。本章会介绍这些基本的集合类型(它们足以让我们编写一些有趣的代码)。我们将在后面的章节中再次讨论这些集合类型,同时还有其他几个集合类型。

本章主要介绍以下这些类型:

  • str ——不可变的Unicode码位序列;
  • bytes ——不可变的字节序列;
  • list ——可变的对象序列;
  • dict ——可变的键值映射。 

本章还将介绍一下Python的for循环。

Python中的字符串数据类型——str,我们已经频繁地使用了它们。字符串是Unicode码位的序列。在大多数情况下,你可以将码位视为类似字符的东西,尽管它们不是严格等同的。Python字符串中的码位序列是不可变的,所以一旦构建了一个字符串,就不能修改它的内容。

码位、字母、字符和符号之间的区别可能令人困惑。我们试着用一个例子来解释一下:希腊字母Σ会被广泛应用于希腊文的写作中,同时,数学家用它来表示序列的求和。字母Σ的这两个用途分别用不同的Unicode字符表示,称为GREEK CAPITAL LETTER SIGMA和N-ARY SUMMATION。通常,使用相同的字母传达不同的信息时,会使用不同的Unicode字符。另一个例子是GREEK CAPITAL LETTER OMEGA和OHM SIGN,这是电阻单位的标志。码位是组成码空间的数值。每个字符与单个码位相关联,因此,把U+03A3分配给了GREEK CAPITAL LETTER SIGMA,同时把U+2211分配给了N-ARY SUMMATION。如上所述,我们通常将码位写成U+nnnn形式,其中nnnn是4位、5位或6位十六进制数。并非所有码位都已分配给字符。例如, ...

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

人工智能技术与大数据

人工智能技术与大数据

Posts & Telecom Press, Anand Deshpande, Manish Kumar
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9781835889626