Skip to Content
Python for Excel (Chinese Edition), 2nd Edition
book

Python for Excel (Chinese Edition), 2nd Edition

by Felix Zumstein
May 2026
Intermediate
418 pages
5h 50m
Chinese
O'Reilly Media, Inc.
Content preview from Python for Excel (Chinese Edition), 2nd Edition

第 3 章 Python入门

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

安装好 Python 和 VS Code 后,您已具备开始学习所需的一切条件。虽然本章内容仅涉及基础知识,但涵盖的范围依然很广。如果您正处于编程生涯的起步阶段,可能会觉得内容有些难以消化。不过,随着后续章节中通过实际案例应用这些概念,大多数概念都会变得更加清晰,因此如果您第一次没有完全理解某些内容,也无需担心。 如果你是经验丰富的 VBA 开发者,我将通过对比这两种语言的异同并指出注意事项,尽力让你更轻松地过渡到 Python。

本章将从 Python 的基本数据类型(如整数和字符串)开始。随后,我将介绍索引和切片——这是 Python 的核心概念,可让你访问序列中的特定元素。接下来是列表和字典等数据结构,它们能够容纳多个对象。之后我将讲解if语句以及forwhile循环,最后介绍函数和模块,它们能帮助你组织和结构化代码。 作为本章的收尾,我将向您展示如何正确格式化 Python 代码。正如您可能已经猜到的,本章的内容极具技术性。因此,亲自在 Jupyter 笔记本中运行示例是个好主意,这样能让学习过程更加互动且充满趣味。您可以手动输入示例代码,或者通过配套仓库中的 notebookch03.ipynb文件运行它们。

创建新的 Jupyter Notebook

如果您不记得如何创建新的 Jupyter Notebook,请Go 到“运行 Jupyter Notebook”章节,了解如何在浏览器中运行经典的 Jupyter Notebook;或者查看“使用 VS Code 运行 Jupyter Notebook”,了解如何直接在 VS Code 中创建新的 Jupyter Notebook。

数据类型

Python与及其他所有编程语言一样,会通过分配不同的数据类型来区别对待数字、文本、布尔值等。我们最常使用的数据类型是整数、浮点数、布尔值和字符串。在本节中,我将通过几个示例依次介绍它们。不过,为了理解数据类型,我首先需要解释什么是对象。

对象

在Python中,万物皆为对象,包括数字、字符串、函数,以及本章中我们将遇到的其他一切。对象通过提供一组变量和函数,能让复杂的事物变得简单直观。变量存储对象的数据 (通常称为状态),而函数则让你能够执行操作。因此,在进入其他内容之前,请允许我先简要谈谈变量和函数!

变量

在Python 中,变量是通过等号为对象赋予的名称。在下面的示例第一行中,名称a被赋予了对象3

In [1]: a = 3
        b = 4
        a + b
Out[1]: 7

这种写法对所有对象都适用,这比 VBA 更简单——在 VBA 中,等号用于数字和字符串等数据类型,而Set语句用于工作簿或工作表等对象。在 Python 中,只需将变量赋值给一个新的 对象,即可改变其类型。这被称为动态类型

In [2]: a = 3
        print(a)
        a = "three"
        print(a)
Out[2]: 3
        three

与 VBA 不同,Python 区分大小写,因此aA是两个不同的变量。变量名必须遵循特定规则:

  • 它们必须以字母或下划线开头。

  • 它们必须由字母、数字和下划线组成。

在简要介绍了变量之后,让我们来看看如何进行函数调用!

函数

本章后文我会更详细地介绍函数。目前,你只需了解如何调用我们在前面的代码示例中使用的print这样的内置函数。要调用函数,只需在函数名后添加圆括号,并在括号内提供参数,这与数学符号几乎完全一致: ...

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

学习勒索软件响应和恢复 (Chinese Edition)

学习勒索软件响应和恢复 (Chinese Edition)

W. Curtis Preston, Michael Saylor
Prometheus:快速入门,第二版

Prometheus:快速入门,第二版

Julien Pivotto, Brian Brazil

Publisher Resources

ISBN: 0642572396008