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

第 3 章 数字 数字

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

能给最多的人带来最大幸福的行动才是最好的行动。

弗朗西斯-赫奇逊

在本章中,我们首先了解 Python 最简单的内置数据类型:

  • 布尔型(其值为TrueFalse)

  • 整数(非小数数字,如42,0,-90100000000)

  • 浮点数(带小数点的数,如3.14159 ,有时也指指数,如1.0e8 ,意思是十的八次方的一倍,或100000000.0)

在某种程度上,这些基本数据类型就像原子,我们将在本章中单独使用它们,在后面的章节中,您将看到如何将它们组合成更大的 "分子",如列表和字典。

每种类型都有特定的使用规则,计算机的处理方式也不尽相同。我还将演示如何使用973.1416 等字面值,以及第 2 章中提到的变量。

本章中的代码示例都是有效的 Python,但它们都是片段。 我们将使用 Python 交互式解释器,键入这些片段并立即看到结果。 请尝试用您计算机上的 Python 版本运行它们。您可以通过>>> 提示认出这些示例。

布尔值

在Python 中,布尔数据类型的值只有TrueFalse 。从本质上讲,这是一个。有时,您会直接使用布尔;有时,您会根据它们的值来评估其他类型的 "真假"。

特殊的 Python 函数bool() 可以将任何 Python 数据类型转换为布尔型。函数 在第 10 章中会有自己的重点,但现在您只需要知道函数有以下几点:

  • 一个名字。

  • 零个或多个逗号分隔的输入参数,用圆括号包围。即使函数没有参数,也需要圆括号 (())。

  • 一个返回值

当我在文中提到一个函数时,我会在它后面加上括号,以帮助您识别它。

bool() 函数接受任何值作为参数,并返回布尔等价物

非零值被视为True

>>> bool(True)
True
>>> bool(1)
True
>>> bool(45)
True
>>> bool(-45)
True

而零值则被视为False

>>> bool(False)
False
>>> bool(0)
False
>>> bool(0.0)
False

第 6章和第 7 章中,您将看到布尔的用途。在后面的章节中,您将看到列表、字典和其他类型如何被视为TrueFalse

整数

整数 是整数--没有分数,没有小数点,没有任何花哨的东西。好吧,除了可能的初始正 (+) 或负 (-) 号。如果你想用小数(10 为基数)以外的其他方式表示数,还有基数;我将在"基数 "中详细介绍。

字面整数

在 Python 中,任何 数字序列都代表一个字面整数

>>> 5
5

纯 0 (0) 是有效的:

>>> 0
0

但是,不能在初始0 后接一个从19 的数字:

>>> 05
  File "<python-input-0>", line 1
    05
    ^
SyntaxError: leading zeros in decimal integer literals are not permitted;
use an 0o prefix for octal integers
>>>
注意

SyntaxError 是一种 特定的 Python 异常。异常警告您输入了违反 Python 规则的内容。在"基础" 中我将解释它的含义 在本书中您将看到更多异常的例子,因为它们是 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

生成式人工智能设计模式

生成式人工智能设计模式

Valliappa Lakshmanan, Hannes Hapke

Publisher Resources

ISBN: 9798341668898