【译】Python教程

【译】1. Whetting Your Appetite

2017-09-06  本文已影响6人  sherlywu88

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

如果你需要用计算机做很多工作,最终你会发现总有些工作你想要让他们自动化。举个例子,你可能想要查找并替换大量的文本文件,亦或者用一种复杂的方式重命名或改编一堆照片文件。或者你想要写一个小型数据库,或者是一个专门的图形用户界面应用,或者是一个简单的游戏。

If you’re a professional software developer, you may have to work with severalC/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. Python is just the language for you.

如果你是一个专业的软件开发者,你可能不得不用一些C/C++/java库工作,但是你会发现常用的写/编译/测试/重新编译周期太慢。或许你正在用某个库写一个测试套件,然后你会发现写测试代码是一件非常乏味的工作。或许你可以用扩展语言写一个项目,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脚本是最舍和移动文件和更改文本数据的,不过不大适合图形用户界面应用或者游戏。你可以写一个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 builtin, such as flexible arrays and dictionaries. Because of its more general datatypes 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编程的示例。这些模块里提供了一些特性,像文件输入/输出、系统调用、套接字、甚至连接至图形用户界面的工具包。

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 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:
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编写的程序简洁而有可读性。用Python编写的程序一般比用C,C++或者Java编写的同样的程序要短的多,有些理由如下:
高级数据类型允许你在单个语句中表达复杂的操作。
语句分组通过缩进而不是开始和结束的括号来完成。
不需要变量或参数申明。

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.
By the way, the language is named after the BBC show “Monty Python’s FlyingCircus” and has nothing to do with reptiles. Making references to MontyPython skits in documentation is not only allowed, it is encouraged!

Python是可扩展的:如果你知道如何用C语言编程,那么在解释器中添加一个新的内置函数或者模块是比较容易,要么以最快的速度执行重要操作,要么将python程序链接到可能以二进制形式提供的库(例如供应商特定的图形库)。一旦你真的完成钩子程序,你可以链接Python解释器到C语言写的应用中,然后用它作为扩展或者作为应用的命令语言。顺便一提,该语言的命名是和BBC的蒙提派森的飞天马戏团的节目有关,和爬行动物无关,在文件中参考蒙提派森的短剧不仅是被允许的,还是被鼓励的。

Now that you are all excited about Python, you’ll want to examine it in somemore 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.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充满激情,你将会想要检验更多的细节。一直以来学号一门语言最好的方法就是去使用它,这个教程邀请你在阅读时使用python解释器。在下一章节,将会解释使用解释器的机制。这只是平凡的信息,但是对于尝试弄明白以后出现的示例至关重要。

The rest of the tutorial introduces various features of the Python language and system through examples, beginning with simple expressions, statements and datatypes, through functions and modules, and finally touching upon advanced concepts like exceptions and user-defined classes.

教程剩下的部分通过示例介绍了各个python语言和系统的特性,以简单的表达式、语句和数据类型开始,然后是函数和模块,最后接触像异常和用户定义的类这样的高级概念。

上一篇下一篇

猜你喜欢

热点阅读