python3官方入门指南英文版
More control flow tools f statements for statements The range function break and continue Statements, and else Clauses on Loops pass Statements Defining Functions More on Defining Functions Default Argument values Ke d arguments IS Arbitrary Argument Lists Unpacking Argument Lists Lambda Forms Documentation Strings Intermezzo: Coding style Data structures Morc on lists Using lists as stacks Using Lists as Que List Comprehensions Nested List Comprehensions The del statement Tuples and Sequences Sets Dictionaries Looping technique More on Conditions Comparing Sequences and Other Types Modules · More on modules Executing modules as scripts The Module scarch path ·“ Compiled” Python files Standard modules The dir function Packages Importing From a Package Intra-package references Packages in Multiple Directories Input and Output Fancier Output Formatting · Old string formatting Reading and Writing Files Methods of File obiects The pickle module Errors and Exceptions Syntax Errors Exceptions · Handling except Raising exceptions User-defined Exceptions Defining clean-up actions Predefined Clean-up Actions · Classes a Word about Terminology Python Scopes and Name spaces Scopes and Namespaces Example A first look at classes Class Definition Syntax · Class Objects Instance Obiccts Method objects Random remarks Inheritor Multiple inheritance Private variables Odds and ends Exceptions Are Classes Too Iterators Generators Generator Expressions Bricf Tour of thc Standard library Opcrating Systcm Interfacc File wildcards Command Line arguments Error Output Redirection and Program Termination String Pattern Matching Mathematics Internet access Dates and Times Data Compression Performance measurement Quality control Batteries Included Brief Tour of the standard library- Part II Output formatting Templating Working with Binary Data Record Layouts Multi-threading logging Weak references ools for Working with List Decimal Floating Point Arithmetic What now? Interactive Input Editing and History Substitution Linc editing History Substitution Key bindings commentary Floating Point Arithmetic: Issues and limitations Representation error 2. Whetting Your Appetites 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 program that could use an extension language, and you dont want to design and implement a hole 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 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 app plicable 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-av programs,or to test functions during bottom-up program dcvclopmcnt. It is also a handy dcsk ay calculator Python enables programs to be written compactly and readably. Programs written in Python are ypically 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 of 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). Oncc you arc rcally hooked, you can link thc 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 pythons 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 arc all cxcited about Python, you ll want to examinc it in somc more dctail. 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 nformation, but essential for trying out the examples shown later The rest of the tutorial introduces various features of the Python language and system through xamples, beginning with simple expressions, statements and data types, through functions and modules, and finally touching upon advanced concepts like exceptions and user-defined classes 3. Using the python Interpreters Invoking the Interpreter! The Python interpreter is usually installed as /usr/local/bin/python 3.0 on those machines where it is available; putting /usr/local/bin in your Unix shells search path makes it possible to start it by typing the command python.0 to the shell. [l] Since the choice of the directory where the interpreter lives is an installation option, other places are possiblc; check with your local Python guru or systcm administrator.(E.g,/usr/ local/python is a popular alternative location. On Windows machines, the Python installation is usually placed in C: Python30, though you can change this when you're running the installer. To add this directory to your path, you can type the following command into the command prompt in a dos box set path-pathi C: python30 Typing an end-of-file character(Control-D on Unix, Control-Z on Windows) at the primary prompt causes the interpreter to exit with a zero cxit status. If that docsn't work, you can cxit the interpreter by typing the following commands: import sys; sys. exit() The interpreter's line-editing features usually arent very sophisticated. On Unix, whoever installed the interpreter may have enabled support for the gnu readline library, which adds more elaborate interactive editing and history features. Perhaps the quickest check to see whether command line diting is supported is typing Control-p to the first Python prompt you get. If it bi have command linc editing; scc Appendix Interactive Input Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if p is echoed, command line editing isnt available, youll only be able to use backspace to remove characters from the current line The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device. it reads and executes commands interactively: when called with a file name argument or with a file as standard input, it reads and executes a script from that file A second way of starting the interpreter is python -c command [arg which executes the statement(s)in command, analogous to the shells c option. Since Python statements often contain spaces or othcr characters that are spccial to the shell, it is usually advised to quote command in its entirety with single quotes Some Python modules are also useful as scripts. These can be invoked using python -m module [arg].., which executes the source file for module as if you had spelled out its full name on the command line Note that there is a difference between python file and python
用户评论