1. Whetting Your Appetite Appetitle 开胃菜

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. 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.

如果你是一名专业的软件开发者,可能你必须使用几种C/C++/JAVA类库,并且发现通常编写/编译/测试/重新编译的周期是如此漫长。 也许你正在为这些类库编写测试用例,但是发现这是一个让人烦躁的工作。 又或者你已经完成了一个可以使用扩展语言的程式,但你并不想为此重新设计并实现一套全新的语言。

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. You could write a C/C++/Java program, but it can take a lot of development time to get even a first-draft program. Python is simpler to use, available on Windows, Mac OS X, and Unix operating systems, and will help you get the job done more quickly.

虽然你能够通过编写Unix shell脚本或Windows批处理文件来处理其中的某些任务,但Shell脚本更适合移动文件或修改文本数据,并不适合编写GUI应用程序或游戏; 虽然你能够使用C/C++/JAVA编写程序,但即使编写一个简单的 first-draft 程序也有可能耗费大量的开发时间。 相比之下,Python更易于使用,无论在Windows、Mac OS X或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. 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. 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易于使用,但它却是一门完整的编程语言;与Shell脚本或批处理文件相比,它为编写大型程序提供了更多的结构和支持。 另一方面,Python提供了比C更多的错误检查,并且作为一门 高级语言 ,它内置支持高级的数据结构类型,例如:灵活的数组和字典。 因其更多的通用数据类型,Python比Awk甚至Perl都适用于更的多问题领域,至少大多数事情在Python中与其他语言同样简单。

Python allows you to split your program into modules that can be reused in other Python programs. 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. Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk.

Python允许你将程序分割为不同的模块,以便在其他的Python程序中重用。 Python内置提供了大量的标准模块,你可以将其用作程序的基础,或者作为学习Python编程的示例。 这些模块提供了诸如文件I/O、系统调用、sockets支持,甚至类似Tk的用户图形界面(GUI)工具包接口。

Python is an interpreted language, which can save you considerable time during program development because no compilation and linking is necessary. 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是一门解释型语言,因为无需编译和链接,你可以在程式开发中节省宝贵的时间。 Python解释器可以交互的使用,这使得试验语言的特性、编写临时程序或在自底向上的程序开发中测试方法非常容易。 你甚至还可以把它当做一个桌面计算器。

Python enables programs to be written compactly and readably. Programs written in Python are typically much shorter than equivalent C, C++, or Java programs, for several reasons:

Python让程序编写的紧凑和可读。 用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). 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语言编程便可以轻易的为解释器添加内置函数或模块,或者为了对性能瓶颈作优化,或者将Python程序与只有二进制形式的库(比如某个专业的商业图形库)连接起来。 一旦你真正掌握了它,你可以将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. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!

顺便说一句,这个语言的名字来自于BBC的“Monty Python’s Flying Cirecus”节目,和爬行类动物没有任何关系。 在文档中引用Monty Python的典故不仅可行,而且值得鼓励!

Now that you are all excited about Python, you’ll want to examine it in some more detail. 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兴奋不已了吧,大概想要领略一些更多的细节! 学习一门语言最好的方法就是使用它,本指南推荐你边读边使用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.

下一节中,我们将解释Python解释器的用法。 这是很简单的一件事情,但它有助于试验后面的例子。

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语言及系统的诸多特性,开始是简单的语法、数据类型和表达式,接着介绍函数与模块,最后涉及异常和自定义类这样的高级内容。

Previous topic

The Python Tutorial Python 入门指南

Next topic

2. Using the Python Interpreter 使用Python解释器

This Page