Begin to Code with Python Rob Miles BEGIN TO CODE WITH PYTHON Published with the authorization of Microsoft Corporation by: Pearson Education, Inc Copyright © 2018 by Pearson Education, Inc All rights reserved Printed in the United States of America This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise For information regarding permissions, request forms, and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/ 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-1-5093-0452-3 ISBN-10: 1-5093-0452-5 Library of Congress Control Number: 2017958202 17 TRADEMARKS Microsoft and the trademarks listed at http://www.microsoft.com on the “Trademarks” webpage are trademarks of the Microsoft group of companies All other marks are property of their respective owners "Python" and the Python logos are trademarks or registered trademarks of the Python Software Foundation, used by Pearson Education with permission from the Foundation 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, the publisher, and Microsoft Corporation 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 or from the use of the programs accompanying it SPECIAL SALES For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419 For government sales inquiries, please contact governmentsales@pearsoned.com For questions about sales outside the U.S., please contact intlcs@pearson.com Editor-in-Chief Greg Wiegand Senior Acquisitions Editor Laura Norman Development Editor Rick Kughen Managing Editor Sandra Schroeder Senior Project Editor Tracey Croom Copy Editor Dan Foster Indexer Valerie Haynes Perry Proofreader Becky Winter Technical Editor John Ray Editorial Assistant Cindy J Teeters Cover Designer Twist Creative, Seattle Compositor Danielle Foster To Mary About the author Rob Miles has spent more than 30 years teaching programming at the University of Hull in the United Kingdom He’s a Microsoft MVP, with a passion for programming, and creating new things If he had any spare time, he’d spend it writing even more code He loves making programs and then running them to see what happens He reckons that programming is the most creative thing you can learn how to He believes that programmers really can claim to be building the future He claims to know a lot of really good jokes, but nobody has ever heard him tell one If you want an insight into the Wacky World™ of Rob Miles, you can read his blog at www.robmiles.com and follow him on Twitter via @RobMiles rob@robmiles.com Contents at a glance Part 1: Programming fundamentals Chapter Starting with Python Chapter Python and Programming Chapter Python program structure Chapter Working with variables Chapter Making decisions in programs Chapter Repeating actions with loops Chapter Using functions to simplify programs Chapter Storing collections of data Part 2: Advanced programming Chapter Use classes to store data Chapter 10 Use classes to create active objects Chapter 11 Object-based solution design Chapter 12 Python applications Part 3: Useful Python (Digital-only) The chapter PDF files for this Part are available at https://aka.ms/BeginCodePython/downloads Chapter 13 Python and Graphical User Interfaces Chapter 14 Python programs as network clients Chapter 15 Python programs as network servers Chapter 16 Create games with Pygame Contents Introduction Part 1: Programming fundamentals Starting with Python What is Python? Python origins Python versions Build a place to work with Python Get the tools Python for Windows PC Start Python What you have learned Python and Programming What makes a programmer Programming and party planning Give us feedback Tell us what you think of this book and help Microsoft improve our products for you Thank you! http://aka.ms/tellpress Programming and problems Programmers and people Computers as data processors Machines and computers and us Programs as data processors Python as a data processor Data and information Work with Python functions The ord function The chr function Investigate data storage using bin What you have learned Python program structure Write your first Python program Run Python programs using IDLE Get program output using the print function Use Python libraries The random library The time library Python comments Code samples and comments Run Python from the desktop Delay the end of the program Adding some snaps Adding the Pygame library Snaps functions What you have learned Working with variables Variables in Python Python names Working with text Marking the start and end of strings Escape characters in text Read in text using the input function Working with numbers Convert strings into integer values Whole numbers and real numbers Real numbers and floating-point numbers Convert strings into floating-point values Perform calculations Convert between float and int Weather snaps What you have learned Making decisions in programs Boolean data Create Boolean variables Boolean expressions Comparing values Boolean operations The if construction Nesting if conditions Working with logic Use decisions to make an application Design the user interface Implement a user interface Testing user input Complete the program Input snaps What you have learned Repeating actions with loops The while construction Repeat a sequence of statements using while Handling invalid user entry Detect invalid number entry using exceptions Exceptions and number reading Handling multiple exceptions Break out of loops Return to the top of a loop with continue Count a repeating loop The for loop construction Make a digital clock using snaps .. .Begin to Code with Python Rob Miles BEGIN TO CODE WITH PYTHON Published with the authorization of Microsoft Corporation by: Pearson... Starting with Python What is Python? Python origins Python versions Build a place to work with Python Get the tools Python for Windows PC Start Python What you have learned Python and Programming What... teach you how to begin to program—and that you can then use to create programs of your own You can download this book’s sample code from the following page: https://aka.ms/BeginCodePython/downloads