PYTHON LICENSE, DISCLAIMER OF LIABILITY, AND LIMITED WARRANTY By purchasing or using this book (the “Work”), you agree that this license grants permission to use the contents contained herein, but does not give you the right of ownership to any of the textual content in the book or ownership to any of the information or products contained in it. This license does not permit uploading of the Work onto the Internet or on a network (of any kind) without the written consent of the Publisher. Duplication or dissemination of any text, code, simulations, images, etc. contained herein is limited to and subject to licensing terms for the respective products, and permission must be obtained from the Publisher or the owner of the content, etc., in order to reproduce or network any portion of the textual material (in any media) that is contained in the Work. M L I (“MLI” or “the Publisher”) and anyone involved ERCURY EARNING AND NFORMATION in the creation, writing, or production of the companion disc, accompanying algorithms, code, or computer programs (“the software”), and any accompanying Web site or software of the Work, cannot and do not warrant the performance or results that might be obtained by using the contents of the Work. The author, developers, and the Publisher have used their best efforts to insure the accuracy and functionality of the textual material and/or programs contained in this package; we, however, make no warranty of any kind, express or implied, regarding the performance of these contents or programs. The Work is sold “as is” without warranty (except for defective materials used in manufacturing the book or due to faulty workmanship). The author, developers, and the publisher of any accompanying content, and anyone involved in the composition, production, and manufacturing of this work will not be liable for damages of any kind arising out of the use of (or the inability to use) the algorithms, source code, computer programs, or textual material contained in this publication. This includes, but is not limited to, loss of revenue or profit, or other incidental, physical, or consequential damages arising out of the use of this Work. The sole remedy in the event of a claim of any kind is expressly limited to replacement of the book, and only at the discretion of the Publisher. The use of “implied warranty” and certain “exclusions” vary from state to state, and might not apply to the purchaser of this product. Companion disc files are available for download from the publisher by writing to [email protected]. Copyright ©2017 by M L I LLC. All rights reserved. ERCURY EARNING AND NFORMATION This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means, media, electronic display or mechanical display, including, but not limited to, photocopy, recording, Internet postings, or scanning, without prior permission in writing from the publisher. Publisher: David Pallai M L I ERCURY EARNING AND NFORMATION 22841 Quicksilver Drive Dulles, VA 20166 [email protected] www.merclearning.com (800) 232-0223 James R. Parker. PYTHON: An Introduction to Programming. ISBN: 978-1-9445346-5-3 The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks, etc. is not an attempt to infringe on the property of others. Library of Congress Control Number: 2016915244 161718321 Printed in the United States of America This book is printed on acid-free paper. Our titles are available for adoption, license, or bulk purchase by institutions, corporations, etc. For additional information, please contact the Customer Service Dept. at 800-232- 0223 (toll free). Digital versions of our titles are available at: www.authorcloudware.com and other e-vendors. All companion files are available by writing to the publisher at [email protected]. The sole obligation of M L I to the purchaser is to ERCURY EARNING AND NFORMATION replace the book and/or disc, based on defective materials or faulty workmanship, but not based on the operation or functionality of the product. Contents Prefacexv Chapter 0 Modern Computers 0.1 Calculations by Machine 0.2 How Computers Work and Why We Made Them 0.2.1 Numbers Example: Base Convert Binary Numbers to Decimal Convert Decimal Numbers to Binary Arithmetic in Binary 0.2.2 Memory 0.2.3 Stored Programs 0.3 Computer Systems Are Built in Layers 0.3.1 Assemblers and Compilers 0.3.2 Graphical User Interfaces (GUIs) Widgets 0.4 Computer Networks 0.4.1 Internet 0.4.2 World Wide Web 0.5 Representation 0.6 Summary Chapter 1 Computers and Programming 1.1 Solving a Problem Using a Computer 1.2 Executing Python 1.3 Guess a Number 1.4 Rock-Paper-Scissors 1.5 Solving the Guess a Number Problem 1.6 Solving the Rock-Paper-Scissors Problem 1.6.1 Variables and Values – Experimenting with the Graphical User Interface 1.6.2 Exchanging Information with the Computer 1.6.3 Example 1: Draw a Circle Using Characters 1.6.4 Strings, Integers, and Real Numbers 1.6.5 Number Bases 1.6.6 Example 2: Compute the Circumference of any Circle 1.6.7 Guess a Number Again 1.7 IF Statements 1.7.1 Else 1.8 Documentation 1.9 Rock-Paper-Scissors Again 1.10 Types Are Dynamic (Advanced) 1.11 Summary Chapter 2 Repetition 2.1 The WHILE Statement 2.1.1 The Guess-A-Number Program Yet Again 2.1.2 Modifying the Game 2.2 Rock-Paper-Scissors Yet Again 2.2.1 Random Numbers 2.3 Counting Loops 2.4 Prime or Non-Prime 2.4.1 Exiting from a Loop 2.4.2 Else 2.5 Loops That are Nested 2.6 Draw a Histogram 2.7 Loops in General 2.8 Exceptions and Errors 2.8.1 Problem: A Final Look at Guess a Number 2.9 Summary Chapter 3 Sequences: Strings, Tuples, and Lists 3.1 Strings 3.1.1 Comparing Strings Problem: Does a City Name, Entered at the Console, Come before or after the Name Denver? 3.1.2 Slicing – Extracting Parts of Strings Problem: Identify a “Print” Statement in a String 3.1.3 Editing Strings Problem: Create a JPEG File Name from a Basic String Problem: Change the Suffix of a File Name Problem: Reverse the Order of Characters in a String Problem: Is a Given File Name That of a Python Program? 3.1.4 String Methods 3.1.5 Spanning Multiple Lines 3.1.6 For Loops Again 3.2 The Type Bytes 3.3 Tuples 3.3.1 Tuples in For Loops Problem: Print the Number of Neutrons in an Atomic Nucleus 3.3.2 Membership Problem: What Even Numbers Less than or Equal to 100 are Also Perfect Squares? 3.3.3 Delete Problem: Delete the Element Lithium from the Tuple Atoms, along with Its Atomic Number. 3.3.4 Update Problem: Change the Entry for Lithium to an Entry for Oxygen 3.3.5 Tuple Assignment 3.3.6 Built-In Functions for Tuples 3.4 Lists Problem: Compute the Average (Mean) of a List of Numbers 3.4.1 Editing Lists 3.4.2 Insert 3.4.3 Append 3.4.4 Extend 3.4.5 Remove 3.4.6 Index 3.4.7 Pop 3.4.8 Sort 3.4.9 Reverse 3.4.10 Count 3.4.11 List Comprehension 3.4.12 Lists and Tuples 3.4.13 Exceptions Problem: Delete the Element Helium from a List Problem: Delete a Specified Element from a List 3.5 Set Types 3.5.1 Example: Craps 3.6 Summary Chapter 4 Functions 4.1 Function Definition: Syntax and Semantics 4.1.1 Problem: Use poundn to Draw a Histogram 4.1.2 Problem: Generalize the Histogram Code for Other Years 4.2 Function Execution 4.2.1 Returning a Value Problem: Write a Function to Calculate the Square Root of its Parameter 4.2.2 Parameters 4.2.3 Default Parameters 4.2.4 None 4.2.5 Example: The Game of Sticks 4.2.6 Scope 4.2.7 Variable Parameter Lists 4.2.8 Variables as Functions Example: Find the Maximum Value of a Function 4.2.9 Functions as Return Values 4.3 Recursion 4.3.1 Avoiding Infinite Recursion 4.4 Creating Python Modules 4.5 Program Design Using Functions – Example: The Game of Nim 4.5.1 The Development Process Exposed 4.6 Summary Chapter 5 Files: Input and Output 5.1 What Is a File? A Little “Theory” 5.1.1 How Are Files Stored on a Disk? 5.1.2 File Access is Slow 5.2 Keyboard Input 5.2.1 Problem: Read a Number from the Keyboard and Divide It by 2 5.3 Using Files in Python: Less Theory, More Practice 5.3.1 Open a File File Not Found Exceptions 5.3.2 Reading from Files End of File Common File Input Operations CSV Files Problem: Print the Names of Planets Having Fewer Than Ten Moons Problem: Play Jeopardy Using a CSV Data Set The With Statement 5.4 Writing To Files Example: Write a Table of Squares to a File 5.4.1 Appending Data to a File Example: Append Another 20 Squares to the Table of Squares File 5.5 Summary Chapter 6 Classes 6.1 Classes and Types 6.1.1 The Python Class – Syntax and Semantics