The Python Tutorial 3.2-1Whetting Your Appetite(开胃菜)

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.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 extensionlanguage, 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程序,但即使编写一个简单的初始草稿程序也有可能耗费大量的开发时间。相比之下,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 asflexible 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 useas 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, andeven 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 programdevelopment. 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 reallyhooked, 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 indocumentation 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语言及系统的诸多特性,从简单的语法、数据类型和表达式开始,接着介绍函数与模块,最后涉及像异常和自定义类这样的高级内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值