如果你有很多工作是通过计算机来完成的,那么你一定希望其中的很多事情能够自动地实现。比方说,你希望在文本文件中实现查找和替换的功能,以某一种机制实现照片的重命名以及重新排序的功能,一个小型的数据库甚至是一个自己的游戏。
如果你是一个专业的软件开发者,你要使用诸如C/C++/Java这样的开发语言却发现他们的开发周期太长了(编程/编译/测试/重新编译)。也许你正在为编写这样的一个库或者是一组测试用例而苦恼。或者你曾经已经写过一些能够被其它语言扩展的代码,而你又不希望设计一个全新的语言来实现你的目的。
Python就是那个你苦苦期盼的语言。
你可能会写一些Unix脚本代码或者是Windows的批处理文件,但是脚本语言的优点仅仅在于它能够轻易地移动文件和变化的数据,但却嫩一编写出GUI或者游戏。也许你能够写C/C++/Java程序,但是它们往往会花费你大量的时间在开放上。Python更容易使用,具备很好的兼容性,同时也能够使你的工作尽快地完成。
Python允许你将程序分成可以被复用的模块。它包含了很多的标准模块并且能够作为你程序开发的基本单元,或者作为你学习这门语言的例子。这些模块包含了文件IO,系统调用,端口甚至用户图形界面接口TK。
Python是一种直译式的语言,它能够帮你省去大量的编译与链接的时间。解释器能够被交互式地使用,它使得开发者能够轻松地对语言的特性进行测试,写一些一次性的项目,它也使得调试的过程变得非常轻松。它同时也是一个非常方便的计算器。
Python使得语言简洁而具有很强的可读性。用Python写的程序往往比C,C++,Java写的程序要简短。有以下几个原因:
内置数据类型使得复杂的计算能够通过一个简单的表达式实现
语句通过缩进进行识别从而避免了使用括号的麻烦
不需要变量类型的声明
Python具有很强的可扩展性。如果你会用C编写代码,那么你将很轻松地将代码或模块内嵌至Python程序中,或者对关键部分执行最高效的操作。
这种语言的命名Python是来源于一个BBC的电影” Monty Python’s Flying Circus”而与蟒蛇没有丝毫的联系。将该电影中的一些名字写进程序中不仅仅是可行的,还应该是被鼓励的!
既然你现在对Python这种语言如此兴奋,你一定会希望了解更多的细节。因为学习一种语言的最佳方式就是去运用它,那么这个知道将带您一起走进Python的世界。
在下一章中,我们将介绍解释器的机制。它是很乏味的一种知识,但是却对你将来运行Python程序起到至关重要的作用。
该指导的其余部分将通过例子介绍Python的语言特性。顺序是:
simple expressions, statements and data types
→
functions and modules
→
advanced concepts
[Original]
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.
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.
Python is just the language for you.
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.
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 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 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 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 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 Flying Circus” and has nothing to do with reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!
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.
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.