Skip to Content
《学习 Python》第 5 版
book

《学习 Python》第 5 版

by Mark Lutz
May 2025
Intermediate to advanced
1648 pages
23h 5m
Chinese
O'Reilly Media, Inc.
Content preview from 《学习 Python》第 5 版

第 5 章 数值类型 数字类型

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

本章开始我们对 Python 语言的深入学习。在 Python 中,数据以对象的形式存在--或者是Python 提供的内置对象,或者是我们使用 Python 工具和其他语言(如 C 语言)创建的对象。事实上,对象是您将要编写的每个 Python 程序的基础。因为对象是 Python 编程中最基本的概念,所以对象也是我们在本书中首先关注的重点。

在上一章中,我们快速浏览了 Python 的核心对象类型。虽然该章介绍了一些基本术语,但为了节省篇幅,我们还是避免涉及太多细节。在这里,我们将开始对数据类型概念进行更仔细的二次研究,以填补我们之前忽略的细节。让我们从探索第一个数据类型类别开始:Python 的数字类型和操作。

数字类型基础知识

Python 的大多数数字类型都相当典型,如果您过去使用过几乎任何其他编程语言,可能会觉得很熟悉。它们可以用来记录您的银行存款余额、到火星的距离、您网站的访问人数以及几乎所有其他数字。

在 Python 中,"数字 "其实并不是一种单一的对象类型,而是一类类似的类型。Python 支持常用的数字类型(整数和浮点数),以及用于创建数字的字面量和处理数字的表达式。此外,Python 还为更高级的工作提供了更高级的数值编程支持和对象。Python 数字工具箱的完整清单包括

  • 整数和浮点对象

  • 复数对象

  • 十进制:固定精度对象

  • 分数:有理数对象

  • 集合:可进行数字运算的集合

  • 布尔:真和假

  • 内置功能和模块:round,math,random 等。

  • 表达式;无限整数精度;位运算;十六进制、八进制和二进制格式

  • 第三方扩展:矢量、库、可视化、绘图等。

因为列表第一项中的类型在 Python 代码中的应用最多,所以本章从基本的数字和基础知识开始,然后探讨列表中的其他类型,它们都有专门的作用。我们还将在这里学习集合,它同时具有数字和集合的特性,但一般认为前者多于后者。在跳转到代码之前,接下来的几节将简要介绍我们如何在脚本中编写和处理数字。

数字字面

在其基本类型中,Python 提供了整数浮点数,前者是 正整数和负整数,后者是带有小数部分的数(为节省口舌,有时称为 "浮点数")。Python 还允许我们使用十六进制、八进制和二进制字面来编写整数;提供了复数类型;允许整数具有无限的精度--它们可以在内存空间允许的范围内增长到尽可能多的位数。表 5-1显示了 Python 的数字类型在程序中以字面形式或构造函数调用形式写出时的样子。

表 5-1. 数字字面和构造函数
字面意思口译

1234,−24,099999999999999

整数(大小不限

1.23,1.,3.14e-10,4E2104.0e+210

浮点数

0o177,0x9ff0b101010

3.X 中的八进制、十六进制和二进制字面量

0177,0o177,0x9ff0b101010

2.X 中的八进制、八进制、十六进制和二进制字面量

3+4j,3.0+4.0j3J

复数字面

set('spam'),{1, 2, 3, 4}

套件:2.X 和 3.X 结构形式

Decimal('1.0'),Fraction(1, 3)

小数和分数扩展类型

bool(X),TrueFalse

布尔类型和常量

一般来说,Python 的数字类型字面量编写起来很简单,但有几个编码概念值得在此强调一下:

整数和浮点字面

整数是以小数位字符串形式写成的。浮点数有一个小数点和/或一个可选的带符号指数,由eE 引入,后面跟一个可选的符号。如果写入一个带小数点或指数的数,Python ...

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

学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656963