"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 with a test suite so that you know if your program is working well enough.
I won't necessarily show you beforehand what you need to write a 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 a program in the bin directory 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 go into the directory with cd article and then do:
$ 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. It's best to put new.py into your $PATH or alter your $PATH to include the directory where it's located. I usually create a $HOME/.local/bin that I add to my $PATH for programs like this.
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.