Hướng dẫn lập trình cơ bản với Python dành cho người mới bắt đầu

411 11 0
Hướng dẫn lập trình cơ bản với Python dành cho người mới bắt đầu

Đ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

www.it-ebooks.info www.it-ebooks.info Beginning Programming with Python ® www.it-ebooks.info www.it-ebooks.info Beginning Programming with Python ® by John Paul Mueller www.it-ebooks.info Beginning Programming with Python® For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2014 by John Wiley & Sons, Inc., Hoboken, New Jersey Media and software compilation copyright © 2014 by John Wiley & Sons, Inc All rights reserved Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc and may not be used without written permission Python is a registered trademark of Python Software Foundation Corporation All other trademarks are the property of their respective owners John Wiley & Sons, Inc is not associated with any product or vendor mentioned in this book LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ For general information on our other products and services, please contact our Customer Care Department within the U.S at 877-762-2974, outside the U.S at 317-572-3993, or fax 317-572-4002 For technical support, please visit www.wiley.com/techsupport Wiley publishes in a variety of print and electronic formats and by print-on-demand Some material included with standard print versions of this book may not be included in e-books or in print-on-demand If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com For more information about Wiley products, visit www.wiley.com Library of Congress Control Number: 2014935516 ISBN 978-1-118-89145-2 (pbk); ISBN 978-1-118-89147-6 (ebk); ISBN ePDF 978-1-118-89149-0 (ebk) Manufactured in the United States of America 10 www.it-ebooks.info Contents at a Glance Introduction Part I: Getting Started with Python Chapter 1: Talking to Your Computer Chapter 2: Getting Your Own Copy of Python 21 Chapter 3: Interacting with Python 39 Chapter 4: Writing Your First Application 57 Part II: Talking the Talk 81 Chapter 5: Storing and Modifying Information 83 Chapter 6: Managing Information 93 Chapter 7: Making Decisions 117 Chapter 8: Performing Repetitive Tasks 133 Chapter 9: Dealing with Errors 149 Part III: Performing Common Tasks 181 Chapter 10: Interacting with Modules 183 Chapter 11: Working with Strings 205 Chapter 12: Managing Lists 223 Chapter 13: Collecting All Sorts of Data 243 Chapter 14: Creating and Using Classes 267 Part IV: Performing Advanced Tasks 291 Chapter 15: Storing Data in Files 293 Chapter 16: Sending an E-Mail 309 Part V: The Part of Tens 327 Chapter 17: Ten Amazing Programming Resources 329 Chapter 18: Ten Ways to Make a Living with Py  thon 339 Chapter 19: Ten Interesting Tools 347 Chapter 20: Ten Libraries You Need to Know About 357 Index 365 www.it-ebooks.info www.it-ebooks.info Table of Contents Introduction About This Book Foolish Assumptions Icons Used in This Book Beyond the Book Where to Go from Here Part I: Getting Started with Python Chapter 1: Talking to Your Computer Understanding Why You Want to Talk to Your Computer Knowing that an Application is a Form of Communication Thinking about procedures you use daily Writing procedures down 10 Seeing applications as being like any other procedure 11 Understanding that computers take things literally 11 Defining What an Application Is 12 Understanding that computers use a special language 12 Helping humans speak to the computer 13 Understanding Why Python is So Cool 14 Unearthing the reasons for using Python 15 Deciding how you can personally benefit from Python 16 Discovering which organizations use Python 17 Finding useful Python applications 18 Comparing Python to other languages 18 Chapter 2: Getting Your Own Copy of Python 21 Downloading the Version You Need 21 Installing Python 24 Working with Windows 25 Working with the Mac 27 Working with Linux 29 Accessing Python on Your Machine 32 Using Windows 32 Using the Mac 35 Using Linux 36 Testing Your Installation 36 www.it-ebooks.info viii Beginning Programming with Python For Dummies Chapter 3: Interacting with Python 39 Opening the Command Line 40 Starting Python 41 Using the command line to your advantage 42 Using Python environment variables to your advantage 44 Typing a Command 45 Telling the computer what to do 46 Telling the computer you’re done 46 Seeing the result 46 Using Help 48 Getting into help mode 48 Asking for help 49 Leaving help mode 52 Obtaining help directly 52 Closing the Command Line 54 Chapter 4: Writing Your First Application 57 Understanding the Integrated DeveLopment Environment (IDLE) 58 Starting IDLE 59 Using standard commands 60 Understanding color coding 61 Getting GUI help 62 Configuring IDLE 63 Creating the Application 67 Opening a new window 67 Typing the command 68 Saving the file 69 Running the Application 71 Understanding the Use of Indentation 72 Adding Comments 74 Understanding comments 74 Using comments to leave yourself reminders 75 Using comments to keep code from executing 75 Loading and Running Existing Applications 78 Using the command line or terminal window 78 Using the Edit window 79 Using the Python Shell window or Python command line 79 Closing IDLE 80 Part II: Talking the Talk 81 Chapter 5: Storing and Modifying Information 83 Storing Information 83 Seeing variables as storage boxes 84 Using the right box to store the data 84 www.it-ebooks.info Index functions for, 213–216, 217 overview, 90–91 searching in, 217–219 selecting individual characters in, 211–213 upper() function, 171 using special characters, 208–211 as viewed by computers, 206 strip() function, 215, 216 structured data, 295 Structured Query Language (SQL), 16, 336, 359 subtraction operator ( - ), 98, 103 sudo command, 31 SUSE Linux, 29 swapcase() function, 215 switch statements, 128, 253–256 switches, command-line, 42–44 syntax concise, errors in, 154 highlighting, 334–335 sys.exit() command, 56 sys.path variable, 43, 45, 192 •T• \t (tab character), 210 Tcl (Tool Command Language), 360 ternary operator, 96 testing C++ applications, 340 installation, 36–38 production servers and, 350 using PyUnit, 354–355 third-party libraries, 332–333 throwing exceptions, 150, 174–175 See also exceptions, handling time() function, 92 TIOBE web site, 15 title() function, 215 TkInter library, 333, 359–360 TODO list management, 348 Tool Command Language (Tcl), 360 tools bug-tracking sites, 348 IPython, 354 Isort, 355 Komodo Edit, 352 Mercurial version control, 355–356 pdoc, 351–352 pydbgr, 353 PyInstaller, 350–351 PyUnit, 354–355 Roundup Issue Tracker, 348–349 VirtualEnv, 349–350 topics keyword, 50 traceback, 45 Trigger, 343 try block, 156, 164 tuples defined, 244 hierarchy of, 247–248 sequence types, 224 using, 245–248 Twisted Matrix, 364 type() method, 90 typographical characters, 209 •U• \u escape sequence, 210 -u option, 43, 45 UAC (User Access Control), 26 Ubuntu, 31 unary operators defined, 96 listing of, 97 precedence, 103 uncommenting lines, 77 Unicode characters, 210, 337 unit testing, 354–355 Universal Serial Bus (USB), 83, 293 unstructured data, 295 www.it-ebooks.info 381 uploaded by [stormrg] 382 Beginning Programming with Python For Dummies update() function, 241, 252 upper() function, 171, 215 while statements USB (Universal Serial Bus), 83, 293 UsefulModules site, 357 User Access Control (UAC), 26 user input, 114–116 user interfaces, 17, 304 •V• \v escape sequence, 210 -V option, 43 -v option, 43, 45 ValueError exception, 168, 177 variables assigning values, 85 class, 268, 278–279 defined, 84 determining type of, 90 instance, 269, 279–280 returning data from functions, 113 verbose mode, 43 version control, 355–356 version option, 43 vertical tab character, 210 VirtualEnv, 349–350 Visual Basic, 15 VMS (Virtual Memory System), 22 -W option, 43, 45 •W• W3Schools site, 335–336 warning level, 43 web programming, 16, 332 nesting, 145–147 overview, 143–144 using, 144–145 whitespace, removing, 215 widgets library, 333 winsound module, 361 with statement, 299 writerow() function, 300 writing data to files, 303–307 •X• \x escape sequence, 210 -x option, 44 -X option, 44 XML (Extensible Markup Language), 16, 296, 335–336 •Y• Yahoo!, 18 year value, 92 Yellow Dog Linux, 29 YouTube, 18 •Z• zero-based indexes, 229 ZeroDivisionError exception, 167, 169 zeroes, padding with, 215 zfill() function, 215 zip files, 296 Zope, 18 z/OS, 22 www.it-ebooks.info About the Author John Mueller is a freelance author and technical editor He has writing in his blood, having produced 95 books and more than 300 articles to date The topics range from networking to artificial intelligence and from database management to heads-down programming Some of his current books include a Windows command-line reference, books on VBA and Visio 2007, a C# design and development manual, and an IronPython programmer’s guide His technical editing skills have helped more than 63 authors refine the content of their manuscripts John has provided technical editing services to both Data Based Advisor and Coast Compute magazines He has also contributed articles to magazines such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer Be sure to read John’s blog at http://blog.johnmuellerbooks.com/ When John isn’t working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature John also likes making wine, baking cookies, and knitting When not occupied with anything else, he makes glycerin soap and candles, which come in handy for gift baskets You can reach John on the Internet at John@JohnMuellerBooks.com John is also setting up a website at http://www.johnmuellerbooks.com/ Feel free to take a look and make suggestions on how he can improve it www.it-ebooks.info www.it-ebooks.info Dedication Some people are simply there in your life — as reliable as the day is long Scott and Pegg Conderman are two such people — they have helped me through an extremely hard time simply by being themselves and knowing just what to to make the day a little better www.it-ebooks.info www.it-ebooks.info Author’s Acknowledgments Thanks to my wife, Rebecca Even though she is gone now, her spirit is in every book I write, in every word that appears on the page She believed in me when no one else would Russ Mullen deserves thanks for his technical edit of this book He greatly added to the accuracy and depth of the material you see here Russ is always providing me with great URLs for new products and ideas However, it’s the testing that Russ does that helps most He’s the sanity check for my work Russ also has different computer equipment from mine, so he’s able to point out flaws that I might not otherwise notice Matt Wagner, my agent, deserves credit for helping me get the contract in the first place and taking care of all the details that most authors don’t really consider I always appreciate his assistance It’s good to know that someone wants to help A number of people read all or part of this book to help me refine the approach, test the coding examples, and generally provide input that all readers wish they could have These unpaid volunteers helped in ways too numerous to mention here I especially appreciate the efforts of Eva Beattie, Glenn A Russell, Emanuel Jonas, and Michael Sasseen, who provided general input, read the entire book, and selflessly devoted themselves to this project Finally, I would like to thank Kyle Looper, Susan Christophersen, and the rest of the editorial and production staff www.it-ebooks.info Publisher’s Acknowledgments Senior Acquisitions Editor: Kyle Looper Project Coordinator: Patrick Redmond Project and Copy Editor: Susan Christophersen Cover Image: © iStock.com / Glam-Y Technical Editor: Russ Mullen Editorial Assistant: Claire Johnson Sr Editorial Assistant: Cherie Case www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info ... way Python works To use this third method of executing Python, you must include Python in the Windows path This is why you want to choose the Add python. exe to Path option when installing Python. .. Python- specific environment variables such as ✓ PYTHONSTARTUP ✓ PYTHONPATH ✓ PYTHONHOME ✓ PYTHONCASEOK ✓ PYTHONIOENCODING ✓ PYTHONFAULTHANDLER ✓ PYTHONHASHSEED None of these environment variables is... Python icon added to the desktop Python is ready for use Accessing Python on Your Machine After you have Python installed on your system, you need to know where to find it In some respects, Python

Ngày đăng: 24/08/2021, 22:34

Mục lục

  • Title Page

  • Copyright Page

  • Contents at a Glance

  • Table of Contents

  • Introduction

    • About This Book

    • Foolish Assumptions

    • Icons Used in This Book

    • Beyond the Book

    • Where to Go from Here

    • Part I: Getting Started with Python

      • Chapter 1: Talking to Your Computer

        • Understanding Why You Want to Talk to Your Computer

        • Knowing that an Application is a Form of Communication

          • Thinking about procedures you use daily

          • Writing procedures down

          • Seeing applications as being like any other procedure

          • Understanding that computers take things literally

          • Defining What an Application Is

            • Understanding that computers use a special language

            • Helping humans speak to the computer

            • Understanding Why Python is So Cool

              • Unearthing the reasons for using Python

              • Deciding how you can personally benefit from Python

              • Discovering which organizations use Python

              • Finding useful Python applications

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

  • Đang cập nhật ...

Tài liệu liên quan