Python: 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. 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. Python : 正合你的口味 如果你做很多在電腦上工作,最終你發現,有一些任務,你想實現自動化。例如,您可能要執行搜尋和替換過大量的文字檔,或在一個複雜的方式重新命名和重新排列了一堆照片檔案。也許你想編寫一個小型的自訂資料庫,或者一個專門的圖形化使用者介面應用程式,或一個簡單的遊戲。
如果你是一個專業的軟體開發人員,你可能有幾個C / C + + / Java庫工作,但發現平時寫/編譯/測試/重編譯周期太慢。也許你正在編寫一個測試套件這樣的庫,並找到一個乏味的任務編寫測試代碼。或者,也許你已經寫了一個程式,可以使用擴充語言,你不想為您的應用程式的設計和實施一種全新的語言。
Python是你的語言。
你可以寫一個Unix shell指令碼或Windows批次檔的某些任務,但是是最好的shell指令碼,在走動檔案和更改文本資料,圖形化使用者介面的應用程式或遊戲並不適合。你可以寫一個C / C + + / Java程式,但它可以採取了很多的開發時間甚至第一方案草案。 Python是便於使用,可在Windows,Mac OS X和Unix作業系統,將協助你得到更快速地完成工作。
Python是使用簡單,但它是一個真正的程式設計語言,提供更多的結構和shell指令碼或批次檔,可以提供比大型程式的支援。另一方面,Python也提供了更多的錯誤檢查比C,是一個非常高層次的語言,它具有高層次的內建資料類型,如靈活的數組和字典。因為擁有更多的通用資料類型,Python是適用於一個更大的問題域比Awk甚至Perl,但至少很多東西都容易在Python這些語言。
Python允許你分割你的程式模組,可以重複使用在其他Python程式。它配備了一個標準的模組,你可以使用你的程式的基礎 - 或作為例子開始學習Python編程的大集合。這些模組提供了一些事情,如檔案I / O,系統調用,插座,甚至像Tk圖形化使用者介面介面。
Python是一種解釋語言,它可以為您節省大量的時間在程式開發過程中是必要的,因為不需要編譯和連結。解譯器可以互動使用,這使得它易於實驗的語言特徵,寫扔掉的節目,在自下而上的程式開發或測試功能。這也是一個方便的案頭計算機。
的Python使編寫應用程式時緊湊和可讀性。 Python寫的程式通常要短得多,比同樣的C,C + +或Java程式,有以下幾個原因:
高層次的資料類型允許你在一條語句中表達複雜的操作;
語句的組織依賴於縮排而不是開始和結束的括弧;
沒有變數或參數聲明是必要的。
Python是可擴充的:如果你知道如何在C程式很容易添加一個新的內建功能或模組的解釋,無論是執行關鍵操作以最大的速度,或連結Python程式庫可能只提供以二進位形式(如供應商特定的圖形庫)。一旦你真的上鉤,你可以連結到一個應用程式用C語言編寫的Python解譯器,並用它作為該應用程式的擴充或命令列語言。
順便說一句,Python語言是根據英國廣播公司(BBC)節目“Monty Python的飛行馬戲團”而命名的,並與爬行動物(Python:巨蟒)無關。不過我們允許並鼓勵你引用文檔中的"巨蟒短劇"!
現在你們都興奮有關Python,你會想要一些更詳細的檢查。由於學習一門語言的最好方式就是使用它,本教程邀請你一起玩的Python解譯器,當你閱讀。
在下一章中,介紹Python解譯器的原理。這是相當普通的資訊,但嘗試後顯示的例子至關重要。
其餘的教程通過執行個體介紹了Python語言和系統的各種功能,開始用簡單的詞彙,語句和資料類型,通過功能和模組,並最終觸及異常和先進的理念,如使用者定義的類。