(Close Ad) Other books by Al Sweigart, free online or available for purchase:
BUY THIS BOOK: No Starch Press (free ebook) | Amazon | Barnes and Noble | Powell's | Bookshop.org | Goodreads | Kobo | Thriftbooks
SUPPORT THE AUTHOR: Patreon or Donate Directly
Next: Bagels The Big Book of Small Python Projects

Introduction

Programming was so easy when it was just following print('Hello, world!') tutorials. Perhaps you’ve followed a well-structured book or online course for beginners, worked through the exercises, and nodded along with its technical jargon that you (mostly) understood. However, when it came time to leave the nest to write your own programs, maybe you found it hard to fly on your own. You found yourself staring at a blank editor window and unsure of how to get started writing Python programs of your own.

The problem is that following a tutorial is great for learning concepts, but that isn’t necessarily the same thing as learning to create original programs from scratch. The common advice given at this stage is to examine the source code of open source software or to work on your own projects, but open source projects aren’t always well documented or especially accessible to newcomers. And while it’s motivating to work on your own project, you’re left completely without guidance or structure.

This book provides you with practice examples of how programming concepts are applied, with a collection of over 80 games, simulations, and digital art programs. These aren’t code snippets; they’re full, runnable Python programs. You can copy their code to become familiar with how they work, experiment with your own changes, and then attempt to re-create them on your own as practice. After a while, you’ll start to get ideas for your own programs and, more importantly, know how to go about creating them.

Programming has proven to be a powerful skill, creating billion-dollar tech companies and amazing technological advances. It’s easy to want to aim high with your own software creations, but biting off more than you can chew can leave you with half-finished programs and frustration. However, you don’t need to be a computer genius to code fun and creative programs.

The Python programs in this book follow several design principles to aid new programmers in understanding their source code:

  1. Small Most of these programs are limited to 256 lines of code and are often significantly shorter. This size limit makes them easier to comprehend. The choice of 256 is arbitrary, but 256 is also 28, and powers of 2 are lucky programmer numbers.
  2. Text based Text is simpler than graphics. Since the source code and program output are both text, it’s easy to trace the cause and effect between print('Thanks for playing!') in the code and Thanks for playing! appearing on the screen.
  3. No installation needed Each program is self-contained in a single Python source file with the .py file extension, like tictactoe.py. You don’t need to run an installer program, and you can easily post these programs online to share with others.
  4. Numerous There are 81 programs in this book. Between board games, card games, digital artwork, simulations, mathematical puzzles, mazes, and humor programs, you’re bound to find many things you’ll love.
  5. Simple The programs have been written to be easy to understand by beginners. Whenever I had to choose between writing code using sophisticated, high-performance algorithms or writing plain, straightforward code, I’ve chosen the latter every time.

The text-based programs may seem old school, but this style of programming cuts out the distractions and potholes that downloading graphics, installing additional libraries, and managing project folders bring. Instead, you can just focus on the code.

This book is written for two groups of people. The people in the first group are those who have already learned the basics of Python and programming but are still unsure of how to write programs on their own. They may feel that programming hasn’t “clicked” for them. They may be able to solve the practice exercises from their tutorials but still struggle to picture what a complete program “looks like.” By first copying and then later re-creating the games in this book, they’ll be exposed to how the programming concepts they’ve learned are assembled into a variety of real programs.

The people in the second group are those who are new to programming but are excited and a bit adventurous. They want to dive right in and get started making games, simulations, and number-crunching programs right away. They’re fine with copying the code and learning along the way. Or perhaps they already know how to program in another language but are new to Python. While it’s no substitute for a complete introductory Python course, this book contains a brief introduction to Python basics and how to use the debugger to examine the inner workings of a program as it runs.

Experienced programmers might have fun with the programs in this book as well, but keep in mind that this book was written for beginners.

