1. Whetting Your Appetite开胃菜¶
If you do much work on computers, eventually you find that there’s some task you’d like to automate. 如果你在计算机上做了很多工作,最终你会发现有一些任务需要自动化。For example, you may wish to perform a search-and-replace over a large number of text files, or rename and rearrange a bunch of photo files in a complicated way. 例如,您可能希望对大量文本文件执行搜索和替换,或者以复杂的方式重命名和重新排列一组照片文件。Perhaps you’d like to write a small custom database, or a specialized GUI application, or a simple game.也许你想编写一个小型的定制数据库,或者一个专门的GUI应用程序,或者一个简单的游戏。
If you’re a professional software developer, you may have to work with several C/C++/Java libraries but find the usual write/compile/test/re-compile cycle is too slow. 如果你是一名专业的软件开发人员,你可能需要使用几个C/C++/Java库,但通常的写/编译/测试/重新编译周期太慢。Perhaps you’re writing a test suite for such a library and find writing the testing code a tedious task. 也许您正在为这样一个库编写测试套件,并发现编写测试代码是一项乏味的任务。Or maybe you’ve written a program that could use an extension language, and you don’t want to design and implement a whole new language for your application.或者你已经编写了一个可以使用扩展语言的程序,你不想为你的应用程序设计和实现一种全新的语言。
Python is just the language for you.Python正是适合你的语言。
You could write a Unix shell script or Windows batch files for some of these tasks, but shell scripts are best at moving around files and changing text data, not well-suited for GUI applications or games. 您可以为其中一些任务编写Unix shell脚本或Windows批处理文件,但shell脚本最擅长在文件中移动和更改文本数据,不太适合GUI应用程序或游戏。You could write a C/C++/Java program, but it can take a lot of development time to get even a first-draft program. 您可以编写一个C/C++/Java程序,但即使是初稿程序也可能需要大量的开发时间。Python is simpler to use, available on Windows, macOS, and Unix operating systems, and will help you get the job done more quickly.Python使用起来更简单,可以在Windows、macOS和Unix操作系统上使用,并且可以帮助您更快地完成工作。
Python is simple to use, but it is a real programming language, offering much more structure and support for large programs than shell scripts or batch files can offer. Python使用简单,但它是一种真正的编程语言,为大型程序提供了比shell脚本或批处理文件多得多的结构和支持。On the other hand, Python also offers much more error checking than C, and, being a very-high-level language, it has high-level data types built in, such as flexible arrays and dictionaries. 另一方面,Python也提供了比C多得多的错误检查,而且作为一种非常高级的语言,它内置了高级数据类型,比如灵活的数组和字典。Because of its more general data types Python is applicable to a much larger problem domain than Awk or even Perl, yet many things are at least as easy in Python as in those languages.由于其更通用的数据类型,Python适用于比Awk甚至Perl更大的问题域,然而在Python中,许多事情至少和在这些语言中一样简单。
Python allows you to split your program into modules that can be reused in other Python programs. Python允许您将程序拆分为可在其他Python程序中重用的模块。It comes with a large collection of standard modules that you can use as the basis of your programs — or as examples to start learning to program in Python. 它附带了大量标准模块,可以作为程序的基础,也可以作为开始学习Python编程的示例。Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk.其中一些模块提供了文件I/O、系统调用、套接字,甚至是Tk等图形用户界面工具包的接口。
Python is an interpreted language, which can save you considerable time during program development because no compilation and linking is necessary. Python是一种解释语言,它可以在程序开发过程中节省大量时间,因为不需要编译和链接。The interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during bottom-up program development. 解释器可以交互使用,这使得在自下而上的程序开发过程中很容易测试语言的功能、编写一次性程序或测试功能。It is also a handy desk calculator.它也是一个方便的台式计算器。
Python enables programs to be written compactly and readably. Python使程序能够简洁易读地编写。Programs written in Python are typically much shorter than equivalent C, C++, or Java programs, for several reasons:用Python编写的程序通常比同等的C、C++或Java程序短得多,原因有几个:
the high-level data types allow you to express complex operations in a single statement;高级数据类型允许您在一条语句中表达复杂的操作;statement grouping is done by indentation instead of beginning and ending brackets;语句分组是通过缩进而不是开始和结束括号来完成的;no variable or argument declarations are necessary.不需要变量或参数声明。
Python is extensible: if you know how to program in C it is easy to add a new built-in function or module to the interpreter, either to perform critical operations at maximum speed, or to link Python programs to libraries that may only be available in binary form (such as a vendor-specific graphics library). Python是可扩展的:如果您知道如何用C编程,就可以很容易地向解释器添加新的内置函数或模块,以最大速度执行关键操作,或者将Python程序链接到可能只有二进制形式的库(例如特定于供应商的图形库)。Once you are really hooked, you can link the Python interpreter into an application written in C and use it as an extension or command language for that application.一旦你真正上瘾了,你可以将Python解释器链接到一个用C编写的应用程序中,并将其用作该应用程序的扩展或命令语言。
By the way, the language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles. 顺便说一句,这种语言是以BBC节目“Monty Python飞行马戏团”命名的,与爬行动物无关。Making references to Monty Python skits in documentation is not only allowed, it is encouraged!在文档中引用Monty Python短剧不仅是允许的,而且是鼓励的!
Now that you are all excited about Python, you’ll want to examine it in some more detail. 既然大家都对Python很感兴趣,那么就需要对它进行更详细的研究。Since the best way to learn a language is to use it, the tutorial invites you to play with the Python interpreter as you read.由于学习一门语言的最佳方法是使用它,本教程邀请您在阅读时使用Python解释器。
In the next chapter, the mechanics of using the interpreter are explained. 在下一章中,将解释使用解释器的机制。This is rather mundane information, but essential for trying out the examples shown later.这是相当普通的信息,但对于后面展示的示例来说是必不可少的。
The rest of the tutorial introduces various features of the Python language and system through examples, beginning with simple expressions, statements and data types, through functions and modules, and finally touching upon advanced concepts like exceptions and user-defined classes.本教程的其余部分通过示例介绍Python语言和系统的各种功能,从简单的表达式、语句和数据类型开始,到函数和模块,最后讨论异常和用户定义类等高级概念。