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

第 6 章 如果和匹配 如果和匹配

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

如果你能在周围的人 当你周围的人都失去理智并把责任归咎于你时,如果你能保持头脑清醒...

鲁德亚德-吉卜林,"如果--"

在前面的章节中,您已经看到了许多数据示例,但还没有对它们做太多的处理。 大多数代码示例都使用了交互式解释器,而且很简短。 在本章中,您将学习如何结构化 Python代码,而不仅仅是数据。

许多计算机语言使用大括号 ({}) 等字符或beginend等关键字来标记代码部分。在这些语言中,使用一致的缩进是一种好的做法,这样可以使你的程序更便于自己和他人阅读。甚至还有一些工具可以使你的代码排得很整齐。

在设计后来成为 Python 的语言时,Guido van Rossum 认为缩进本身就足以定义程序的结构,因此避免键入所有的括号和大括号。 Python 在使用空白定义程序结构方面是与众不同的 。 这是新手最先注意到的方面之一,对于那些有其他语言经验的人来说,这似乎很奇怪。 事实证明,在写了 Python 一段时间后,这种结构感觉很自然,你就不会再注意到它了。 你甚至会习惯于做更多的事情,同时键入更少的内容。

我们最初的代码示例都是单行的,让我们先看看如何编写注释和多行命令。

带 # 的评论

注释是您程序中的一段文本,Python 解释器会忽略它。您可以使用注释来澄清附近的 Python 代码,或者给自己做注释以便将来修正某些问题,或者用于任何您喜欢的目的。您可以使用# 字符来标记注释;从该字符开始到当前行结束的所有内容都是注释的一部分。 您通常会看到注释单独出现在一行中,如图所示:

>>> # 60 sec/min * 60 min/hr * 24 hr/day
>>> seconds_per_day = 86400

或者与注释代码在同一行:

>>> seconds_per_day = 86400 # 60 sec/min * 60 min/hr * 24 hr/day

# 字符有很多名字:hashsharppound 或听起来很邪恶的octothorpe1 不管你叫它什么、2 它的作用只持续到它所在行的末尾。

Python 没有多行注释,您需要明确地以# 开始每行或每节注释:

>>> # I can say anything here, even if Python doesn't like it,
... # because I'm protected by the awesome
... # octothorpe.
...
>>>

但是,如果它出现在文本字符串中,强大的八角形字符就会恢复其作为一个普通的# 字符的角色:

>>> print("No comment: quotes make the # harmless.")
No comment: quotes make the # harmless.

用"...... "继续行\

如果行比较短,程序 的可读性会更好。推荐的(不是必须的)最大行长是 80 个字符。如果您不能在这个长度内说出您想说的所有内容,您可以 使用续行符\ (反斜线)。只要在行的末尾加上\ ,Python 就会突然像您还在同一行上一样。

例如,如果我想添加前五位数字,我可以一行一行地添加:

>>> sum = 0
>>> sum += 1
>>> sum += 2
>>> sum += 3
>>> sum += 4
>>> sum ...
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