Python Playground : Geeky Projects for the Curious Programmer

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Put the fun back in Python programming and build your skills as you create 3D simulations and graphics, speech-recognition machine-learning systems, IoT devices, and more. The fully updated 2nd edition is here, now with 5 brand-new projects! 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. Who Is This Book For? Python Playground was written for anyone curious about how to use programming to understand and explore ideas. The projects in this book assume that you know basic Python syntax and basic programming concepts and that you’re familiar with high school mathematics. I’ve done my best to explain in detail the math you need for all projects. This book isn’t intended to be your first book on Python. I won’t walk you through the basics. I will, however, show you how to use Python to solve a variety of real-world problems in a series of nontrivial projects. As you work through the projects, you’ll explore the nuances of the Python programming language and learn how to work with some popular Python libraries. But perhaps even more important, you’ll learn how to break down a problem into parts, develop an algorithm to solve that problem, and then implement a solution from the ground up using Python. It can be difficult to solve real-world problems because they are often open-ended and require expertise in various domains. But Python offers the tools to facilitate problem-solving. Overcoming difficulties and finding solutions to real problems are the most important parts of your journey on the way to becoming an expert programmer.

Author(s): Mahesh Venkitachalam
Edition: 2
Publisher: No Starch Press
Year: 2023

Language: English
Pages: 646

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