An Animated Introduction to Programming with Python

by Mark Mahoney

Welcome! This is a brief introduction to programming in Python. Python is a great first language to learn. It has been around for a while, it is friendly, and succinct. This book is a collection of code 'playbacks'. Playbacks were designed to help guide a reader through code examples. You will get to see how I wrote some code along with comments that I made along the way. These comments will have text, screenshots, and hand drawn pictures. When you click on a playback title a new tab will open. There are controls to move through the playback. The most important of these are the 'Play/Pause' button and the >> button. Hitting play will animate the code as I wrote it. The playback will pause for comments that I added to the code. Hitting the >> button will skip the animation so you can focus on the code at comment points. Using the >> is the recommended way to quickly move through a playback. The programming language that we are going to use is called Python. It is relatively easy to write Python code that is easy for humans to read. This might not seem important since the computer is the only one that needs to ‘read’ the code (in order to execute it). However, almost all code is ‘living' in that it will need to be read, altered, and added to by other human beings. We need to be able to understand each other's code in order to do that. Python code was built to be read by a human. Python also has an excellent collection of libraries for scientists and data nerds. And it's free to use! I am using Python version 3.7 in this book. You may be using an older version and that is most likely fine. I'll point out places where I use newer language features. Python is an interpreted language. All this means is that there is no special tool to check if our code is following all of the rules of the language as we write it. A compiled language, like Java or C++, has a compiler that will check the syntax of our code as we write it and highlight mistakes before the program is ever run. In Python, we won’t learn of our syntax errors until we run our code. It is important to realize that the Python programming language has a set of rules that the programmer must adhere to. These rules are called the syntax of the language. The English language also has a syntax: "The do.g barks loudy" Here, we are violating the syntax because a period can’t be in the middle of a word and there is no word ‘loudy’. When we run a program with a syntax error the program dies a horrible death (and usually spits out some kind of error message). This is not the only type of error that can occur, however. A semantic error is an error in the meaning or logic of your code: "The cat barks loudly." This violates no syntax but is logically flawed. This is a semantic error. These are usually harder to diagnose because instead of the program dying with a loud error message the program usually runs to completion but gives you faulty or incorrect results. Remember that the computer is not a human, it will do exactly what you tell it to do even if that makes no sense. The computer has no ability to infer what you really mean. With human communication it is common for another human to infer what you really mean. For example, everyone knows what I mean by “shoot me a text" because of our ability to inference. Until you have mastered the language, most of your time will be spent trying to find, identify, and fix your errors. After you have mastered it, you will spend only slightly less time finding, identifying, and fixing errors. This is what programming is all about. Even the best programmers make a ridiculous number of mistakes (I know because continue to make them). So, don't get upset if your program doesn't work the first time. It never does for me.