While the bulk of this book is dedicated to the featured programs, there are also extra resources with general programming and Python information. Here’s what’s contained in this book:

  1. Projects The 81 projects are too numerous to list here, but each one is a self-contained mini-chapter that includes the project’s name, a description, a sample run of the program’s output, and the source code of the program. There are also suggestions for experimental edits you can make to the code to customize the program.
  2. Appendix A: Tag Index Lists all of the projects categorized by their project tags.
  3. Appendix B: Character Map A list of character codes for symbols such as hearts, lines, arrows, and blocks that your programs can print.

This book doesn’t teach Python or programming concepts like a traditional tutorial. It has a learn-by-doing approach, where you’re encouraged to manually copy the programs, play with them, and inspect their inner workings by running them under a debugger.

The point of this book isn’t to give a detailed explanation of programming language syntax, but to show solid examples of programs that perform an actual activity, whether it’s a card game, an animation, or exploration of a mathematical puzzle. As such, I recommend the following steps:

  1. Download the program and run it to see what the program does for yourself.
  2. Starting from a blank file, copy the code of the game from this book by manually typing it yourself. (Don’t use copy and paste!)
  3. Run the program again, and go back and fix any typos or bugs you may have introduced.
  4. Run the program under a debugger, so you can carefully execute each line of code one at a time to understand what it does.
  5. Find the comments marked with (!) to find code that you can modify and then see how this affects the program the next time you run it.
  6. Finally, try to re-create the program yourself from scratch. It doesn’t have to be an exact copy; you can put your own spin on the program.

