Python Playground

Python Playground

Geeky Projects for the Curious Programmer
by Mahesh Venkitachalam
October 2015, 352 pp.
ISBN-13: 
978-1-59327-604-1

Python is a powerful programming language that’s easy to learn and fun to play with. But once you’ve gotten a handle on the basics, what do you do next?

Python Playground is a collection of imaginative programming projects that will inspire you to use Python to make art and music, build simulations of real-world phenomena, and interact with hardware like the Arduino and Raspberry Pi. You’ll learn to use common Python tools and libraries like numpy, matplotlib, and pygame to do things like:

  • Generate Spirograph-like patterns using parametric equations and the turtle module
  • Create music on your computer by simulating frequency overtones
  • Translate graphical images into ASCII art
  • Write an autostereogram program that produces 3D images hidden beneath random patterns
  • Make realistic animations with OpenGL shaders by exploring particle systems, transparency, and billboarding techniques
  • Construct 3D visualizations using data from CT and MRI scans
  • Build a laser show that responds to music by hooking up your computer to an Arduino

Programming shouldn’t be a chore. Have some solid, geeky fun with Python Playground.

The projects in this book are compatible with both Python 2 and 3.

Author Bio 

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.

Table of contents 

Part 1: Warming Up

Chapter 1: Parsing iTunes Playlists
Chapter 2: Spirographs

Part 2: Simulating Life

Chapter 3: Conway’s Game of Life
Chapter 4: Generating Musical Overtones with the Karplus-Strong Algorithm
Chapter 5: Boids: Simulating a Flock

Part 3: Fun with Images

Chapter 6: ASCII Art
Chapter 7: Photomosaics
Chapter 8: Autostereograms

Part 4: Enter 3D

Chapter 9: Understanding OpenGL
Chapter 10: Particle Systems
Chapter 11: Volume Rendering

Part 5: Hardware Hacking

Chapter 12: Introduction to Arduino
Chapter 13: Laser Audio Display
Chapter 14: A Raspberry Pi–based Weather Monitor

Appendix A: Software Installation
Appendix B: Basic Practical Electronics
Appendix C: Raspberry Pi Tips & Tricks

View the detailed Table of Contents (PDF)
View the detailed Index

Reviews 

"If you want to become adept at doing clever things with Python, I doubt you'll find a better group of projects or more useful help for understanding how the language works."
IT World

"This is a book that belongs in every Python programmer's library."
Full Circle Magazine

"Packed with interesting projects."
iProgrammer

Read an interview with Mahesh Venkitachalam on InfoQ!

Mahesh Venkitachalam was selected as the Python Developer of the Week on Mouse v. Python! Read the full interview here.

"Python Playground targets programmers that want to further improve their skills and knowledge of the language. The book does a good job at explaining all relevant details and makes sure that readers get a clear pictures of what is going on."
InfoQ

Updates 

Page 19: the first sentence should read: "Let’s begin by considering that the equation used to describe a circle with radius r, centered at the origin of a two-dimensional plane, is + = .

The formula should be y = b + r sin(theta) in place of y = b + r cos(theta)

Page 20: In Figure 2-3, the angle theta (Θ) should be between the x-axis and the segment OC.

In the formula for y, we're subtracting lksin((1 - k) / k * Θ), not adding it.

Page 47: in the 3rd line of the last code block, argumentss should instead read arguments

Page 58: in the 4th sentence of the second paragraph, "step 1" should instead read "step 2"

Page 61: the 2nd to last line of the final code block that reads:
>>> print d
should instead read:
>>> print(d)

Page 62: the line labeled (4) that reads:
avg = 0.996*0.5*(buf[0] + buf[1])
should instead read:
avg = 0.995*0.5*(buf[0] + buf[1])

Page 64: in the 2nd line of the code block, Karplus String should instead read Karplus-Strong

Page 65: in the 3rd full paragraph, -play should instead read --play

Page 67: in the 8th line of the code block, Karplus String should instead read Karplus-Strong

Page 68: in the 2nd code block, -display should instead read --display

Page 69: in the code block, the line that currently reads:
$ python ks.py -play
should instead read:
$ python3 ks.py --play

and the first sentence of step 3 that currently reads:
"Add a --piano command line option to the project."
should instead read:
"Modify the --piano command line option for the project."

Page 73: in the second to last sentence on the page:
"If you draw a line from the origin to a point on this circle, it becomes a unit vector that depends on the angle A."
should instead read:
"If you draw a line from the origin to a point on this circle, it becomes a unit vector that depends on the angle t."

Page 78: on both the 3rd and 7th lines of the code block, distMatrix should instead read self.distMatrix
and on the 11th line of the code block, remove the semicolon from the line vel += vel2; so that it reads: vel += vel2

Page 83: remove the semicolon from the line vel += vel2; so that it reads: vel += vel2

Page 124: the 1st sentence of the 1st paragraph which currently reads:
"At (1), you perform a sanity check to ensure that the depth map and the image have the same dimensions."
should instead read:
"At (1), you convert the depth map into a single channel image if needed."

and in the 1st sentence of the 2nd paragraph, Image.Load() should instead read Image.load()

Page 128: in the final code block, -tile should instead read --tile

Page 138: the last sentence before the equation that reads:
"See how the matrix multiplication translates a point (x, y, z, 1.0) to (x + tx, y + ty, z + tz, 1.0)."
should instead read:
"See how the matrix multiplication translates a point (x, y, z, 1.0) to (x + , y + , z + tᶻ, 1.0)."

Page 150: the line labeled (1) should be deleted, and the (1) label should be moved to the following line: in vec2 vTexCoord;

and the sentence following the code block, that currently reads:
"Starting at (1), you define inputs to the fragment shader—the same color and texture coordinate variables you set as output in the vertex shader."
should instead read:
"Starting at (1), you define inputs to the fragment shader—the same texture coordinate variable you set as output in the vertex shader."

Page 156: the code block that currently reads:
$python simpleglfw.py
should instead read:
$python3 simpleglfw.py

Page 163: the second equation, which currently reads:
V = (cos(θ)sin(ϕ),sin(θ)sin(ϕ),cos(ϕ))
should instead read:
V = (cos(ϕ)sin(θ),sin(ϕ)sin(θ),cos(θ))

Page 168: in the sentence following the first code block, "-1" should not be a subscript

Page 171: in the line labeled with (1) in the code block:
vec4 texCol = texture2D(uSampler, vec2(vTexCoord.s, vTexCoord.t));
should instead read:
vec4 texCol = texture(uSampler, vec2(vTexCoord.s, vTexCoord.t));

Page 186: in the 3rd sentence, "0.1 seconds" should instead read "0.01 seconds"