Python Playground, 2nd Edition

Book description

Harness the power of Python as you turn code into tangible creations with Python Playground, a collection of 15 inventive projects that will expand your programming horizons, spark your curiosity, and elevate your coding skills.

Go beyond the basics as you write programs to generate art and music, simulate real-world phenomena, and interact with hardware, all through the use of Python and common libraries such as numpy, matplotlib, and Pillow.

As you work through the book’s projects, you will:

•Craft intricate Spirograph-like designs with parametric equations and the turtle module
•Generate music by synthesizing plucked string sounds
•Transform everyday images into ASCII art, photomosaics, and eye-popping autostereograms
•Design engaging cellular automata and flocking simulations
•Explore the realm of 3D graphics, from basic shape rendering to visualizing MRI scan data
•Build a Raspberry Pi–powered laser show that dances along with music

New to this edition: We’ve expanded your playground with five new projects: you’ll draw fractals, bring Conway’s Game of Life into 3D space, and use a Raspberry Pi and Python to create a musical instrument, an IoT garden monitor, and even a machine learning–driven speech recognition system.

Whether you’re a seasoned professional or just getting started, you’ll find Python Playground to be a great way to learn, experiment with, and master this versatile programming language.

Covers Python 3.x

Table of contents

  1. Cover
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. Brief Contents
  6. Contents in Detail
  7. Acknowledgments
  8. Introduction
    1. Who Is This Book For?
    2. What’s in This Book?
    3. What’s New to the Second Edition?
    4. Why Python?
    5. The Code in This Book
  9. Part I: Warming Up
    1. 1 The Koch Snowflake
      1. How It Works
        1. Using Recursion
        2. Computing the Snowflake
        3. Drawing with turtle Graphics
      2. Requirements
      3. The Code
        1. Calculating the Points
        2. Recursing
        3. Drawing a Flake
        4. Writing the main() Function
      4. Running the Snowflake Code
      5. Summary
      6. Experiments!
      7. The Complete Code
    2. 2 Spirographs
      1. How It Works
        1. Understanding Parametric Equations
        2. Drawing Curves with turtle Graphics
      2. Requirements
      3. The Code
        1. Drawing the Spiros
        2. Coordinating the Animation
        3. Saving the Curves
        4. Parsing Command Line Arguments and Initialization
      4. Running the Spirograph Animation
      5. Summary
      6. Experiments!
      7. The Complete Code
  10. Part II: Simulating Life
    1. 3 Conway’s Game of Life
      1. How It Works
      2. Requirements
      3. The Code
        1. Representing the Grid
        2. Setting the Initial Conditions
        3. Enforcing the Boundary Conditions
        4. Implementing the Rules
        5. Sending Command Line Arguments to the Program
        6. Initializing the Simulation
      4. Running the Game of Life Simulation
      5. Summary
      6. Experiments!
      7. The Complete Code
    2. 4 Musical Overtones with Karplus-Strong
      1. How It Works
        1. The Simulation
        2. The WAV File Format
        3. The Minor Pentatonic Scale
      2. Requirements
      3. The Code
        1. Implementing the Ring Buffer with deque
        2. Implementing the Karplus-Strong Algorithm
        3. Writing a WAV File
        4. Playing WAV Files with pyaudio
        5. Creating Notes and Parsing Arguments
      4. Running the Plucked String Simulation
      5. Summary
      6. Experiments!
      7. The Complete Code
    3. 5 Flocking Boids
      1. How It Works
      2. Requirements
      3. The Code
        1. Initializing the Simulation
        2. Setting Boundary Conditions
        3. Drawing a Boid
        4. Applying the Rules of the Boids
        5. Influencing the Simulation
        6. Incrementing the Simulation
        7. Parsing Arguments and Instantiating the Boids
      4. Running the Boids Simulation
      5. Summary
      6. Experiments!
      7. The Complete Code
  11. Part III: Fun with Images
    1. 6 ASCII Art
      1. How It Works
      2. Requirements
      3. The Code
        1. Defining the Grayscale Levels and Grid
        2. Computing the Average Brightness
        3. Generating the ASCII Content from the Image
        4. Creating Command Line Options
        5. Writing the ASCII Art Strings to a Text File
      4. Running the ASCII Art Generator
      5. Summary
      6. Experiments!
      7. The Complete Code
    2. 7 Photomosaics
      1. How It Works
        1. Splitting the Target Image
        2. Averaging Color Values
        3. Matching Images
      2. Requirements
      3. The Code
        1. Reading In the Input Images
        2. Calculating the Average Color Value of an Image
        3. Splitting the Target Image into a Grid
        4. Finding the Best Match for a Tile
        5. Creating an Image Grid
        6. Creating the Photomosaic
        7. Writing the main() Function
      4. Running the Photomosaic Generator
      5. Summary
      6. Experiments!
      7. The Complete Code
    3. 8 Autostereograms
      1. How It Works
        1. Perceiving Depth in an Autostereogram
        2. Working with Depth Maps
        3. Shifting Pixels
      2. Requirements
      3. The Code
        1. Creating a Tile from Random Circles
        2. Repeating a Given Tile
        3. Creating Autostereograms
        4. Providing Command Line Options
      4. Running the Autostereogram Generator
      5. Summary
      6. Experiments!
      7. The Complete Code
  12. Part IV: Enter 3D
    1. 9 Understanding OpenGL
      1. How OpenGL Works
        1. Geometric Primitives
        2. 3D Transformations
        3. Shaders
        4. Vertex Buffers
        5. Texture Mapping
        6. The OpenGL Context
      2. Requirements
      3. The Code
        1. The RenderWindow Class
        2. The Scene Class
        3. Utility Functions
      4. Running the OpenGL Application
      5. Summary
      6. Experiments!
      7. The Complete Code
    2. 10 Conway’s Game of Life on a Torus
      1. How It Works
        1. Computing Vertices
        2. Computing Normals for Lighting
        3. Rendering
        4. Coloring the Triangle Strips
        5. Controlling the Camera
        6. Mapping the Grid to the Torus
      2. Requirements
      3. The Code
        1. Rendering the Torus
        2. Implementing the Game of Life Simulation
        3. Creating the Camera
        4. Putting Everything Together
      4. Running the 3D Game of Life Simulation
      5. Summary
      6. Experiments!
      7. The Complete Torus Rendering Code
      8. The Complete Game of Life Simulation Code
      9. The Complete Camera Code
      10. The Complete RenderWindow Code
    3. 11 Volume Rendering
      1. How It Works
        1. The Data Format
        2. Ray Generation
        3. The OpenGL Window
      2. Requirements
      3. The Code
        1. Generating a 3D Texture
        2. Generating Rays
        3. Implementing the Ray Casting Algorithm
        4. Showing 2D Slices
        5. Putting the Code Together
      4. Running the Program
      5. Summary
      6. Experiments!
      7. The Complete 3D Texture Code
      8. The Complete Ray Generation Code
      9. The Complete Volume Ray Casting Code
      10. The Complete 2D Slicing Code
      11. The Complete Main File Code
  13. Part V: Hardware Hacking
    1. 12 Karplus-Strong on a Raspberry Pi Pico
      1. How It Works
        1. Input and Output
        2. The I2S Protocol
      2. Requirements
        1. Hardware Setup
        2. MicroPython Setup
      3. The Code
        1. Setting Up
        2. Generating the Notes
        3. Playing a Note
        4. Writing the main() Function
      4. Running the Pico Code
      5. Summary
      6. Experiments!
      7. The Complete Code
    2. 13 Laser Audio Display with a Raspberry Pi
      1. How It Works
        1. Generating Patterns with a Laser
        2. Analyzing Audio with the Fast Fourier Transform
      2. Requirements
        1. Setting Up the Raspberry Pi
        2. Constructing the Laser Display
        3. Hooking Up the Hardware
      3. The Code
        1. Setting Up
        2. Controlling the Hardware
        3. Processing the Audio
        4. Testing the Motors
        5. Putting It All Together
      4. Running the Laser Display
      5. Summary
      6. Experiments!
      7. The Complete Code
    3. 14 IoT Garden
      1. How It Works
        1. Bluetooth Low Energy
        2. The Bottle Web Framework
        3. The SQLite Database
      2. Requirements
        1. Raspberry Pi Setup
        2. CircuitPython Setup
        3. If This Then That Setup
      3. The Code
        1. The CircuitPython Code
        2. The BLE Scanner Code
        3. The Web Server Code
        4. The Main Program File
      4. Running the IoT Garden
      5. Summary
      6. Experiments!
      7. The Complete CircuitPython Code
      8. The Complete BLE Scanner Code
      9. The Complete Python Web Server Code
      10. The Complete Main Program Code
    4. 15 Audio ML on Pi
      1. A Machine Learning Overview
      2. How It Works
        1. Spectrograms
        2. Inference on the Raspberry Pi
      3. Requirements
      4. The Code
        1. Training the Model in Google Colab
        2. Using the Model on the Raspberry Pi
      5. Running the Speech Recognition System
      6. Summary
      7. Experiments!
      8. The Complete Code
    5. A Python Installation
      1. Installing Source Code for the Book’s Projects
      2. Installing Python and Python Modules
        1. Windows
        2. macOS
        3. Linux
    6. B Raspberry Pi Setup
      1. Setting Up the Software
      2. Testing Your Connection
      3. Logging in to Your Pi with SSH
      4. Installing Python Modules
      5. Working Remotely with Visual Studio Code
  14. Index

Product information

  • Title: Python Playground, 2nd Edition
  • Author(s): Mahesh Venkitachalam
  • Release date: November 2023
  • Publisher(s): No Starch Press
  • ISBN: 9781718503045