When copying the code from this book, you don’t necessarily have to type the comments (the text at the end of a line following the # symbol), as these are notes for human programmers and are ignored by Python. However, try to write your Python code on the same line numbers as the programs in this book to make comparison between the two easier. If you have trouble finding typos in your program, you can compare your code to the code in this book with the online diff tool at https://inventwithpython.com/bigbookpython/diff/.

Each program has been given a set of tags to describe it, such as board game, simulation, artistic, and two-player. An explanation of each of these tags and a cross-index of tags and projects can be found in Appendix A. The projects are listed in alphabetical order, however.

Python is the name of both the programming language and the interpreter software that runs your Python code. The Python software is completely free to download and use. You can check if you already have Python installed from a command line window. On Windows, open the Command Prompt program and then enter py --version. If you see output like the following, then Python is installed:

C:\Users\Al>py --version
Python 3.9.1

On macOS and Linux, open the Terminal program and then enter python3 --version. If you see output like the following, then Python is installed:

$ python3 --version
Python 3.9.1

This book uses Python version 3. Several backward-incompatible changes were made between Python 2 and 3, and the programs in this book require at least Python version 3.1.1 (released in 2009) to run. If you see an error message telling you that Python cannot be found or the version reports Python 2, you can download the latest Python installer for your operating system from https://python.org/. If you’re having trouble installing Python, you can find more instructions at https://installpython3.com/.

While the Python software runs your program, you’ll type the Python code into a text editor or integrated development environment (IDE) application. I recommend using Mu Editor for your IDE if you are a beginner because it’s simple and doesn’t distract you with an overwhelming number of advanced options.

Open https://codewith.mu/ in your browser. On Windows and macOS, download the installer for your operating system and then run it by double-clicking the installer file. If you are on macOS, running the installer opens a window where you must drag the Mu icon to the Applications folder icon to continue the installation. If you are on Ubuntu, you’ll need to install Mu as a Python package. In that case, open a new Terminal window and run pip3 install mu-editor to install and mu-editor to run it. Click the Instructions button in the Python Package section of the download page for full instruction details.

Once it’s installed, let’s start Mu:

The first time Mu runs, a Select Mode window appears with the following options: Adafruit CircuitPython, BBC micro:bit, Pygame Zero, and Python 3. Select Python 3. You can always change the mode later by clicking the Mode button at the top of the editor window.

You’ll be able to enter the code into Mu’s main window and then save, open, and run your files from the buttons at the top.

You can use any number of editors for writing Python code. The Integrated Development and Learning Environment (IDLE) software installs along with Python, and it can serve as a second editor if for some reason you can’t get Mu installed or working. Let’s start IDLE now:

There are several other free editors you can use to enter and run Python code, such as:

Most of the programs in this book only require the Python Standard Library, which is installed automatically with Python. However, some programs require third-party modules such as pyperclip, bext, playsound, and pyttsx3. All of these can be installed at once by installing the bigbookpython module.

For the Mu Editor, you must install the 1.1.0-alpha version (or later). As of 2020, you can find this version at the top of the download page at https://codewith.mu/en/download under the “Try the Alpha of the Next Version of Mu” section. After installation, click the gear icon in the lower-left corner of the window to bring up the Mu Administration window. Select the Third Party Packages tab, enter bigbookpython into the text field, and click Ok. This installs all of the third-party modules used by the programs in this book.

For the Visual Studio Code or IDLE editor, open the editor and run the following Python code from the interactive shell:

>>> import os, sys
>>> os.system(sys.executable + ' -m pip install --user bigbookpython')
0

The number 0 appears after the second instruction if everything worked correctly. Otherwise, if you see an error message or another number, try running the following instructions, which don’t have the --user option:

>>> import os, sys
>>> os.system(sys.executable + ' -m pip install bigbookpython')
0

No matter which editor you use, you can try running import pyperclip or import bext to check if the installation worked. If these import instruction don’t produce an error message, these modules installed correctly and you’ll be able to run the projects in this book that use these modules.

Programming is a skill that you improve by programming. Don’t just read the code in this book or copy and paste it to your computer. Take the time to enter the code into the editor for yourself. Open a new file in your code editor and enter the code. Pay attention to the line numbers in this book and your editor to make sure you aren’t accidentally skipping any lines. If you encounter errors, use the online diff tool at https://inventwithpython.com/bigbookpython/diff/ to show you any differences between your code and the code in this book. To get a better understanding of these programs, try running them under the debugger.

After entering the source code and running it a few times, try making experimental changes to the code. The comments marked with (!) have suggestions for small changes you can make, and each project lists suggestions for larger modifications.

Next, try re-creating the program from scratch without looking at the source code in this book. It doesn’t have to be exactly the same as this program; you can invent your own version!

Once you’ve worked through the programs in this book, you might want to start creating your own. Most modern video games and software applications are complicated, requiring teams of programmers, artists, and designers to create. However, many board, card, and paper-and-pencil games are often simple enough to re-create as a program. Many of these fall under the category of “abstract strategy games.” You can find a list of them at https://en.wikipedia.org/wiki/List_of_abstract_strategy_games.

The programming projects in this book that use the bext module have colorful text for their output. However, these colors won’t appear when you run them from Mu, IDLE, or other editors. These programs should be run from a terminal, also called command line, window. On Windows, run the Command Prompt program from the Start menu. On macOS, run Terminal from Spotlight. On Ubuntu Linux, run Terminal from Ubuntu Dash or press ctrl-alt-T.

When the terminal window appears, you should change the current directory to the folder with your .py files with the cd (change directory) command. (Directory is another term for folder.) For example, if I were on Windows and saved my Python programs to the C:\Users\Al folder, I would enter the following line.

C:\>cd C:\Users\Al

C:\Users\Al>

Then, to run Python programs, enter python yourProgram.py on Windows or python3 yourProgram.py on macOS and Linux, replacing yourProgram.py with the name of your Python program:

C:\Users\Al>python guess.py
Guess the Number, by Al Sweigart [email protected]

I am thinking of a number between 1 and 100.
You have 10 guesses left. Take a guess.
--snip--

You can terminate programs run from the terminal by pressing ctrl-C rather than close the terminal window itself.

Ideally you’ll have a laptop or desktop computer with a full keyboard to write code, as tapping on a phone or even tablet keyboard can be tedious. While there are no established Python interpreters for Android or iOS, there are websites that host online Python interactive shells you can use from a web browser. These will also work for laptops and desktops, in case you’re an instructor who doesn’t have account permission to install new software on your classroom’s computers.

The websites https://repl.it/languages/Python3/ and