"The only way to learn a new programming language is by writing programs in it." - Dennis Ritchie
I believe you can learn serious things through silly games. I also think you will learn best by doing. This is a book of programming exercises. Each chapter includes a description of a program you should write with examples of how the program should work. Most importantly, each program includes tests so that you know if your program is working well enough.
I won't necessarily show you beforehand how to write each program. I'll describe what the program should do and provide some discussion about how to write it. I'll also create an appendix with short examples of how to do things like how to use argparse, how to read/write from/to a file, how to process all the files in a directory, how to extract k-mers from a string, etc. I'll provide some building blocks, but I want you to figure out how to put the pieces together.
First use the GitHub interface to "fork" this repository into your own account. Then do git clone of your repository to get a local copy. Inside that checkout, do:
git remote add upstream https://github.com/kyclark/playful_python.git
This will allow you to git pull upstream master in order to get updates. When you create new files, git add/commit/push them to your repository. (Please do not create pull requests on my repository -- unless, of course, you have suggestions for improving my repo!).
I provide some useful programs in the bin directory including one called new.py that will help you stub out new Python programs using the argparse module to parse the command line arguments and options for your programs. I recommend you start every new program with this program. For example, in the article directory the README.md wants you to create a program called article.py. You should do this:
$ cd article
$ new.py article
This will create a new file called article.py (that has been made executable with chmod +x, if your operating system supports that) that has example code for you to start writing your program.
Your $PATH is a list of directories where your operating system will look for programs. To see what your $PATH looks like, do:
$ echo $PATH
Probably each directory is separated by a colon (:). The order of the directories matters! For instance, it's common to have more than one version of Python installed. When you type python on the command line, the directories in your $PATH are searched in order, and the first python found is the one that is used (and it's probably Python version 2!)
You could execute new.py by giving the full path to the program, e.g., $HOME/work/playful_python/bin/new.py, but that's really tedious. It's best to put new.py into one of the directories that is already in your $PATH like maybe /usr/local/bin. The problem is that you probably need administrator privileges to write to most of the directories that are in your $PATH. If you are working on your laptop, this is probably not a problem, but if you are on a shared system, you probably won't be able to copy the program into your $PATH directories.
An alternative is to alter your $PATH to include the directory where new.py is located. E.g., if new.py is in $HOME/work/playful_python/bin/, then add this directory to your $PATH -- probably by editing .bashrc or .bash_profile located in your $HOME directory (if you use bash). See the documentation for your shell of choice to understand how to edit and persist your $PATH.
For what it's worth, I always create a $HOME/.local directory for local installations of software I need, so I add $HOME/.local/bin to my $PATH. Then I copy programs like new.py there and they are available to me anywhere on the system.
Once you have stubbed out your new program, open it in your favorite editor and change the example arguments in get_args to suit the needs of your app, then add your code to main to accomplish the task described in the README. To run the test suite using make, you can type make test in the same directory as the test.py and article.py program. If your system does not have make or you just don't want to use it, type pytest -v test.py.
Your goal is to pass all the tests. The tests are written in an order designed to guide you in how break the problem down, e.g., often a test will ask you to alter one bit of text from the command line, and this it will ask you to read and alter the text from a file. I would suggest you solve the tests in order.
Ken Youens-Clark is a Sr. Scientific Programmer in the lab of Dr. Bonnie Hurwitz at the University of Arizona. He started college as a music major at the University of North Texas but changed to English lit for his BA in 1995. He started programming at his first job out of college, working through several languages and companies before landing in bioinformatics in 2001. In 2019 he earned his MS in Biosystems Engineering, and enjoys helping people learn programming. When he's not working, he likes playing music, riding bikes, cooking, and being with his wife and children.
© Ken Youens-Clark 2019