Python Coding -
One Year Later
Copyright January, 2021 by H. Zimmerman
Educators at primary, secondary, and post-secondary schools are encouraged to contact hzimmerman4741@att.net for written permission to freely use this publication for educational purposes and receive original diagrams and PDFs.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any other means, electronic, mechanical, photocopying, recording, scanning or otherwise by any party except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act without the prior written permission of the publisher and copyright owner.
Trademarks
All terms mentioned in this book that are known to be trademarks have been capitalized. All other trademarks are the property of their respective owners.
References & Citations
Welcome to Python.org. Python.org, www.python.org /.
IDE used in this book is from www.Anaconda.com
Warning and Disclaimer
The information provided in this book is on an as is basis. The author is not responsible or liable to any person or entity with respect to any loss or damages arising from the information contained in this book. The author makes no representations or warranties, express or implied, as to the condition, quality, merchantability or fitness of any product mentioned.
1. Introduction
The primary goal of this book is to learn the simple basics of debugging a Python script. Debugging is the process of finding and removing bugs or defects in a program. Debugging is also useful in getting your code to run in the first place! Chapter 3 presents Python Basics because a big part of debugging is knowing the correct syntax for a particular object and task. This book doesnt try to cover all the nuances of Python but does cover the terms and syntax youre likely to encounter in your first few weeks or months of programming. The overhead and clutter is gone, leaving behind clear and simple instructions.
Code examples are self-contained so you can copy and paste them into your IDE and run the program. There are certainly more elegant ways to do many of these tasks, but I wanted to demonstrate each concept with a working piece of code limited to a few lines. In Chapter 3 well look at the following topics and more.
Functions, Lists, Dictionaries, Tuples, Ranges, Comprehensions
Indexing, Slicing, Comparison Operators, Control Statements
When looking at concepts such as indexing, slicing, scope, or recursive functions, Ive included lots of diagrams. Well look at syntax relevant to each type of object. For example, to add to a list, you could use append, join or extend methods. While adding a new key:value pair to a dictionary is straightforward, adding to an existing dictionary is not. In this scenario, its not about the value of the object in the key:value pair, but more about the type of object in the key:value pair. Once you know the type of the dictionary object, youll use the corresponding methods to add or change values, whether its a list, string, tuple, or even another dictionary.
There are dozens of code examples, and I tried to use real-world examples. Between the detailed Table of Contents and extensive Index, I hope you can quickly find what youre looking for. The Index includes common phrases for those times when you dont know the technical term. For example, to find a string in Python you could use the in comparison operator. Whether you look for find, search or locate, the Index refers you to the in comparison operator. The examples are varied so you can work with different types of data, such as:
xml
html
matplotlib (plots/charts/graphs)
OS (files/directories)
*.txt, *.csv
user input
url
datetime
To help my daughter with her first Python class, I looked around for debugging information that I could share with her. I wanted a simple guide with everything in one place and suggestions for how to go about the process of debugging. Initially, my research focused on gathering examples of common issues, but I knew something was missing. After all, what happens if there is no example of the bug that youre experiencing?
I knew I needed to provide a debugging foundation. Not just how to use debugging tools, but when to take action and why. With that goal in mind, Chapters 1 through 6 build a debugging arsenal, so youre ready to tackle the examples in Chapter 7. Each example has References to the related topics covered in earlier chapters. So theoretically, you could jump right into the examples in Chapter 7.
I also made a point to cross-reference topics so you can easily locate whatever youre interested in from any point in the material. This approach means you can pick up the book at any time and quickly jump back in where you left off. Or, if you prefer, you can hop around from topic to topic with as much detail as you want.
Hopefully, after reading this book, you wont feel like this man who posted a plea for help on a chat board. His frustration shows through in his comment, For the love of God, how is this done? Instead, youll know exactly how its done and have fun doing it!
1.1 Overview
How you may ask, are we going to build your debugging arsenal? Lets begin with these topics.
How to use the debug environment.
The Python Error Codes and specific examples of how they happen.
Step-by-step instructions on the process of debugging code.
Finding the information you need to modify your program : help on Syntax, Functions, Classes, and more.
The goal of debugging is a working program, and debugging is just part of the process of writing code. When I realize I have a bug, Ill experiment and try a few things to find a clue where the issue is. Youll see this process in the examples in Chapter 7, where I use different approaches from my debugging toolbox to isolate an issue. You might take a different approach to the sample problem, and there is no wrong approach. The idea is to try a few things and see what works.
In this book, I demonstrate Python using the open-source Anaconda Data Science Distribution that includes Python version 3.7. Spyder, the Scientific Python Development Environment, comes with Anaconda. The Spyder IDE, or Integrated Design Environment, includes an Editor, Console, and debugging tools. You may notice slight differences in screenshots, depending on whether I am using Spyder on my Windows or Mac computer.
1.2 What This Book is About
My intent in writing this book was not to provide a guide to Python Programming. Instead, this book is specifically about debugging Python with Anacondas Spyder application. The concepts around Python debugging apply equally to other environments, but the screens and debugging tools may vary slightly.
You may wonder why Ive included Python Basics in Chapter 3. I found it difficult to explain an IndexError without first explaining data structures and their indexes. Similarly, a Dictionary KeyError doesnt mean much without an understanding of a Dictionary. Syntax errors are fairly obvious in Spyder, but it doesnt hurt to have a brief explanation of the syntax the parser expects.
Finally, Chapter 6 demonstrates how to view values, types, and the length of objects. Since the syntax varies by the type of object, I wanted to provide a reference with the exact syntax for each object type.
1.3 Whats Next?
The next chapter walks you through installing Anaconda and the basic Spyder environment. Well also look at an overall plan for debugging code.