Python Phrasebook.pdf
(
926 KB
)
Pobierz
684035712 UNPDF
Python
PHRASEBOOK
ESSENTIAL CODE AND COMMANDS
Brad Dayley
DEVELOPER’S
LIBRARY
Sams Publishing, 800 East 96th Street, Indianapolis, Indiana 46240 USA
Python Phrasebook
Copyright
©
2007 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system,
or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,
without written permission from the publisher. No patent liability is assumed with respect
to the use of the information contained herein. Although every precaution has been taken
in the preparation of this book, the publisher and author assume no responsibility for
errors or omissions. Nor is any liability assumed for damages resulting from the use of the
information contained herein.
International Standard Book Number: 0-672-32910-7
Library of Congress Catalog Card Number: 2006922308
Printed in the United States of America
First Printing: November 2006
9 8 7 6
4321
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have
been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this infor-
mation. Use of a term in this book should not be regarded as affecting the validity of any
trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible,
but no warranty or fitness is implied. The information provided is on an “as is” basis. The
author and the publisher shall have neither liability nor responsibility to any person or enti-
ty with respect to any loss or damages arising from the information contained in this book.
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk
purchases or special sales. For more information, please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside the United States, please contact
International Sales
international@pearsoned.com
The Safari
®
Enabled icon on the cover of your favorite technology book means
the book is available through Safari Bookshelf. When you buy this book, you
get free access to the online edition for 45 days. Safari Bookshelf is an
electronic reference library that lets you easily search thousands of technical books,
find code samples, download chapters, and access technical information whenever and
wherever you need it.
To gain 45-day Safari Enabled access to this book:
n
Go to
http://www.samspublishing.com/safarienabled
n
Complete the brief registration form
n
Enter the coupon code IZIK-GNBH-NGLQ-42HB-LZPB
If you have difficulty registering on Safari Bookshelf or accessing the online
edition, please e-mail customer-service@safaribooksonline.com.
Acquisitions Editors
Jenny Watson
Mark Taber
Development Editor
Songlin Qiu
Managing Editor
Patrick Kanouse
Project Editor
Tonya Simpson
Copy Editor
Sarah Kearns
Indexer
Heather McNeil
Proofreader
Mike Henry
Technical Editor
Tim Boronczyk
Publishing
Coordinator
Vanessa Evans
Book Designer
Gary Adair
Page Layout
TnT Design, Inc.
Table of Contents
Introduction
1
1 Understanding Python
3
Why Use Python?
3
Invoking the Interpreter
4
Built-In Types
5
Understanding Python Syntax
10
Python Objects, Modules, Classes,
and Functions
16
Error Handling
28
Using System Tools
30
2 Manipulating Strings
37
Comparing Strings
37
Joining Strings
39
Splitting Strings
41
Searching Strings for Substrings
42
Search and Replace in Strings
43
Searching Strings for Specific
Endings/Beginnings
44
Trimming Strings
46
Aligning/Formatting Strings
47
Executing Code Inside Strings
49
Interpolating Variables Inside Strings
50
Converting Unicode to Local Strings
52
3 Managing Data Types
55
Defining a List
56
Accessing a List
57
Slicing a List
59
Adding and Removing Items in a List
61
iv
Contents
Sorting a List
63
Using Tuples
66
Constructing a Dictionary
68
Adding a Value to a Dictionary
69
Retrieving a Value from a Dictionary
71
Slicing a Dictionary
73
Swapping Keys for Values in a Dictionary
75
4 Managing Files 77
Opening and Closing Files 77
Reading an Entire File 80
Reading a Single Line from a File 82
Accessing Each Word in a File 83
Writing a File 84
Determining the Number of Lines in a File 86
Walking the Directory Tree 87
Renaming Files 88
Recursively Deleting Files and Subdirectories 90
Searching for Files Based on Extension
92
Creating a TAR File
94
Extracting a File from a TAR File
97
Adding Files to a ZIP File
98
Retrieving Files from a ZIP File
100
5 Managing Threads 103
Starting a New Thread 104
Creating and Exiting Threads 106
Synchronizing Threads 108
Implementing a Multithreaded Priority Queue 110
Initiating a Timer-Interrupted Thread
113
6 Managing Databases
117
Adding Entries to a DBM File
118
Retrieving Entries from a DBM File
120
Plik z chomika:
SmartCenter
Inne pliki z tego folderu:
Programming.Python.4th.Edition (od 3).pdf
(30141 KB)
Learning Python, 5th Edition (553).pdf
(20695 KB)
Python dla każdego. Podstawy programowania. Wydanie III.pdf
(7606 KB)
Beginning Game Development with Python and Pygame.pdf
(9352 KB)
Python Pocket Reference, 5th Edition.pdf
(5193 KB)
Inne foldery tego chomika:
instalki
py to exe
Zgłoś jeśli
naruszono regulamin