Al Sweigart is a software developer, author, and Fellow of the Python Software Foundation. He was previously the education director at Oakland, Californias video game museum, The Museum of Art and Digital Entertainment. He has written several programming books, including Automate the Boring Stuff with Python and Invent Your Own Computer Games with Python. His books are freely available under a Creative Commons license at his website https://inventwithpython.com. His cat Zophie loves eating nori seaweed snacks.
Sarah Kuchinsky, MS, is a corporate trainer and consultant. She uses Python for a variety of applications, including health systems modeling, game development, and task automation. Sarah is a co-founder of the North Bay Python conference, tutorials chair for PyCon US, and lead organizer for PyLadies Silicon Valley. She holds degrees in Management Science & Engineering and Mathematics.
Introduction
Programming was so easy when it was just following print('Hello, world!')
tutorials. Perhaps youve 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 isnt 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 arent always well documented or especially accessible to newcomers. And while its motivating to work on your own project, youre 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 arent code snippets; theyre 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, youll start to get ideas for your own programs and, more importantly, know how to go about creating them.
How to Design Small Programs
Programming has proven to be a powerful skill, creating billion-dollar tech companies and amazing technological advances. Its 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 dont 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:
- 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.
- Text based Text is simpler than graphics. Since the source code and program output are both text, its easy to trace the cause and effect between
print('Thanks for playing!')
in the code and Thanks for playing!
appearing on the screen. - No installation needed Each program is self-contained in a single Python source file with the .py file extension, like tictactoe.py. You dont need to run an installer program, and you can easily post these programs online to share with others.
- Numerous There are 81 programs in this book. Between board games, card games, digital artwork, simulations, mathematical puzzles, mazes, and humor programs, youre bound to find many things youll love.
- 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, Ive 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.
Who Is This Book For?
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 hasnt 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, theyll be exposed to how the programming concepts theyve 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. Theyre 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 its 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.