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 版

第 10 章 Python 语句介绍 Python 语句介绍

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

现在您已经熟悉了 Python 的核心内置对象类型,本章开始我们对其基本语句形式的探索。与上一部分一样,我们将从语句语法的一般介绍开始,在接下来的几章中,我们将介绍有关特定语句的更多细节。

简单地说,语句是用来告诉 Python 程序应该做什么的东西。如果像第 4 章所说的那样,程序是 "用东西做事情",那么语句就是指定程序做什么事情的方式。不那么正式地说,Python 是一种基于语句的过程式语言;通过组合语句,您可以指定 Python 为实现程序目标而执行的过程

重新审视 Python 概念层次结构

理解语句作用的另一种方法是重温第 4 章中介绍的概念层次,其中谈到了内置对象和用于操作它们的表达式。本章将把层次结构提升到 Python程序结构的下一个层次:

  1. 程序由模块组成。

  2. 模块包含语句。

  3. 语句包含表达式

  4. 表达式创建和处理对象。

从根本上说,用 Python 语言编写的程序是由语句和表达式组成的。表达式处理对象并嵌入到语句中。此外,语句是对象产生的地方(例如赋值语句中的表达式),有些语句会创建全新的对象(函数、类等)。在顶层,语句总是存在于模块中,而模块本身也是由语句管理的。

Python 的声明

表 10-1 总结了 Python 的语句集。Python 中的每条语句都有其特定的目的和特定的语法--即定义其结构的规则--不过,正如我们将看到的,许多语句有共同的语法模式,有些语句的作用是重叠的。表 10-1还给出了根据语法规则编码的每种语句的示例。在你的程序中,这些代码单元可以执行操作、重复任务、做出选择、构建更大的程序结构,等等。

本书的这一部分将讨论表10-1中从上到下的条目breakcontinue 。我们已经非正式地了解了表 10-1中的一些语句;本书的这一部分将填补前面跳过的细节,介绍 Python 过程语句集的其余部分,并涵盖整个语法模型。表 10-1中较低的语句与较大的程序单元--函数、类、模块和异常--有关,它们引出了更大的编程思想,因此它们将有自己的章节。更集中的语句 (如del ,删除各种组件) 将在本书的其它部分或 Python 标准手册中介绍。

表 10-1. Python 语句
声明角色示例

任务

创建引用

a, b = 'good', 'bad'

通话和其他表达方式

运行功能

log.write("spam, ham")

print 电话

打印对象

print('The Killer', joke)

if/elif/else

选择 操作

if "python" in text:
    print(text)

for/else

ation

for x in mylist:
    print(x)

while/else

一般循环

while X > Y:
    print('hello')

pass

空占位符

while True:
    pass

break

环路出口

while True:
    if exittest(): break

continue

循环继续

while True:
    if skiptest(): continue

def

功能和方法

def f(a, b, c=1, *d):
    print(a+b+c+d[0])

return

功能结果

def f(a, b, c=1, *d):
    return a+b+c+d[0]

yield

发电机功能

def gen(n):
    for i in n: yield i*2

global

名称空间

x = 'old' def ...
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