Impractical Python Projects is a collection of fun and educational projects designed to entertain programmers while enhancing their Python skills. It picks up where the complete beginner books leave off, expanding on existing concepts and introducing new tools that you'll use every day. And to keep things interesting, each project includes a zany twist featuring historical incidents, pop culture references, and literary allusions.
You'll flex your problem-solving skills and employ Python's many useful libraries to do things like:
• Help James Bond crack a high-tech safe with a hill-climbing algorithm
• Write haiku poems using Markov Chain Analysis
• Use genetic algorithms to breed a race of gigantic rats
• Crack the world's most successful military cipher using cryptanalysis
• Derive the anagram, "I am Lord Voldemort" using linguistical sieves
• Plan your parents' secure retirement with Monte Carlo simulation
• Save the sorceress Zatanna from a stabby death using palingrams
• Model the Milky Way and calculate our odds of detecting alien civilizations
• Help the world's smartest woman win the Monty Hall problem argument
• Reveal Jupiter's Great Red Spot using optical stacking
• Save the head of Mary, Queen of Scots with steganography
• Foil corporate security with invisible electronic ink
Simulate volcanoes, map Mars, and more, all while gaining valuable experience using free modules like Tkinter, matplotlib, Cprofile, Pylint, Pygame, Pillow, and Python-Docx.
Whether you're looking to pick up some new Python skills or just need a pick-me-up, you'll find endless educational, geeky fun with Impractical Python Projects.
Author(s): Lee Vaughan
Edition: 1
Publisher: No Starch Press
Year: 2018
Language: English
Commentary: True PDF
Pages: 424
City: San Francisco, CA
Tags: Genetic Algorithms; Natural Language Processing; Programming; Python; Cryptography; Finance; Monte Carlo Simulation; NLTK; Tkinter; PyGame; Probability Theory; Fraud Detection; Investment; Game Development; Teaching; Markov Models; Recreative Mathematics; Puzzles; Elementary; Monty Hall Problem; Haiku; Benford’s Law
Brief Contents
Contents In Detail
Acknowledgments
Introduction
Who This Book Is For
What’s in This Book
Python Version, Platform, and IDE
The Code
Coding Style
Where to Get Help
Onward!
Chapter 1: Silly Name Generator
Project #1: Generating Pseudonymns
Planning and Designing a Project
The Strategy
The Pseudocode
The Code
Using the Python Community’s Style Guide
Checking Your Code with Pylint
Describing Your Code with Docstrings
Checking Your Code Style
Summary
Further Reading
Pseudocode
Style Guides
Third-Party Modules
Practice Projects
Pig Latin
Poor Man’s Bar Chart
Challenge Projects
Poor Foreign Man’s Bar Chart
The Middle
Something Completely Different
Chapter 2: Finding Palingram Spells
Finding and Opening a Dictionary
Handling Exceptions When Opening Files
Loading the Dictionary File
Project #2: Finding Palindromes
The Strategy and Pseudocode
The Palindrome Code
Project #3: Finding Palingrams
The Strategy and Pseudocode
The Palingrams Code
Palingram Profiling
Palingram Optimization
dnE ehT
Further Reading
Practice Project: Dictionary Cleanup
Challenge Project: Recursive Approach
Chapter 3: Solving Anagrams
Project #4: Finding Single-Word Anagrams
The Strategy and Pseudocode
Anagram-Finder Code
Project #5: Finding Phrase Anagrams
The Strategy and Pseudocode
The Anagram Phrase Code
Project #6: Finding Voldemort: The Gallic Gambit
Project #7: Finding Voldemort: The British Brute-Force
Strategy
The British Brute-Force Code
Summary
Further Reading
Practice Project: Finding Diagrams
Challenge Project: Automatic Anagram Generator
Chapter 4: Decoding American Civil War Ciphers
Project #8: The Route Cipher
The Strategy
The Pseudocode
The Route Cipher Decryption Code
Hacking the Route Cipher
Adding a User Interface
Project #9: The Rail Fence Cipher
The Strategy
The Rail Fence Cipher Encryption Code
The Rail Fence Cipher Decryption Code
Summary
Further Reading
Practice Projects
Hacking Lincoln
Identifying Cipher Types
Storing a Key as a Dictionary
Automating Possible Keys
Route Transposition Cipher: Brute-Force Attack
Challenge Projects
Route Cipher Encoder
Three-Rail Fence Cipher
Chapter 5: Encoding English Civil War Ciphers
Project #10: The Trevanion Cipher
Strategy and Pseudocode
The Trevanion Cipher Code
Project #11: Writing a Null Cipher
The List Cipher Code
The List Cipher Output
Summary
Further Reading
Practice Projects
Saving Mary
The Colchester Catch
Chapter 6: Writing in Invisible Ink
Project #12: The Vigenère Cipher
The Platform
The Strategy
Creating Invisible Ink
Manipulating Word Documents with python-docx
Downloading the Assets
The Pseudocode
The Code
Importing python-docx, Creating Lists, and Adding a Letterhead
Formatting and Interleaving the Messages
Adding the Vigenère Cipher
Detecting the Hidden Message
Summary
Further Reading
Practice Project: Checking the Number of Blank Lines
Challenge Project: Using Monospace Font
Chapter 7: Breeding Giant Rats with Genetic Algorithms
Finding the Best of All Possible Solutions
Project #13: Breeding an Army of Super-Rats
Strategy
The Super-Rats Code
Summary
Project #14: Cracking a High-Tech Safe
Strategy
The Safecracker Code
Summary
Further Reading
Challenge Projects
Building a Rat Harem
Creating a More Efficient Safecracker
Chapter 8: Counting Syllables for Haiku Poetry
Japanese Haiku
Project #15: Counting Syllables
The Strategy
Using a Corpus
Installing NLTK
Downloading CMUdict
Counting Sounds Instead of Syllables
Handling Words with Multiple Pronunciations
Managing Missing Words
The Training Corpus
The Missing Words Code
The Count Syllables Code
Prepping, Loading, and Counting
Defining the main() Function
A Program to Check Your Program
Summary
Further Reading
Practice Project: Syllable Counter Vs. Dictionary File
Chapter 9: Writing Haiku with Markov Chain Analysis
Project #16: Markov Chain Analysis
The Strategy
Choosing and Discarding Words
Continuing from One Line to Another
The Pseudocode
The Training Corpus
Debugging
Building the Scaffolding
Using the logging Module
The Code
Setting Up
Building Markov Models
Choosing a Random Word
Applying the Markov Models
Generating the Haiku Lines
Writing the User Interface
The Results
Good Haiku
Seed Haiku
Summary
Further Reading
Challenge Projects
New Word Generator
Turing Test
Unbelievable! This Is Unbelievable! Unbelievable!
To Haiku, or Not to Haiku
Markov Music
Chapter 10: Are We Alone? Exploring the Fermi Paradox
Project #17: Modeling the Milky Way
The Strategy
Estimating the Number of Civilizations
Selecting Radio Bubble Dimensions
Generating a Formula for the Probability of Detection
The Probability-of-Detection Code
Calculating Probability of Detection for a Range of Civilizations
Generating a Predictive Formula and Checking the Results
Building the Graphical Model
Scaling the Graphical Model
The Galaxy Simulator Code
Results
Summary
Further Reading
Practice Projects
A Galaxy Far, Far Away
Building a Galactic Empire
A Roundabout Way to Predict Detectability
Challenge Projects
Creating a Barred-Spiral Galaxy
Adding Habitable Zones to Your Galaxy
Chapter 11: The Monty Hall Problem
Monte Carlo Simulation
Project #18: Verify vos Savant
The Strategy
The vos Savant Verification Code
Project #19: The Monty Hall Game
A Brief Introduction to Object-Oriented Programming
The Strategy and Pseudocode
Game Assets
The Monty Hall Game Code
Summary
Further Reading
Practice Problem: The Birthday Paradox
Chapter 12: Securing Your Nest Egg
Project #20: Simulating Retirement Lifetimes
The Strategy
Historical Returns Matter
The Greatest Uncertainty
A Qualitative Way to Present Results
The Pseudocode
Finding Historical Data
The Code
Importing Modules and Defining Functions to Load Data and Get User Input
Getting the User Input
Checking for Other Erroneous Input
Defining the Monte Carlo Engine
Simulating Each Year in a Case
Calculating the Probability of Ruin
Defining and Calling the main() Function
Using the Simulator
Summary
Further Reading
Challenge Problems
A Picture Is Worth a Thousand Dollars
Mix and Match
Just My Luck!
All the Marbles
Chapter 13: Simulating an Alien Volcano
Project #21: The Plumes of Io
A Slice of pygame
The Strategy
Using a Game Sketch to Plan
Planning the Particle Class
The Code
Importing Modules, Initiating pygame, and Defining Colors
Defining the Particle Class
Ejecting a Particle
Updating the Particle and Handling Boundary Conditions
Defining the main() Function
Completing the main() Function
Running the Simulation
Summary
Further Reading
Practice Project: Going the Distance
Challenge Project: Shock Canopy
Challenge Project: The Fountainhead
Challenge Project: With a Bullet
Chapter 14: Mapping Mars with the Mars Orbiter
Astrodynamics for Gamers
The Law of Universal Gravity
Kepler’s Laws of Planetary Motion
Orbital Mechanics
Project #22: The Mars Orbiter Game
The Strategy
Game Assets
The Code
Importing and Building a Color Table
Defining the Satellite Class Initialization Method
Setting the Satellite’s Initial Position, Speed, Fuel, and Sound
Firing Thrusters and Checking for Player Input
Locating the Satellite
Rotating the Satellite and Drawing Its Orbit
Updating the Satellite Object
Defining the Planet Class Initialization Method
Rotating the Planet
Defining the gravity() and update() Methods
Calculating Eccentricity
Defining Functions to Make Labels
Mapping Soil Moisture
Casting a Shadow
Defining the main() Function
Instantiating Objects, Setting Up Orbit Verification, Mapping, and Timekeeping
Starting the Game Loop and Playing Sounds
Applying Gravity, Calculating Eccentricity, and Handling Failure
Rewarding Success and Updating and Drawing Sprites
Displaying Instructions and Telemetry and Casting a Shadow
Summary
Challenge Projects
Game Title Screen
Smart Gauges
Radio Blackout
Scoring
Strategy Guide
Aerobraking
Intruder Alert!
Over the Top
Chapter 15: Improving Your Astrophotography with Planet Stacking
Project #23: Stacking Jupiter
The pillow Module
Working with Files and Folders
Directory Paths
The Shell Utilities Module
The Video
The Strategy
The Code
The Cropping and Scaling Code
The Stacking Code
The Enhancing Code
Summary
Further Reading
Challenge Project: Vanishing Act
Chapter 16: Finding Frauds with Benford’s Law
Project #24: Benford’s Law of Leading Digits
Applying Benford’s Law
Performing the Chi-Square Test
The Dataset
The Strategy
The Code
Importing Modules and Loading Data
Counting First Digits
Getting the Expected Counts
Determining Goodness of Fit
Defining the Bar Chart Function
Completing the Bar Chart Function
Defining and Running the main() Function
Summary
Further Reading
Practice Project: Beating Benford
Challenge Projects
Benfording the Battlegrounds
While No One Was Looking
Appendix: Practice Problem Solutions
Chapter 1: Silly Name Generator
Chapter 2: Finding Palingram Spells
Chapter 3: Solving Anagrams
Chapter 4: Decoding American Civil War Ciphers
Chapter 5: Encoding English Civil War Ciphers
Chapter 6: Writing in Invisible Ink
Chapter 8: Counting Syllables for Haiku Poetry
Chapter 10: Are We Alone? Exploring the Fermi Paradox
Chapter 11: The Monty Hall Problem
Chapter 13: Simulating an Alien Volcano
Chapter 16: Finding Frauds with Benford’s Law
Index