Skip to Content
Python入门指南, 3rd Edition
book

Python入门指南, 3rd Edition

by Bill Lubanovic
September 2025
Intermediate to advanced
660 pages
7h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Python入门指南, 3rd Edition

第 5 章 字节和字节数组

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

我感觉有点不对劲。

生病的字节

Python 3 引入了以下两种类型的八位整数序列,可能的值从 0 到 255:

  • 不可变的 8 位数值序列:bytes

  • 可变的 8 位数值序列:bytearray

本章将介绍这两种类型的基础知识。第 20 章将更详细地介绍它们在实际生活中的用途,包括二进制文件格式。 与文本字符串相比,您处理二进制数据的次数可能要少得多。

Python 官方文档有关于这些数据类型的所有细节,本章只包括最常用和最有用的数据类型。

下面是字节与字符串的一种思考方式:

  • 字节就像电线上大小相同的珠子。

  • 字符串就像吊坠手链。

字节

字节和字符串一样,是不可变的。创建bytes 值后,就不能追加、插入或更改其内容。

使用引号创建

字面bytes 对象与文本字符串一样由引号包围,但在 初始引号字符前有一个b 。其中的每个字节都指定为 ASCII 字符或两个字符的十六进制字面:

>>> b1 = b'ABC\x01\x02\x03\x41\x42\x43'
>>> b1
b'ABC\x01\x02\x03ABC'
>>> b2 = b'abc\x01\x02\x03\x61\x62\x63'
>>> b2
b'abc\x01\x02\x03abc'

这并不意味着bytes 值可以包含文本字符,而是说您可以用十六进制值ASCII 对应值(如果有的话)来指定字节。 Python 允许使用这种 ASCII 捷径,以方便输入和显示输出。

使用 bytes() 创建

以一个名为blist 的 list (任意值的序列;参见第 8 章) 开始,下一个示例创建了一个名为the_bytesbytes 变量:

>>> blist = [1, 2, 3, 255]
>>> the_bytes = bytes(blist)
>>> the_bytes
b'\x01\x02\x03\xff'

下一个示例演示了不能更改bytes 变量:

>>> blist = [1, 2, 3, 255]
>>> the_bytes = bytes(blist)
>>> the_bytes[1] = 127
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bytes' object does not support item assignment

"字节数组 "你将看到可以改变bytearray 的内部结构。

每个变量都会产生一个 256 元素的结果,值从 0 到 255:

>>> the_bytes = bytes(range(0, 256))

当打印bytesbytearray 数据时,Python 使用\xhh 来表示不可打印的字节1 打印数据时,Python 使用不可打印的字节表示为 "\n",可打印的字节表示为它们的 ASCII 对应字符(加上一些常用的转义字符,例如用 代替\x0a )。下面是the_bytes的打印表示(手动重新格式化为每行显示 16 个字节):

>>> the_bytes
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f
\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f ...
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

生成式人工智能设计模式

生成式人工智能设计模式

Valliappa Lakshmanan, Hannes Hapke

Publisher Resources

ISBN: 9798341668898