Prentice hall core python programming 2nd edition sep 2006 ISBN 0132269937 pdf

703 115 0
Prentice hall core python programming 2nd edition sep 2006 ISBN 0132269937 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

1 IT-SC book: Core Python Programming Core Python Programming Wesley J Chun Publisher: Prentice Hall PTR First Edition December 14, 2000 ISBN: 0-13-026036-3, 816 pages IT-SC book: Core Python Programming Review New to Python? This is the developer's guide to Python development! Learn the core features of Python as well as advanced topics such as regular expressions, multithreaded programming, Web/Internet and network development, GUI development with Tk(inter) and more Also includes features found in the new Python 1.6 and 2.0 releases CD-ROM: Complete Python distributions (source code, documentation, and various binaries) plus all example scripts in the book Python is an Internet and systems programming language that is soaring in popularity in today's fast-paced software development environment, and no wonder: it's simple (yet robust), object-oriented (yet can be used as a procedural language), extensible, scalable and features an easy to learn syntax that is clear and concise Python combines the power of a compiled object language like Java and C++ with the ease of use and rapid development time of a scripting language In fact, its syntax is so easy to understand that you are more likely to pick it up faster than any of the other popular scripting languages in use today! In Core Python Programming, Internet software engineer and technical trainer Wesley Chun provides intermediate and experienced developers all they need to know to learn Python-fast Like all Core Series books, Core Python Programming delivers hundreds of industrial-strength code snippets and examples, all targeted at professional developers who want to leverage their existing skills! In particular, Core Python Programming presents numerous interactive examples that can be entered into the Python interpreter right in front of you! Finally, we present a chapter that shows you step-by-step how to extend Python using C or C++ Python syntax and style Development and Run-time Environments Objects and Python memory management Standard data types, methods, and operators Loops and conditionals IT-SC book: Core Python Programming Files and Input/Output Exceptions and error handling Functions, scope, arguments, and functional programming Importing modules and module attributes Object-oriented Programming with classes, methods, and instances Callable Objects Extending Python Coverage of the Python standard module library and client-server application development includes comprehensive introductions to the following topics in Python programming: Regular expressions TCP/IP and UDP/IP Network programming using sockets Operating system interface GUI development with Tk using Tkinter Multithreaded programming Interactive Web/CGI/Internet applications Executing code in a restricted environment Inheritance, type emulation, operator overloading, and delegation in an OOP environment Finally, we provide an introduction to the new features introduced in Python 1.6 These include Unicode string support, the new function invocation syntax which lets the caller provide a tuple of positional arguments and/or a dictionary of keyword arguments, and the new string methods We also provide a glimpse into features that will only be found in the newer 2.0 release Every Core Series book: DEMONSTRATES how to write commercial-quality code FEATURES dozens of programs and examples! FOCUSES on the features and functions most important to real developers PROVIDES objective, unbiased coverage of cutting-edge technologies-no IT-SC book: Core Python Programming hype! Core Python Programming delivers: Coverage of the core parts of the Python language Real-world insights for developing Web/Internet, network, multithreaded and GUI applications Tables and charts detailing Python modules, built-in functions, operators, and attributes Code snippets to try live with Python's interactive interpreter, hammering the concepts home Extensive code examples-including several complete sample applications CD-ROM includes complete Python source code and documentation distributions for Unix/Linux along with binaries for Windows and Macintosh platforms plus source code for all examples in the book IT-SC book: Core Python Programming Library of Congress Cataloging-in-Publication Date Chun, Wesley Core python / Wesley Chun p cm Includes bibliographical references and index ISBN 0-13-026036-3 Python (Computer program language) I Title QA76.73.P98 C48 2000 005.13'3 dc21 00-047856 IT-SC book: Core Python Programming Copyright Information © 2001 Prentice Hall PTR Prentice-Hall, Inc Upper Saddle River, NJ 07458 The publisher offers discounts on this book when ordered in bulk quantities For more information, contact Corporate Sales Department, Prentice Hall PTR One Lake Street Upper Saddle River, NJ 07458 Phone: 800-382-3419; FAX: 201-236-7141 E-mail (Internet): corpsales@prenhall.com All products or services mentioned herein are the trademarks or service marks of their respective companies or organizations All rights reserved No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher Printed in the United States of America 10 Prentice-Hall International (UK) Limited, London Prentice-Hall of Australia Pty Limited, Sydney Prentice-Hall Canada Inc., Toronto Prentice-Hall Hispanoamericana, S.A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Pearson Education P.T.E., Ltd To my parents, who taught me that everybody is different IT-SC book: Core Python Programming And to my wife, who lives with someone who is different IT-SC book: Core Python Programming Table of Contents Welcome to Python! Style:Technical, Yet Easy Reading Author's Experience with Python Book Contents Part I : Core Python Chapter —Welcome to Python! Chapter —Getting Started Chapter —Syntax and Style Chapter —Python Objects Chapter —Numbers Chapter —Sequences: Strings, Lists, and Tuples Chapter —Dictionaries Chapter —Conditionals and Loops Chapter —Files and Input/Output Chapter 10 —Errors and Exceptions Chapter 11 —Functions Chapter 12 —Modules Chapter 13 —Classes and OOP Chapter 14 —Execution Environment Part II : Advanced Topics Chapter 15 —Regular Expressions Chapter 16 —Network Programming with Sockets Chapter 17 —Multithreaded Programming Chapter 18 —GUI Programming with Tkinter Chapter 19 —Web Programming Chapter 20 —Extending Python Optional Sections Conventions Book Support Acknowledgements I: CORE PYTHON Welcome to Python! What Is Python? History of Python Features of Python Obtaining Python Obtaining Python Installing Python Running Python Python Documentation Comparing Python JPython and Some Nomenclature Exercises Getting Started Program Output, the print Statement, and "Hello World!" Program Input and the raw_input() Built-in Function IT-SC book: Core Python Programming Comments Operators Variables and Assignment Numbers Strings Lists and Tuples Dictionaries Code Blocks Use Indentation if Statement while Loop for Loop and the range() Built-in Function Files and the open() Built-in Function Errors and Exceptions Functions Classes Modules Exercises Syntax and Style Statements and Syntax Variable Assignment Identifiers Basic Style Guidelines Memory Management First Python Application Exercises Python Objects Python Objects Standard Types Other Built-in Types Internal Types Standard Type Operators Standard Type Built-in Functions Categorizing the Standard Types Unsupported Types Exercises Numbers Introduction to Numbers Integers Floating Point Real Numbers Complex Numbers Operators Built-in Functions Related Modules Exercises Sequences: Strings, Lists, and Tuples Sequences Strings Strings and Operators String-only Operators 10 IT-SC book: Core Python Programming PythonLabs (commercial home page) http://www.pythonlabs.com/ PythonWare http://www.pythonware.com ReportLab http://www.reportlab.com/ UC Santa Cruz Extension Scripting Language Courses http://www.ucsc-extension.edu/to/software/silang.html Communication Bay Area Python Interest Group http://www.baypiggies.org/ comp.lang.python weekly newsgroup summaries http://purl.org/thecliff/python/url.html Python Conferences http://www.python.org/workshops/ Python links (sizeable hotlist) http://www.cetus-links.de/oo_python.html Python mailing lists http://www.python.org/mailman/listinfo Python Special Interest Groups http://www.python.org/sigs Starship FAQ http://starship.python.net/~tbryan/FAQ/Starship/ Starship Python http://starship.python.net Core 689 IT-SC book: Core Python Programming JPython http://www.jpython.org/ Python.org (community home page) http://www.python.org/ DBs Databases http://www.python.org/topics/database/ Python database modules http://www.python.org/topics/database/modules.html Python DB API 2.0 specification http://www.python.org/topics/database/DatabaseAPI-2.0.html"> Extending Extending and Embedding reference http://www.python.org/doc/current/ext/ext.html Python-C API http://www.python.org/doc/current/api/api.html SWIG (Simple Wrapper and Interface Generator) http://www.swig.org GUIs (with Python development interfaces) Gimp-Python http://www.daa.com.au/~james/pygimp Glade (GTK+ UI builder) http://glade.pn.org GLC (Glade Python Code Generator) http://glc.sourceforge.net 690 IT-SC book: Core Python Programming GTK+ (GIMP Toolkit) http://www.gtk.org KDE (K Desktop Environment) http://www.kde.org PMW (Python MegaWidgets for Tkinter) http://www.dscpl.com.au/pmw/ PyG Tools (PyGTK, PyGNOME, etc.) http://www.bioinformatics.org/pygtools PyGTK Module http://www.daa.com.au/~james/pygtk PyQt-PyKDE http://www.thekompany.com/projects/pykde Python-KDE Tutorial http://www.xs4all.nl/~bsarempt/python/tutorial.html Tkinter (Python-Tk) http://www.python.org/topics/tkinter Tkinter intro (F Lundh) http://www.pythonware.com/library/tkinter/introduction TrollTech Qt products (commercial) http://www.trolltech.com/products wxPython http://www.wxpython.org/ Macintosh Macintosh Library Modules http://www.python.org/doc/current/mac/mac.html MacPython 691 IT-SC book: Core Python Programming http://www.cwi.nl/~jack/macpython.html MacPython download page http://www.python.org/download/download_mac.html Open Directory MacPython links http://dmoz.org/computers/systems/macintosh/development/languages/python News Python Events http://www.python.org/Events.html Python mailing lists http://www.python.org/mailman/listinfo Python News http://www.python.org/News.html Numerical/Scientific Processing NumPy numerical extensions http://www.python.org/topics/scicomp/numpy.html NumPy source(SourceForge) http://sourceforge.net/project/?group_id=1369 Programming Comparing Python to … http://www.python.org/doc/Comparisons.html Computer Programming for Everybody (CP4E) http://www.python.org/cp4e/ CP4E proposal paper http://www.python.org/doc/essays/cp4e.html Empirical language comparison paper 692 IT-SC book: Core Python Programming http://wwwipd.ira.uka.de/~prechelt/Biblio/jccpprtTR.pdf Guido's CP4E talk http://www.python.org/doc/essays/ppt/acm-cp4e/ Instant Hacking: Learning to Program with Python http://www.idi.ntnu.no/~mlh/python/programming.html Instant Python (crash course in Python) http://www.idi.ntnu.no/~mlh/python/instant.html Learning to Program http://members.xoom.com/alan_gauld/tutor/tutindex.htm Non-Programmers Tutorial http://www.honors.montana.edu/~jjc/easytut/easytut/ Reference Python documentation http://www.python.org/doc FAQ (Frequently Asked Questions) http://www.python.org/doc/FAQ.html FAQTS Python Knowledge Base http://python.faqts.com Global Module Index for the Python Standard Library http://www.python.org/doc/current/modindex.html Language Reference Manual http://www.python.org/doc/current/ref/ref.html Library Reference http://www.python.org/doc/current/lib/lib.html Python-Perl Cookbook http://starship.python.net/crew/da/jak/cookbook.html 693 IT-SC book: Core Python Programming Quick Reference Guide http://starship.python.net/quick-ref1_52.html Regular Expressions HOWTO http://www.python.org/doc/howto/regex/regex.html Releases Python 1.5 to 1.5.2 http://www.python.org/1.5 Python 1.6 http://www.python.org/1.6 Python 2.0 http://www.pythonlabs.com/products/python2.0 Python Download http://www.python.org/download Python FTP site ftp://ftp.python.org What's New in 2.0 http://starship.python.net/crew/amk/python/writing/new-python Unicode Python Unicode Integration (M.A Lemburg) http://starship.python.net/crew/lemburg/unicode-proposal.txt Python Unicode Tutorial http://www.reportlab.com/i18n/python_unicode_tutorial.html Unicode Standard home page http://www.unicode.org/ Web 694 IT-SC book: Core Python Programming Five Minutes to a Python CGI (D Mertz, Web Review) http://webreview.com/pub/2000/07/07/feature/index02.html HTMLgen home page http://starship.python.net/crew/friedrich/HTMLgen/html/ Web programming http://www.python.org/topics/web/ Writing CGI Programs in Python (P Landers, Dev Shed) http://www.devshed.com/Server_Side/Python/CGI/print.html XML Annotated XML 1.0 Specification http://www.xml.com/axml/axmlintro.html Python-XML How-To http://www.python.org/doc/howto/xml/ Python-XML reference http://www.python.org/doc/howto/xml-ref/ XML http://www.python.org/topics/xml/ XML Cover Pages http://www.oasis-open.org/cover/ XML FAQ http://www.ucc.ie/xml/ Zope http://www.zope.org/ 695 IT-SC book: Core Python Programming Python Operator Summary TableC.1 represents the complete set of Python operators and to which standard types they apply The operators are sorted from highest-to-lowest precedence, with those sharing the same shaded group having the same priority Operator [] Int long Table c.1 Python Operators († - unary) float complex string list tuple dictionary • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • is • • • • • • • • • • • • • • • • is not • • • • • [:] ** • • • • +† • • • • -† • • • • ~† * • • • • • • / • • • • % • • • • • + • • • • - • • • • > • • & • • ^ • • | • • < • • • > • • = == != • • • in • • • not in • • • not† and • • • • • • • • • • • • • • • • or • • • • • • • • 696 IT-SC book: Core Python Programming What's New in Python 2.0? Introduction During the creation process of this text, the Python development team has been hard at work producing Python 2.0, which at press time, is finally being released to the public The supplementary CD-ROM in the back of the book contains the three most current releases of Python: 1.5.2, 1.6, and 2.0, including the most recent Java version of the Python interpreter, JPython (a.k.a Jython): 1.1 1.5.2 has been a rock stable release for almost two years, and is the foundation for most of the content in the book 1.6 brought many new changes to Python String methods and Unicode support have been added, as well as an improved regular expression engine A few more significant changes have made their way into the 2.0 release, which we will address here We also recommend the "What's New in 2.0" document found at the Python 2.0 Web site (see the Online Resources section of the Appendix for the URL) Review and Preview The following is a review of the major changes from Python 1.5.2 to 1.6, along with the expected bug fixes and module updates (new, revised, and obsoleted modules) Unicode support String methods Upgraded regular expression engine (performance and Unicode enhancements) New function invocation mechanism The 2.0 release also features the usual fixes and module updates, but in addition, offers the following new features to the language: Augmented assignment List comprehensions Extended import statement Extended print statement Once you get Python 2.0 compiled and/or installed on your system, you will see the familiar start-up line in UNIX (or similar if you are using another platform): 697 IT-SC book: Core Python Programming % python Python 2.0 (#4, Oct 2000, 23:58:52) [GCC 2.95.1 19990816 (release)] on sunos5 Type "copyright", "credits" or "license" for more information >>> Now, let's take a look at some of those new features Augmented Assignment Augmented assignment refers to the use of operators, which imply both an arithmetic operation as well as an assignment You will recognize the following symbols if you are a C, C++, or Java programmer: += = *= &= /= ^= %= |= **= For example, the shorter A += B can now be used instead of A = A + B Other than the obvious syntactical change, the most significant difference is that the first object (A in our example) is examined only once Mutable objects will be modified in place, whereas immutable objects will have the same effect as "A = A + B" (with a new object allocated) except that A is only evaluated once, as we have mentioned before >>> >>> >>> >>> >>> 25 >>> >>> >>> 698 m = 12 m %= m m **= m aList = [123, 'xyz'] aList += [45.6e7] aList IT-SC book: Core Python Programming [123, 'xyz', 456000000.0] These in-place operators have equivalent special method names when creating classes to emulate numeric types To implement an in-place special method, just add an "i" in front of the not-in-place operator; e.g., implement iadd () for the += operator as opposed to add () for just the + operator List Comprehensions Remember how we used lambda functions along with map() and filter() to apply an operation to list members or to filter out list members based on criteria via a conditional expression? Well, list comprehensions simplify that task and improve performance by bypassing the necessity of using lambda along with functional programming built-in functions List comprehensions allow you to provide an operation directly with an iteration over the original list sequence Let's take a look at the simpler list comprehension syntax first: [ expression for iterative_var in sequence ] The core of this statement is the for loop, which iterates over each item of sequence The prefixed expression is applied for each member of the sequence, and the resulting values comprise the list that the expression yields The iteration variable need not be part of the expression Recall the following code seen earlier in the text (Chapter 11, Functions) which utilizes a lambda function to square the members of a sequence: >>> map(( lambda x: x ** 2), range(6)) [0, 1, 4, 9, 16, 25] We can replace this code with the following list comprehension statement: >>> [ x ** for x in range(6) ] [0, 1, 4, 9, 16, 25] 699 IT-SC book: Core Python Programming In the new statement, only one function call (range()) is made (as opposed to three— range(), map(), and the lambda function) You may also use parentheses around the expression if "[ (x ** 2) for x in range(6) ]" is easier for you to read This syntax for list comprehensions can be a substitute for and is more efficient than using the map() built-in function along with lambda List comprehensions also support an extended syntax with the if statement: [ expression for iterative_var in sequence if cond_expression] This syntax will filter or "capture" sequence members only if they meet the condition provided for in the cond_expression conditional expression during iteration Recall the following odd() function below, which determines whether a numeric argument is odd or even (returning for odd numbers and for even numbers): def odd(n): return n % We were able to take the core operation from this function, and use it with filter() and lambda to obtain the set of odd numbers from a sequence: >>> seq = [11, 10, 9, 9, 10, 10, 9, 8, 23, 9, 7, 18, 12, 11, 12] >>> filter(lambda x: x % 2, seq) [11, 9, 9, 9, 23, 9, 7, 11] As in the previous example, we can bypass the use of filter() and lambda to obtain the desired set of numbers with list comprehensions: >>> [ x for x in seq if x % ] [11, 9, 9, 9, 23, 9, 7, 11] 700 IT-SC book: Core Python Programming List comprehensions also support multiple nested for loops and more than one if clause Please see the documentation including the "What's New" online document for more information Extended import Statement Another fairly common request from Python programmers is the ability to import modules and module attributes into your program using names other than their original given names One common workaround is to assign the module name to a variable: >>> import longmodulename >>> short = longmodulename >>> del longmodulename In the example above, rather than using longmodulename.attribute, you would use the short.attribute to access the same object (A similar analogy can be made with importing module attributes using from-import… see below.) However, to this over and over again, and in multiple modules can be annoying and seem wasteful The new extended import statement will now support the following: >>> import longmodulename as short Accordingly, you may also use this syntax with from-import statements >>> from sys import stderr as err >>> err.write("now using sys.stderr") now using sys.stderr We will note that as is not a new keyword and is only recognized when using import As a result, you can still use it as a valid identifier in your code: >>> as = 14 >>> as += >>> as 17 Extended print Statement 701 IT-SC book: Core Python Programming One of the last and more argumentative additions to Python 2.0 is the extended print statement The change, which employs a pair of "greater than" symbols (>>), allows you to direct the output of print to a file other than standard output In the example below, we utilize our import of sys.stderr to err above: >>> print >> err, "using sys.stderr again" using sys.stderr again Conclusion While both augmented assignments and list comprehensions appear to be adding a twist to Python's easygoing syntax, the basic philosophy of keeping the language clean and simple has not changed The key value-adds that these new features bring to the table is actually under the covers Augmented assignment only evaluates the first object once—a timesaver and performance enhancer over the long haul Also, because function objects created by lambda are practically the same as those generated by def, the overhead of a real function call is incurred when they are executed By using list comprehensions, there is no additional function object created on the fly, nor is there the additional function call overhead present In this sense, list comprehensions give Python more "inlined" execution The extended import and print statements have less to with performance as they with programmer convenience Other additions include an optional garbage collector that can detect cycles and improved XML support (xml.dom, xml.sax, xml.parsers, and pyexpat modules) Other features to look for in 2.0 are range displays, parallel for loops, and ports to 64-bit systems Exercise Q: Create a composite list comprehension statement that creates (randomly) a list of between and 10 random numbers, ranging from to 100, and pull out only the odd ones A: Answer: 702 IT-SC book: Core Python Programming Our solution uses list comprehensions as well as the new extended import syntax >>> from random import randint as >>> [ y for y in [ ri(1, 100) for [47, 9, 85] >>> [ y for y in [ ri(1, 100) for [45, 3] >>> [ y for y in [ ri(1, 100) for [] >>> [ y for y in [ ri(1, 100) for [47, 25, 95, 83, 15, 77] ri x in range(ri(1, 10)) ] if y % 2] x in range(ri(1, 10)) ] if y % 2] x in range(ri(1, 10)) ] if y % 2] x in range(ri(1, 10)) ] if y % 2] 703 ... book: Core Python Programming Core Python Programming Wesley J Chun Publisher: Prentice Hall PTR First Edition December 14, 2000 ISBN: 0-13-026036-3, 816 pages IT-SC book: Core Python Programming. .. CORE PYTHON Welcome to Python! What Is Python? History of Python Features of Python Obtaining Python Obtaining Python Installing Python Running Python Python Documentation Comparing Python JPython... book: Core Python Programming Part I: CORE PYTHON 23 IT-SC book: Core Python Programming Chapter Welcome to Python! Chapter Topics What is Python, Its History and Features Where to Obtain Python

Ngày đăng: 20/03/2019, 15:02

Từ khóa liên quan

Mục lục

  • Core Python Programming

  • Picture

  • Review

  • Table of Contents

  • Welcome to Python!

  • Acknowledgements

  • Part I: CORE PYTHON

    • Chapter 1. Welcome to Python!

      • What Is Python?

      • Chapter 2. Getting Started

      • Chapter 3. Syntax and Style

      • Chapter 4. Python Objects

      • Chapter 5. Numbers

      • Chapter 6. Sequences: Strings, Lists, and Tuples

      • Chapter 7. Dictionaries

      • Chapter 8. Conditionals and Loops

      • Chapter 9. Files and Input/Output

      • Chapter 10. Errors And Exceptions

      • Chapter 11. Functions

      • Chapter 12. Modules

      • Chapter 13. Classes and OOP

      • Chapter 14. Execution Environment

Tài liệu cùng người dùng

Tài liệu liên quan