The AbsoluteBeginners Guide toPython ProgrammingA Step-by-Step Guidewith Examples and LabExercisesKevin WilsonThe Absolute Beginners Guide to Python Programming: A Step-by-StepGuide with Examples and Lab Exercises Kevin Wilson London, UK ISBN-13 (pbk): 978-1-4842-8715-6 ISBN-13 (electronic): 978-1-4842-8716-3 https://doi.org/10.1007/978-1-4842-8716-3 Copyright 2022 by Kevin Wilson This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made.
The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Celestin Suresh John Development Editor: James Markham Coordinating Editor: Shrikant Vishwakarma Cover designed by eStudioCalamar Cover image by Shutterstock Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (https://github.com/Apress). For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper Table of Contents About the Author xi About the Technical Reviewer xiii Introduction xv Chapter 1: Introduction to Computer Programming 1 What Is Python 2 Getting Started 4 Setting Up 4 Install on Windows 4 Install on MacOS 9 Install on Linux 11 Summary13 Chapter 2: The Basics 15 Language Classification 15 Low-Level Language 15 High-Level Language 16 Python Language Syntax18 Reserved Words 18 Identifiers 20 Indentation 20 Comments 20 Input 21 iii Table of ConTenTs Output 21 Escape Characters 21 Writing a Program 22 Lab Exercises 29 Summary30 Chapter 3: Working with Data 31 Variables 31 Local Variables 31 Global Variables 32 Basic Data Types 32 Integers 32 Floating Point Numbers 33 Strings 33 Lists 33 Two-Dimensional Lists 35 Sets 37 Tuples 37 Dictionaries 38 Program Input 39 Program Output 40 Casting Data Types 41 Arithmetic Operators 42 Operator Precedence 42 Performing Arithmetic 43 Comparison Operators 43 Boolean Operators 44 iv Table of ConTenTs Bitwise Operators 45 Lab Exercises 45 Summary46 Chapter 4: Flow Control 49 Sequence 49 Selection 52 if else 52 elif55 Iteration (Loops) 61 For Loop 61 While Loop 65 Break and Continue 68 Lab Exercises 69 Summary69 Chapter 5: Handling Files 71 File Types 71 Text File 71 Binary 72 Text File Operations73 Open Files 73 Write to a File 75 Read from a File 78 Binary File Operations 79 Open Files 79 Write to a File 80 Read a File 81 v Table of ConTenTs Random File Access 83 Lab Exercises 84 Summary85 Chapter 6: Using Functions 87 Declaring Functions 87 Scope 90 Recursion 90 Lab Exercises 92 Summary93 Chapter 7: Using Modules 95 Importing Modules 96 Creating Your Own Modules 100 Lab Exercises 101 Summary102 Chapter 8: Exception Handling 103 Types of Exception 103 Catching Exceptions 105 Raising Your Own Exceptions 107 Summary108 Chapter 9: Object-Oriented Programming 109 Principles of OOP 109 Encapsulation 109 Inheritance 110 Polymorphism 110 Abstraction 110 vi Table of ConTenTs Classes and Objects 110 Class Inheritance 113 Polymorphic Classes 116 Method Overriding 117 Lab Exercises 119 Summary120 Chapter 10: Building an Interface 121 Creating a Window 121 Adding Widgets 124 Menus 124 The Canvas 126 Images 129 Buttons 130 Message Boxes 131 Text Field 132 Listbox 133 Checkbox 135 Labels 137 Label Frame 138 Interface Design 139 Summary144 Chapter 11: Developing a Game145 Installing Pygame 145 Opening a Window 147 Adding an Image 148 The Game Loop 149 The Event Loop 151 vii Table of ConTenTs Shapes 155 Basic Animation 156 Summary164 Chapter 12: Python Web Development 167 Web Servers 167 Install the Web Server 169 Set Up Python Support 169 Executing a Script 171 Python Web Frameworks 175 Summary181 Appendix A: Quick Reference 183 Data Types 183 Numeric Operators 183 Comparison Operators 184 Boolean Operators 184 String Operators 184 List Operators 184 Dictionary Operators 185 String Methods 185 List Methods 185 Dictionary Methods 185 Functions 186 Files 186 Conditional 186 Multi-conditional 186 While Loop 187 viii Table of ConTenTs For Loop 187 Loop Control 187 Modules 187 Built-In Functions 187 Declare a Class 188 Child Class 188 Create Object 188 Call Object Method 188 Access Object Attributes 188 Exceptions 188 Index 189 ix About the Author With over 20 years experience in the computer industry, Kevin Wilson has made a career out of technology and showing others how to use it.
After earning a masters degree in computer science, software engineering, and multimedia systems, Kevin has held various positions in the IT industry including graphic and web design, digital film and photography, programming and software engineering, developing and managing corporate networks, building computer systems, and IT support. He currently teaches computer science at college and works as an IT trainer in England while researching for his Ph.D. xi About the Technical ReviewerJoos Korstanje is a data scientist, with over five years of industry experience in developing machine-learning tools. He has a double M.Sc. in applied data science and environmental science and has extensive experience working with geodata use cases. He currently works at Disneyland Paris, where he develops machine learning for a variety of tools.
His project experience includes forecasting, recommender engines, optimization, machine learning on GPS tracking data, and more. Joos is also an active blogger on Medium and has worked on multiple book publications. xiii Introduction The aim of this book is to provide a first course in the use of Python to develop programs. It provides a foundation for those who wish to write computer programs based on sound programming principles, and because the book is intended to be a primer, it allows the beginner to become comfortable with basic programming tasks. As it is a first course, no previous experience of computer programming is assumed. Throughout the book, well explore the Python programming language with worked examples and lab exercises for you to complete yourself.
Next page