ebook img

Python Power!: The Comprehensive Guide PDF

529 Pages·2008·2.972 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python Power!: The Comprehensive Guide

P y t h o n P o w e r ! THE COMPREHENSIVE GUIDE Q Q Q Matt Telles © 2008 Thomson Course Technology, a division of Thomson Learning Inc. All rights reserved. Publisher and General No part of this book may be reproduced or transmitted in any form or by any means, Manager, Thomson Course electronic or mechanical, including photocopying, recording, or by any information storage Technology PTR: or retrieval system without written permission from Thomson Course Technology PTR, Stacy L. Hiquet except for the inclusion of brief quotations in a review. Associate Director of The Thomson Course Technology PTR logo and related trade dress are trademarks of Marketing: Thomson Course Technology, a division of Thomson Learning Inc., and may not be used Sarah O’Donnell without written permission. Manager of Editorial Python is a trademark of the Python Software Foundation. Services: Microsoft Windows is a registered trademark of Microsoft Corporation. Heather Talbot All other trademarks are the property of their respective owners. Marketing Manager: Mark Hughes Important: Thomson Course Technology PTR cannot provide software support. Please contact the appropriate software manufacturer’s technical support line or Web site for Acquisitions Editor: assistance. Mitzi Koontz Thomson Course Technology PTR and the author have attempted throughout this book to Marketing Assistant: distinguish proprietary trademarks from descriptive terms by following the capitalization Adena Flitt style used by the manufacturer. Project and Copy Editor: Information contained in this book has been obtained by Thomson Course Technology PTR Marta Justak from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Thomson Course Technology PTR, or others, the Publisher Technical Reviewer: does not guarantee the accuracy, adequacy, or completeness of any information and is not Michael Dawson responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aware of the fact that the Internet is an ever-changing entity. PTR Editorial Services Some facts may have changed since this book went to press. Coordinator: Erin Johnson Educational facilities, companies, and organizations interested in multiple copies or licensing of this book should contact the Publisher for quantity discount information. Interior Layout Tech: Training manuals, CD-ROMs, and portions of this book are also available individually or can Value Chain International, be tailored for specific needs. Ltd. ISBN-10: 1-59863-158-6 Cover Designer: ISBN-13: 978-1-59863-158-6 Mike Tanamachi eISBN-10: 1-59863-159-4 Indexer: Library of Congress Catalog Card Number: 2006923270 Sharon Hilgenberg Printed in the United States of America Proofreader: 08 09 10 11 12 TW 10 9 8 7 6 5 4 3 2 1 Steve Honeywell Thomson Course Technology PTR, a division of Thomson Learning Inc. 25 Thomson Place Boston, MA 02210 http://www.courseptr.com This book is dedicated to the ladies in my life: Teresa, Jenny, Rachel and Sarah. Acknowledgments The author would like to acknowledge the aid of the Python community and Usenet newsgroups in finding answers to all of the questions that eluded me. A special thanks to Mike Dawson, who went well above and beyond the call of duty to help a poor programmer out with this book. Thanks, Mike, you did a great job! Also, a small note of thanks to Marta, without whom this book would never have gotten off the ground. Thank you, dear. Now get back to work. iv Q Q Q About the Author Matt Telles is a 22-year veteran of the computer wars. Having moved from the mainframe world, with his beloved DEC 1091, he is now ensconced in the PC world of Windows and Linux. A long-time C++ programmer, he has since moved on to more modern languages like C#, Python, and PHP. The high point of his career is in writing his own biography for books. v Q Q Q This page intentionally left blank } Contents TABLE OF CHAPTER 1 About Python ............................................................................................1 What Is Python? ................................................................................................................1 A Brief History of Python ................................................................................................2 Interpreters Versus Compilers .......................................................................................5 When to Use (or Not Use) an Interpreted Language .........................................8 Understanding Bytecodes ......................................................................................10 Why Use Python? ...........................................................................................................11 Object-Oriented ........................................................................................................11 Cross Platform ..........................................................................................................11 Broad User Base .......................................................................................................11 Well Supported in Third-Party Tools ...................................................................12 Good Selection of Tools Available ........................................................................12 Good Selection of Pre-built Libraries ..................................................................12 Where Is Python Used? .................................................................................................13 How Is Python Licensed? ..............................................................................................13 Where Do I Get Python? ...............................................................................................14 Installing Python ............................................................................................................14 Getting Information on Python ..................................................................................16 Python Communities .....................................................................................................17 Other Software ................................................................................................................18 And Now for Something Completely Different… ....................................................18 CHAPTER 2 Python Language Overview .................................................................19 Python Syntax .................................................................................................................20 Comments .................................................................................................................20 Indentation ...............................................................................................................20 vii Q Q Q CONTENTS Python Reserved Words ................................................................................................24 Decision Making and Iteration Keywords ..........................................................25 Debugging Keywords ..............................................................................................27 Package and Module Handling Keywords .........................................................27 Exception Handling Keywords ..............................................................................29 General Language Keywords .................................................................................31 Other Keywords ........................................................................................................32 Variable Usage ................................................................................................................34 The Continuation Variable ....................................................................................36 Watching Out for Spelling Mistakes! ..................................................................37 Predicates .........................................................................................................................38 Identifier Scope ...............................................................................................................39 Operators .........................................................................................................................42 Modulo Operator .....................................................................................................44 Exponential Operator .............................................................................................46 Logical Operators ....................................................................................................46 Comparative Operators ..........................................................................................49 Bitwise Operators ....................................................................................................51 Membership Operators and String Operators ..................................................53 Identity Operators ...................................................................................................53 In Conclusion ..................................................................................................................53 CHAPTER 3 Tools ..........................................................................................................55 IDLE ...................................................................................................................................55 File Menu ...................................................................................................................57 The Path Browser Dialog ........................................................................................62 Edit Menu ..................................................................................................................64 Shell Menu .................................................................................................................70 Debug Menu ..............................................................................................................71 The Edit Window ......................................................................................................79 Format Menu ............................................................................................................80 viii Q Q Q CONTENTS Command Line Compiler ..............................................................................................90 Creating Python Files ....................................................................................................93 Documentation ...............................................................................................................95 In Conclusion ..................................................................................................................96 CHAPTER 4 Data Types ...............................................................................................97 Numeric Types ................................................................................................................98 Integers ......................................................................................................................98 Demonstrating Long Integers ...............................................................................99 Octal and Hexadecimal ........................................................................................100 Floating Point Numbers .............................................................................................101 Strings ............................................................................................................................103 String Variables .....................................................................................................103 Concatenating Strings .........................................................................................106 Repeating Strings ..................................................................................................107 Substrings ...............................................................................................................108 Slicing ......................................................................................................................110 String Functions ....................................................................................................111 String Constants ....................................................................................................112 Conversion Functions ...........................................................................................114 Search Functions ...................................................................................................118 Formatting Functions ..........................................................................................120 Escape Sequences ..................................................................................................121 Sequences ......................................................................................................................122 Lists ..........................................................................................................................123 Shared References .................................................................................................128 Tuples .......................................................................................................................128 Dictionaries ............................................................................................................132 Advanced Type .............................................................................................................136 Classes and Objects ..............................................................................................136 Complex Type .........................................................................................................137 Generator Type ......................................................................................................138 ix Q Q Q

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.