About This eBook
ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the eBook in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Sams Teach Yourself Python in 24 Hours
Katie Cunningham
800 East 96th Street, Indianapolis, Indiana, 46240 USA
Sams Teach Yourself Python in 24 Hours
Copyright 2014 by Pearson Education, Inc.
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.
ISBN-13: 978-0-672-33687-4
ISBN-10: 0-672-33687-1
Library of Congress Control Number: 2013944085
Printed in the United States of America
First Printing October 2013
Editor-in-Chief
Mark Taub
Executive Editor
Debra Williams Cauley
Development Editor
Michael Thurston
Managing Editor
Kristy Hart
Project Editor
Andy Beaster
Copy Editor
Bart Reed
Indexer
Lisa Stumpf
Proofreader
Dan Knott
Technical Editors
Doug Hellmann
Gabriel Nilsson
Publishing Coordinator
Kim Boedigheimer
Cover Designer
Mark Shirar
Senior Compositor
Gloria Schurick
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 information. 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 entity 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
For sales outside of the U.S., please contact
International Sales
About the Author
Katie Cunningham is a Python developer at Cox Media Group. Shes a fervent advocate for Python, open source software, and teaching people how to program. Shes a frequent speaker at open source conferences, such as PyCon and DjangoCon, speaking on beginners topics such as someones first site in the cloud and making a site that is accessible to everyone.
She also helps organize PyLadies in the DC area, a program designed to increase diversity in the Python community. She has taught classes for the organization, bringing novices from installation to writing their first app in 48 hours.
Katie is an active blogger at her website (http://therealkatie.net), covering issues such as Python, accessibility, and the trials and tribulations of working from home.
Katie lives in the DC area with her husband and two children.
Dedication
This is dedicated to my family, who helps keep me sane every time
I decide to do this again. Jim, thank you for picking up the slack.
Mom, thank you for taking the kids and offering help every time
I started to look like I was going to fall over.
Kids, thank you for being okay with all the delivery food.
Acknowledgments
This book wouldnt have happened without the help from quite a few people.
First, my editor, Debra Williams Cauley, has been both patient and enthusiastic. Without her, I dont know if I would have ever hit the deadline.
A special thanks goes to my tech editors, Doug Hellmann and Gabriel Nilsson. They were machines when it came to catching my glaring errors, and their suggestions only made this book stronger. Also, a thanks goes out to Richard Jones, who took the time to review my PyGame chapter.
Thanks to Michael Thurston, who made me sound fabulous. I swear, one of these days, Ill learn to spell installation right.
Finally, a thank you goes out to the Python community, who has been on hand every time I had a question, needed a sanity check, or just needed some inspiration. You guys are my home.
Preface
Why Python?
I get this question quite a bit. Why should someone learning to program learn Python? Why not a language that was made for beginners, such as Scratch? Why not learn Java or C++, which most colleges seem to be using?
Personally, I believe that Python is an ideal language for beginners. It runs on multiple systems. The syntax (the grammar of the language) isnt fussy. Its easy to read, and many people can walk through a simple script and understand what its doing without ever having written a single line of code.
Its also ideal because its easy for a beginner to move on to more advanced projects. Python is used in a number of areas, from scientific computing to game development. A new programmer can almost always find one, if not multiple, projects to fit their tastes.
Who This Book Is For
This book is for those who have never programmed before and for those who have programmed some but now want to learn Python. This is not a book for those who are already experienced developers.
It is assumed you have a computer you have admin rights to. Youll need to install Python, as well as multiple libraries and applications later in the book. The computer does not need to be terribly powerful.
You should also have an Internet connection in order to access some of the resources.
How This Book Is Organized
This book covers the basics of programming in Python as well as some advanced concepts such as object-oriented programming.
The Introduction and cover the background of Python and installation.