No Starch Press’ Python Playground provides a fairly large and varied collection of projects that aim to show how Python can be used in such different contexts as creating ASCII art, birds simulation, interfacing to Raspberry Pi, and more. InfoQ has spoken to the book’s author, Mahesh Venkitachalam.
Python Playground is not exactly an introductory Python book, since it targets programmers that are already comfortable with Python and want to further improve their skills and knowledge of the language. Still, the book does a reasonably good job at explaining all relevant details and make sure that readers get a clear picture of what is going on.
Each chapter is dedicated to a single project. After an initial description of the project’s context and solution, the book describes at length the code that implements it and finally proposes a few more experiments based on it that readers can run. Thanks to its structure, the book can be read in no specific order and readers can jump into any project they wish without any specific requirements.
This is a list of the book’s projects:
- iTunes playlists, focusing on XML and plist files, list and dictionaries, diagrams and scatter plots, etc.
- Spirographs, showing how to create parametric graphics, animate and save them to file.
- Conway’s game of life, introducing
matplotlib
to represent a grid of data and animate it,numpy
, and random distribution. - Musical overtones, showing how to implement a ring buffer, play WAV files, and plot a graph.
- Birds simulation, focusing again on
numpy
,matplotlib
, etc. - ASCII art, introducing grayscale image processing.
- Photomosaic, introducing RGB image processing.
- Austereograms, which are two dimensional image that create the illusion of three dimensions.
- OpenGL, showing how basics of OpenGL can be used in Python.
- Particle systems, which further builds on OpenGL skills introducing blending, depth masks, alpha channels, cameras etc.
- Volume rendering, introducing OpenGL shaders.
- Arduino intro, guiding readers through the first steps to hack an Arduino microcontroller.
- Laser audio display, a more advanced use of Arduino aimed at generating patterns using a laser and two rotating mirrors. This project includes driving motors based on the real-time FFT analysis of audio data.
- Raspberry Pi, introducing the Raspberry Pi and its interfacing via Python to a temperature and humidity sensor.
As this list makes clear, one of the goals of the book is presenting as a varied and diverse set of examples as possible and show different ways that Python can be used in real contexts. Interestingly, to make it possible for the readers to build more varied, personal hardware-interfacing projects, the book contains a final chapter that reviews the basics of practical electronics, including common components, tools, building circuits, etc.
InfoQ: Could you explain the motivation to write this book? Why was this book necessary?
I am a long time C++ programmer. A few years ago, I started using Python for automating tasks. Soon, I found myself using Python as my go-to tool for all manners of computation. In many cases it was quicker to try out an idea in Python before coding it in C++. I had rediscovered the joy of programming. When I started accumulating a set of exploratory programs in Python, I thought that this might make a good book of projects.
InfoQ: Your book targets intermediate-level developers with some experience in Python. What advice would you give them when reading this book? What should they expect? What should they not expect?
There are plenty of good books out there which teach you Python syntax and nuances of the language, and my book does not attempt to duplicate that effort. Python Playground tries to answer the question "I know some Python. Now what?" This is a book of projects which introduces you to libraries, techniques and approaches you might use to solve a particular problem or explore an idea. All projects have the same structure - you state the problem clearly, break it down into sub-tasks, identify algorithms, and find the right modules that will help you arrive at a solution quickly. My hope is that readers use these projects as stepping stones towards solving problems they are interested in.
InfoQ: There are very many Python books that target beginner-level or advanced books that deal with a specific application field, such as machine learning, network programming, scientific computation, etc. You take a different approach in that you try to cover a wide range of application fields. What is the appeal of your approach?
Python is a language that has a small set of core features – one that you can pick up very quickly. Added to that is the extensive set of built-in and third-party libraries, which means you get a helping hand for almost anything you want to do. For example, if you want to parse a web page or talk to the serial port, you have libraries readily available. Application specific books are very useful for people working on a particular domain. But my goal was to demonstrate the spread of Python - on how the language can be used to explore a variety of topics. This is especially useful to someone who has learned Python recently and is unsure about how their skills can be put to use.
InfoQ: What criteria drove your selection of projects? Were there any projects that you would have liked to include but could not make into the book for whatever reason?
The project selection is based on my personal interests. Long ago, for my MS thesis I worked on computer simulations in Materials Science – hence the projects on conway, boids, and karplus-strong. Similarly, I have been working with OpenGL for almost two decades, and I had great fun writing the 3D chapters. The hardware chapters reflect my current interest. Several project ideas did not make the cut, because we needed to balance the interestingness of a project with the complexity of implementation and explanation. One of them was a Raspberry Pi based robot, and another was on anamorphic projections – you might have seen a kind of street art where viewing it from a particular angle makes the drawing pop out in 3D.
InfoQ: Which project was most fun for you to implement? Which one the hardest?
I think the most fun project for me was the Laser audio display. The hardest one was the volume rendering project – but mostly in the explanation while writing.
InfoQ: The book also devotes a whole part to hardware hacking, which also implies some form of C-like programming. Would you explain the advantages of using Python for such kind of hardware-interfacing applications?
Python can be used with hardware as an interfacing language – where you use a full-fledged computer and something like the serial port to talk to hardware. Then you have hardware like the Raspberry Pi with an Operating System and built-in support for Python. The latter is very appealing, since it allows you to talk to hardware directly using Python. But you still need to be aware of limitations in using a high level language like Python on top of an operating system. In timing critical cases, it's often necessary to understand the hardware and code in a lower level language like C. But the good news is that you can easily create a Python binding for your C code once you are done. For example, look at the implementation of the Adafruit DHT library used in the Raspberry Pi weather monitor project. Another exciting development in this area which I'd like to explore is Micropython.
InfoQ: The code you provide is compatible with Python 2.7–3.3. While that choice is understandable in order to target as large an audience as possible, haven't you found yourself in a situation where using features only available in Python 3.x would have been of help? What is your opinion on the Python 2.7/3.x divide?
This book mostly uses core Python features and offloads the domain specific work to libraries. So in that sense, I didn't find myself to be restricted with respect to language versions. Regarding the 2.7/3.3 divide, I am in the camp of keeping things simple, so I would have been just happy with 2.7.
InfoQ: Python is one of the most widely used languages for scripting. Yet, Go has recently seen some adoption as a replacement for large Python scripts. Do you think this could be a stable trend? Is Python still the right language for medium to complex projects that were solved with Python in the last decade?
I have not explored Go, so I can't speak much about it. Since Google is behind it, I would expect traction for this language. I think the biggest appeal for Python is the simplicity of the language, and I experience this aspect first-hand as I teach Python to my 10 year old. More and more people are learning programming every day, and with the arrival of ultra-cheap hardware like the $5 Raspberry Pi Zero computer, I think we'll be seeing more Python programmers, not less.
InfoQ: Which of the new features that Python has been adding recently (async/await, type annotations, async IO, generators, etc.) can help programmers be more productive according to your experience?
Generator usage has become very common with 3.x, but with some of the other features, I think people tend to use them as the need arises, or if a particular library or framework requires it. Coming from a complex language (C++), my draw for Python remains in its simplicity and succinctness.
Download a sample chapter from Python Playground.
About the Book Author
Mahesh Venkitachalam is a software engineer with two decades of programming experience. He has nurtured a passion for technology since the eighth grade, which he channels into his popular electronics and programming blog, electronut.in.