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. 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 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. 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 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 ArduinoProgramming 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. Python is a powerful programming language thats easy to learn and fun to play with. But once youve 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. Youll 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 shouldnt be a chore. Have some solid, geeky fun with Python Playground . The projects in this book are compatible with both Python 2 and 3. Python is a powerful programming language that's fun to play around with. Getting to know Python isn't just about hammering syntax into your brain--it's about discovering new tools to explore whatever excites you. Python Playground is a collection of fun programming projects that will inspire you to new heights as a Pythonista. Inside, you'll learn to use Python for all kinds of playful purposes. You'll manipulate images, build simulations, and interact with hardware using Arduino and Raspberry Pi. As you work through each project, you'll power up your programming skills as you learn how to leverage external libraries for specialized tasks, how to break problems into smaller, solvable pieces, and how to translate an algorithm into code Provides instructions for programming projects using Python that teach such programming skills as how to leverage external libraries for specialized tasks, how to break problems into solvable pieces, and how to translate an algorithm into code