Welcome to the Software Craftsmanship Python examples. These examples will lead you through a variety of programming exercises in conjunction with the main Software Craftsmanship lessons. Programming is a skill, and like any other skill, it takes practice to become truly proficient. The exercises in these examples range from "monkey-see-monkey-do" problems getting you used to typing exactly as a computer expects, to free-ranging ideas on programs you might be interested in writing for yourself.
You will want to complete every "MSMD" section exactly as written. Computers are not forgiving when it comes to what you type in your program, and these exercises are a safe way to begin exploring the world of computer programming. The exercises for each chapter build on the MSMD section, and are opportunities for you to apply what you’ve learned conceptually to a program. You should make an attempt to complete all the exercises. Finally, projects are larger scale ideas for programs that you would be able to write with the tools presented by that point in the main text. The more you program, the better a programmer you’ll be, but you’ll probably want to pick and choose projects that interest you.
In this book, we’ll be using Python 3. The easiest way to begin using Python 3 is by using Github Codespaces, which creates a virtual linux computer in the cloud for you to use. It is already configured to develop all of the programs in this book. By using codespaces, you will be up and running immediately, with no need to install any programs yourself. However, codespaces are not free. The are billed per hour of activity, though they do deactivate after 30 minutes of inactivity. At the time of this writing, the basic tier of Codespaces are more than sufficient for anything you will do in this book, and costs $0.085 per hour.
If you are using your personal computer, python is included with many Linux distributions, but if you are using macOS (OSX) or Windows, you will need to install it. On Windows and Mac, you will need to download and run the windows installer or the mac osx installer respectively. If it is not installed by default with your linux distribution, follow the python documentation for getting an up to date copy. After installing Python, instructions in this workbook are exactly the same for local development or using Codespaces. Choose one, and stick with it.
An editor is a program you’ll use to write the source code of your programs. It’s different from a word processor in that only the characters you type go into the program file. For instance, a word program will have information about what is a heading, where the tab stops are, and where any tables might be. In contrast, the text editor only saves the actual letters and tab you’d type. Any formatting or coloring you see is added after-the-fact, and is not part of the original source code.
There are numerous code editors available, ranging from free to hundreds of dollars, and from having almost no features to being able to write significant portions of your source code and manage large pieces of your program for you. We are going to choose a free editor with many nice features, but one that still makes you handle most of the programming yourself. Managing your project on your own will help you be a better programmer later.
If you’re using the recommended Github Codespaces, the editor is Visual Studio Code (or VSCode for short). If you are wanting to install it on your own computer, you can follow the link. It is well supported, has a tremendously rich feature set and ecosystem of extensions, and best of all, is completely free. When you visit the VSCode page, it should suggest an appropriate download link for your platform.
Why can’t I just use TextEdit on a mac or Notepad on Windows? You could, but neither of those programs have any tools to help you program. Most importantly, neight has syntax highlighting. Visual Studio Code will color different parts of your code neatly, allowing you to see at a glance important pieces of your program, and often allows you to catch typing mistakes before you even run the program. It also has numerous language integrations, from running code to suggesting likely completions as you type.
When VSCode is first opened, you will need to make a new folder.
Once you have your project folder, create a file and name it hello.py.
Type this into the file and save:
print("Hello, world!")
print(2 + 3, 12/5, 152 * 12.6)Use VSCode to execute in the terminal.
Throughout the book, we will be using an editor and a command prompt almost exclusively to write and run our programs. A simple command prompt and text editor offer numerous advantages over other programming setups like Integrated Development Environments (IDEs) and visual programming tools. All the reasons stem from the fact that the command prompt is much closer to the code than any of the other tools allow. While many common operations are streamlined, knowing how to use the command line effectively is crucial to overcoming many of the common problems you will encounter in the course of your programming.
In Visual Studio Code, you can quickly and easily run a program in the built-in terminal by right-clicking the file and selecting "Run Python File in Terminal".
And you should see the output
Hello, world! 5 2.4 1915.2
Congratulations! You’ve written your first program!

When you run a python file, VSCode will open the terminal below your code, and execute the python file using the currently selected python version. VSCode may choose to use python 2, which will cause problems with some programs we run. To fix this, click the "Settings" gear in the bottom-left of VSCode, select "Command Pallete" at the bottom of the list, type "Python: Select Interpreter" in the command list, and choose a Python 3 version.
This is a lot of new activity for many readers, and it’s ok if something went wrong. I’ll go through the most common issues here. Keep this page handy, because even if you don’t have any of these problems this time, they may accidentally come up again later.
Early readers: Please send me a message with any problems you have, so I can include them here.
-
Say more things Make your program say
-
"Time for Breakfast!"
-
"Goodnight, world."
-
"Programming is fun!"
-
-
Say even more things! Make your program say another five things.
-
Say some math. Make your program say the results of more mathematical expressions. We’ll cover arithmetic more in the next chapter.
When you’ve done some or all of these, get started in [chapter 1](../../01_basic_types_and_control_flow/README.md)