0%

Book Description

The projects are tiny, but the rewards are big: each chapter in Tiny Python Projects challenges you with a new Python program, including a password creator, a word rhymer, and a Shakespearean insult generator. As you complete these entertaining exercises, you’ll graduate from a Python beginner to a confident programmer—and you’ll have a good time doing it!

Table of Contents

  1. Tiny Python Projects
  2. Copyright
  3. brief contents
  4. contents
  5. front matter
    1. preface
      1. Why write Python?
      2. Why did I write this book?
    2. acknowledgments
    3. about this book
      1. Who should read this book
      2. How this book is organized: A roadmap
      3. About the code
      4. Software/hardware requirements
      5. iiveBook discussion forum
      6. Other online resources
    4. about the author
    5. about the cover
  6. 0 Getting started: Introduction and installation guide
    1. Writing command-line programs
    2. Using test-driven development
    3. Setting up your environment
    4. Code examples
    5. Getting the code
    6. Installing modules
    7. Code formatters
    8. Code linters
    9. How to start writing new programs
    10. Why not Notebooks?
    11. The scope of topics we’ll cover
    12. Why not object-oriented programming?
    13. A note about the lingo
  7. 1 How to write and test a Python program
    1. 1.1 Creating your first program
    2. 1.2 Comment lines
    3. 1.3 Testing your program
    4. 1.4 Adding the #! (shebang) line
    5. 1.5 Making a program executable
    6. 1.6 Understanding $PATH
      1. 1.6.1 Altering your $PATH
    7. 1.7 Adding a parameter and help
    8. 1.8 Making the argument optional
    9. 1.9 Running our tests
    10. 1.10 Adding the main() function
    11. 1.11 Adding the get_args() function
      1. 1.11.1 Checking style and errors
    12. 1.12 Testing hello.py
    13. 1.13 Starting a new program with new.py
    14. 1.14 Using template.py as an alternative to new.py
    15. Summary
  8. 2 The crow’s nest: Working with strings
    1. 2.1 Getting started
      1. 2.1.1 How to use the tests
      2. 2.1.2 Creating programs with new.py
      3. 2.1.3 Write, test, repeat
      4. 2.1.4 Defining your arguments
      5. 2.1.5 Concatenating strings
      6. 2.1.6 Variable types
      7. 2.1.7 Getting just part of a string
      8. 2.1.8 Finding help in the REPL
      9. 2.1.9 String methods
      10. 2.1.10 String comparisons
      11. 2.1.11 Conditional branching
      12. 2.1.12 String formatting
      13. 2.1.13 Time to write
    2. 2.2 Solution
    3. 2.3 Discussion
      1. 2.3.1 Defining the arguments with get_args()
      2. 2.3.2 The main() thing
      3. 2.3.3 Classifying the first character of a word
      4. 2.3.4 Printing the results
      5. 2.3.5 Running the test suite
    4. 2.4 Going further
    5. Summary
  9. 3 Going on a picnic: Working with lists
    1. 3.1 Starting the program
    2. 3.2 Writing picnic.py
    3. 3.3 Introducing lists
      1. 3.3.1 Adding one element to a list
      2. 3.3.2 Adding many elements to a list
      3. 3.3.3 Indexing lists
      4. 3.3.4 Slicing lists
      5. 3.3.5 Finding elements in a list
      6. 3.3.6 Removing elements from a list
      7. 3.3.7 Sorting and reversing a list
      8. 3.3.8 Lists are mutable
      9. 3.3.9 Joining a list
    4. 3.4 Conditional branching with if/elif/else
      1. 3.4.1 Time to write
    5. 3.5 Solution
    6. 3.6 Discussion
      1. 3.6.1 Defining the arguments
      2. 3.6.2 Assigning and sorting the items
      3. 3.6.3 Formatting the items
      4. 3.6.4 Printing the items
    7. 3.7 Going further
    8. Summary
  10. 4 Jump the Five: Working with dictionaries
    1. 4.1 Dictionaries
      1. 4.1.1 Creating a dictionary
      2. 4.1.2 Accessing dictionary values
      3. 4.1.3 Other dictionary methods
    2. 4.2 Writing jump.py
    3. 4.3 Solution
    4. 4.4 Discussion
      1. 4.4.1 Defining the parameters
      2. 4.4.2 Using a dict for encoding
      3. 4.4.3 Various ways to process items in a series
      4. 4.4.4 (Not) using str.replace()
    5. 4.5 Going further
    6. Summary
  11. 5 Howler: Working with files and STDOUT
    1. 5.1 Reading files
    2. 5.2 Writing files
    3. 5.3 Writing howler.py
    4. 5.4 Solution
    5. 5.5 Discussion
      1. 5.5.1 Defining the arguments
      2. 5.5.2 Reading input from a file or the command line
      3. 5.5.3 Choosing the output file handle
      4. 5.5.4 Printing the output
      5. 5.5.5 A low-memory version
    6. 5.6 Going further
    7. Summary
  12. 6 Words count: Reading files and STDIN, iterating lists, formatting strings
    1. 6.1 Writing wc.py
      1. 6.1.1 Defining file inputs
      2. 6.1.2 Iterating lists
      3. 6.1.3 What you’re counting
      4. 6.1.4 Formatting your results
    2. 6.2 Solution
    3. 6.3 Discussion
      1. 6.3.1 Defining the arguments
      2. 6.3.2 Reading a file using a for loop
    4. 6.4 Going further
    5. Summary
  13. 7 Gashlycrumb: Looking items up in a dictionary
    1. 7.1 Writing gashlycrumb.py
    2. 7.2 Solution
    3. 7.3 Discussion
      1. 7.3.1 Handling the arguments
      2. 7.3.2 Reading the input file
      3. 7.3.3 Using a dictionary comprehension
      4. 7.3.4 Dictionary lookups
    4. 7.4 Going further
    5. Summary
  14. 8 Apples and Bananas: Find and replace
    1. 8.1 Altering strings
      1. 8.1.1 Using the str.replace() method
      2. 8.1.2 Using str.translate()
      3. 8.1.3 Other ways to mutate strings
    2. 8.2 Solution
    3. 8.3 Discussion
      1. 8.3.1 Defining the parameters
      2. 8.3.2 Eight ways to replace the vowels
    4. 8.4 Refactoring with tests
    5. 8.5 Going further
    6. Summary
  15. 9 Dial-a-Curse: Generating random insults from lists of words
    1. 9.2 Writing abuse.py
      1. 9.1.1 Validating arguments
      2. 9.1.2 Importing and seeding the random module
      3. 9.1.3 Defining the adjectives and nouns
      4. 9.1.4 Taking random samples and choices
      5. 9.1.5 Formatting the output
    2. 9.2 Solution
    3. 9.3 Discussion
      1. 9.3.1 Defining the arguments
      2. 9.3.2 Using parser.error()
      3. 9.3.3 Program exit values and STDERR
      4. 9.3.4 Controlling randomness with random.seed()
      5. 9.3.5 Iterating with range() and using throwaway variables
      6. 9.3.6 Constructing the insults
    4. 9.4 Going further
    5. Summary
  16. 10 Telephone: Randomly mutating strings
    1. 10.1 Writing telephone.py
      1. 10.1.1 Calculating the number of mutations
      2. 10.1.2 The mutation space
      3. 10.1.3 Selecting the characters to mutate
      4. 10.1.4 Mutating a string
      5. 10.1.5 Time to write
    2. 10.2 Solution
    3. 10.3 Discussion
      1. 10.3.1 Mutating a string
      2. 10.3.2 Using a list instead of a str
    4. 10.4 Going further
    5. Summary
  17. 11 Bottles of Beer Song: Writing and testing functions
    1. 11.1 Writing bottles.py
      1. 11.1.1 Counting down
      2. 11.1.2 Writing a function
      3. 11.1.3 Writing a test for verse()
      4. 11.1.4 Using the verse() function
    2. 11.2 Solution
    3. 11.3 Discussion
      1. 11.3.1 Counting down
      2. 11.3.2 Test-driven development
      3. 11.3.3 The verse() function
      4. 11.3.4 Iterating through the verses
      5. 11.3.5 1,500 other solutions
    4. 11.4 Going further
    5. Summary
  18. 12 Ransom: Randomly capitalizing text
    1. 12.1 Writing ransom.py
      1. 12.1.1 Mutating the text
      2. 12.1.2 Flipping a coin
      3. 12.1.3 Creating a new string
    2. 12.2 Solution
    3. 12.3 Discussion
      1. 12.3.1 Iterating through elements in a sequence
      2. 12.3.2 Writing a function to choose the letter
      3. 12.3.3 Another way to write list.append()
      4. 12.3.4 Using a str instead of a list
      5. 12.3.5 Using a list comprehension
      6. 12.3.6 Using a map() function
    4. 12.4 Comparing methods
    5. 12.5 Going further
    6. Summary
  19. 13 Twelve Days of Christmas: Algorithm design
    1. 13.1 Writing twelve_days.py
      1. 13.1.1 Counting
      2. 13.1.2 Creating the ordinal value
      3. 13.1.3 Making the verses
      4. 13.1.4 Using the verse() function
      5. 13.1.5 Printing
      6. 13.1.6 Time to write
    2. 13.2 Solution
    3. 13.3 Discussion
      1. 13.3.1 Making one verse
      2. 13.3.2 Generating the verses
      3. 13.3.3 Printing the verses
    4. 13.4 Going further
    5. Summary
  20. 14 Rhymer: Using regular expressions to create rhyming words
    1. 14.1 Writing rhymer.py
      1. 14.1.1 Breaking a word
      2. 14.1.2 Using regular expressions
      3. 14.1.3 Using capture groups
      4. 14.1.4 Truthiness
      5. 14.1.5 Creating the output
    2. 14.2 Solution
    3. 14.3 Discussion
      1. 14.3.1 Stemming a word
      2. 14.3.2 Formatting and commenting the regular expression
      3. 14.3.3 Using the stemmer() function outside your program
      4. 14.3.4 Creating rhyming strings
      5. 14.3.5 Writing stemmer() without regular expressions
    4. 14.4 Going further
    5. Summary
  21. 15 The Kentucky Friar: More regular expressions
    1. 15.1 Writing friar.py
      1. 15.1.1 Splitting text using regular expressions
      2. 15.1.2 Shorthand classes
      3. 15.1.3 Negated shorthand classes
      4. 15.1.4 Using re.split() with a captured regex
      5. 15.1.5 Writing the fry() function
      6. 15.1.6 Using the fry() function
    2. 15.2 Solution
    3. 15.3 Discussion
      1. 15.3.1 Writing the fry() function manually
      2. 15.3.2 Writing the fry() function with regular expressions
    4. 15.4 Going further
    5. Summary
  22. 16 The scrambler: Randomly reordering the middles of words
    1. 16.1 Writing scrambler.py
      1. 16.1.1 Breaking the text into lines and words
      2. 16.1.2 Capturing, non-capturing, and optional groups
      3. 16.1.3 Compiling a regex
      4. 16.1.4 Scrambling a word
      5. 16.1.5 Scrambling all the words
    2. 16.2 Solution
    3. 16.3 Discussion
      1. 16.3.1 Processing the text
      2. 16.3.2 Scrambling a word
    4. 16.4 Going further
    5. Summary
  23. 17 Mad Libs:Using regular expressions
    1. 17.1 Writing mad.py
      1. 17.1.1 Using regular expressions to find the pointy bits
      2. 17.1.2 Halting and printing errors
      3. 17.1.3 Getting the values
      4. 17.1.4 Substituting the text
    2. 17.2 Solution
    3. 17.3 Discussion
      1. 17.3.1 Substituting with regular expressions
      2. 17.3.2 Finding the placeholders without regular expressions
    4. 17.4 Going further
    5. Summary
  24. 18 Gematria: Numeric encoding of text using ASCII values
    1. 18.1 Writing gematria.py
      1. 18.1.1 Cleaning a word
      2. 18.1.2 Ordinal character values and ranges
      3. 18.1.3 Summing and reducing
      4. 18.1.4 Using functools.reduce
      5. 18.1.5 Encoding the words
      6. 18.1.6 Breaking the text
    2. 18.2 Solution
    3. 18.3 Discussion
      1. 18.3.1 Writing word2num()
      2. 18.3.2 Sorting
      3. 18.3.3 Testing
    4. 18.4 Going further
    5. Summary
  25. 19 Workout of the Day: Parsing CSV files, creating text table output
    1. 19.1 Writing wod.py
      1. 19.1.1 Reading delimited text files
      2. 19.1.2 Manually reading a CSV file
      3. 19.1.3 Parsing with the csv module
      4. 19.1.4 Creating a function to read a CSV file
      5. 19.1.5 Selecting the exercises
      6. 19.1.6 Formatting the output
      7. 19.1.7 Handling bad data
      8. 19.1.8 Time to write
    2. 19.2 Solution
    3. 19.3 Discussion
      1. 19.3.1 Reading a CSV file
      2. 19.3.2 Potential runtime errors
      3. 19.3.3 Using pandas.read_csv() to parse the file
      4. 19.3.4 Formatting the table
    4. 19.4 Going further
    5. Summary
  26. 20 Password strength: Generating a secure and memorable password
    1. 20.1 Writing password.py
      1. 20.1.1 Creating a unique list of words
      2. 20.1.2 Cleaning the text
      3. 20.1.3 Using a set
      4. 20.1.4 Filtering the words
      5. 20.1.5 Titlecasing the words
      6. 20.1.6 Sampling and making a password
      7. 20.1.7 l33t-ify
      8. 20.1.8 Putting it all together
    2. 20.2 Solution
    3. 20.3 Discussion
      1. 20.3.1 Cleaning the text
      2. 20.3.2 A king’s ransom
      3. 20.3.3 How to l33t()
      4. 20.3.4 Processing the files
      5. 20.3.5 Sampling and creating the passwords
    4. 20.4 Going further
    5. Summary
  27. 21 Tic-Tac-Toe: Exploring state
    1. 21.1 Writing tictactoe.py
      1. 21.1.1 Validating user input
      2. 21.1.2 Altering the board
      3. 21.1.3 Printing the board
      4. 21.1.4 Determining a winner
    2. 21.2 Solution
      1. 21.2.1 Validating the arguments and mutating the board
      2. 21.2.2 Formatting the board
      3. 21.2.3 Finding the winner
    3. 21.3 Going further
    4. Summary
  28. 22 Tic-Tac-Toe redux: An interactive version with type hints
    1. 22.1 Writing itictactoe.py
      1. 22.1.1 Tuple talk
      2. 22.1.2 Named tuples
      3. 22.1.3 Adding type hints
      4. 22.1.4 Type verification with Mypy
      5. 22.1.5 Updating immutable structures
      6. 22.1.6 Adding type hints to function definitions
    2. 22.2 Solution
      1. 22.2.1 A version using TypedDict
      2. 22.2.2 Thinking about state
    3. 22.3 Going further
    4. Summary
  29. Epilogue
  30. Appendix. Using argparse
    1. A.1 Types of arguments
    2. A.2 Using a template to start a program
    3. A.3 Using argparse
      1. A.3.1 Creating the parser
      2. A.3.2 Creating a positional parameter
      3. A.3.3 Creating an optional string parameter
      4. A.3.4 Creating an optional numeric parameter
      5. A.3.5 Creating an optional file parameter
      6. A.3.6 Creating a flag option
      7. A.3.7 Returning from get_args
    4. A.4 Examples using argparse
      1. A.4.1 A single positional argument
      2. A.4.2 Two different positional arguments
      3. A.4.3 Restricting values using the choices option
      4. A.4.4 Two of the same positional arguments
      5. A.4.5 One or more of the same positional arguments
      6. A.4.6 File arguments
      7. A.4.7 Manually checking arguments
      8. A.4.8 Automatic help
    5. Summary
  31. index
13.59.100.42