Welcome to Manning India!

We are pleased to be able to offer regional eBook pricing for Indian residents.
All eBook prices are discounted 40% or more!
Tiny Python Projects
Learn coding and testing with puzzles and games
Ken Youens-Clark
  • MEAP began January 2020
  • Publication in Summer 2020 (estimated)
  • ISBN 9781617297519
  • 325 pages (estimated)
  • printed in black & white

Learn Python, one laugh at a time. No seriously, if you want to learn Python the way your next employer would expect you to, but don't want to get bored in the process, and learn really really quickly, read this!

Mafinar Khan
A long journey is really a lot of little steps. The same is true when you’re learning Python, so you may as well have some fun along the way! Written in a lighthearted style with entertaining exercises that build powerful skills, Tiny Python Projects takes you from amateur to Pythonista as you create 19 bitesize programs. Each tiny project teaches you a new programming concept, from the basics of lists and strings right through to regular expressions and randomness. Along the way you’ll also discover how testing can make you a better programmer in any language.
Table of Contents detailed table of contents

Why Write Python?

Who Am I?

Who Are You?

Why Did I Write This Book?

0 Why Did I Write This Book?

0 Getting Started: Introduction and Installation Guide

0.1 Using test-driven development

0.2 Setting up your environment

0.3 Code examples

0.4 Getting the code

0.5 Installing modules

0.6 Code formatters

0.7 Code linters

0.8 How to start writing new programs

0.9 Why Not Notebooks?

0.10 A Note about the lingo

1 How to write and test a Python program

1.1 Comment lines

1.2 Testing our program

1.3 Adding the shebang line

1.4 Making a program executable

1.5 Understanding $PATH

1.6 Altering your $PATH

1.7 Adding a parameter and help

1.8 Making the argument optional

1.9 Running our tests

1.10 Adding main

1.11 Adding get_args

1.11.1 Checking style and errors

1.12 Testing hello.py

1.13 Starting a new program with new.py

1.14 template.py as an alternative to new.py

1.15 Summary

2 The Crow’s Nest: Working with strings

2.1 Getting started

2.1.1 How to use the tests

2.1.2 Creating programs with new.py

2.1.3 Write, test, repeat

2.1.4 Defining your arguments

2.1.5 Concatenating strings

2.1.6 Variable types

2.1.7 Getting just part of a string

2.1.8 Finding help in the REPL

2.1.9 String methods

2.1.10 String comparisons

2.1.11 Conditional branching

2.1.12 String formatting

2.2 Solution

2.3 Discussion

2.3.1 Defining the arguments with get_args

2.3.2 The main thing

2.3.3 Classifying the first character of a word

2.3.4 Printing the results

2.3.5 Running the test suite

2.4 Summary

2.5 Going Further

3 Going on a picnic: Working with lists

3.1 Starting the program

3.2 Writing picnic.py

3.3 Introduction to lists

3.3.1 Adding one element to a list

3.3.2 Adding many elements to a list

3.3.3 Indexing lists

3.3.4 Slicing lists

3.3.5 Finding elements in a list

3.3.6 Removing elements from a list

3.3.7 Sorting and reversing a list

3.3.8 Lists are mutable

3.3.9 Joining a list

3.4 Conditional branching with if/elif/else

3.5 Solution

3.4 Discussion

3.4.1 Defining the arguments

3.4.2 Assigning and sorting the items

3.4.3 Formatting the items

3.4.4 Printing the items

3.4.5 Testing

3.5 Summary

3.6 Going Further

4 Jump the Five: Working with dictionaries

4.1 Dictionaries

4.1.1 Creating a dictionary

4.1.2 Accessing dictionary values

4.1.3 Other dictionary methods

4.2 Writing jump.py

4.3 Solution

4.4 Discussion

4.4.1 Defining the arguments

4.4.2 Using a dict for encoding

4.4.3 Method 1: Using a for loop to print each character

4.4.4 Method 2: Using a for loop to build a new string

4.4.5 Method 3: Using a for loop to build a new list

4.4.6 Method 4: List comprehension

4.4.7 Method 5: str.translate

4.4.8 (Not) using str.replace

4.5 Summary

4.6 Going Further

5 Howler: Working with files and STDOUT

5.1 Reading files

5.2 Writing files

5.3 Writing howler.py

5.4 Solution

5.5 Discussion

5.5.1 Defining the arguments

5.5.2 Reading input from a file or the command line

5.5.3 Choosing the output file handle

5.5.4 Printing the output

5.5.5 A low-memory version

5.6 Review

5.7 Going Further

6 Words Count: Reading files/STDIN, iterating lists, formatting strings

6.1 Writing wc.py

6.1.1 Defining file inputs

6.1.2 Iterating lists

6.1.3 What you’re counting

6.1.4 Formatting your results

6.2 Solution

6.3 Discussion

6.3.1 Defining the arguments

6.3.2 Reading a file using a for loop

6.4 Review

6.5 Going Further

7 Gashlycrumb: Looking items up in a dictionary

7.1 Writing gashlycrumb.py

7.2 Solution

7.3 Discussion

7.3.1 Handling the arguments

7.3.2 Reading the input file

7.3.3 Looping with for versus a list comprehensions

7.3.4 Dictionary lookups

7.4 Review

7.5 Going Further

8 Apples and Bananas: Find and replace

8.1 Altering strings

8.1.1 Using str.replace

8.1.2 Using str.translate

8.1.3 Other ways to mutate strings

8.2 Solution

8.3 Discussion

8.3.1 Defining the parameters

8.4 Eight ways to replace the vowels

8.4.1 Method 1: Iterate every character

8.4.2 Method 2: Using str.replace

8.4.3 Method 3: Using str.translate

8.4.4 Method 4: List comprehension

8.4.5 Method 5: List comprehension with function

8.4.6 Method 6: The map function

8.4.7 Method 7: Using map with a defined function

8.4.8 Method 8: Using regular expressions

8.5 Refactoring with tests

8.6 Review

8.7 Going Further

9 Dial-A-Curse: Generating random insults from lists of words

9.1 Writing abuse.py

9.1.1 Validating arguments

9.1.2 Importing and seeding the random module

9.1.3 Defining the adjectives and nouns

9.1.4 Taking random samples and choices

9.1.5 Formatting the output

9.2 Solution

9.3 Discussion

9.3.1 Defining the arguments

9.3.2 Using parser.error

9.3.3 Program exit values and STDERR

9.3.4 Controlling randomness with random.seed

9.3.5 Iterating for loops with range

9.3.6 Constructing the insults

9.4 Review

9.5 Going Further

10 Telephone: Randomly mutating strings

10.1 More briefing

10.2 Writing telephone.py

10.3 Calculating the number of mutations

10.4 The mutation space

10.5 Selecting the characters to mutate

10.5.1 Non-deterministic selection

10.5.2 Randomly sampling characters

10.6 Mutating a string

10.7 Time to write

10.8 Solution

10.9 Discussion

10.9.1 Defining the arguments

10.9.2 Mutating a string

10.9.3 Using a list instead of a str

10.10 Review

10.11 Going Further

11 Bottles of Beer Song: Writing and testing functions

11.1 Writing bottles.py

11.2 Counting down

11.3 Writing a function

11.4 Writing a test for verse

11.5 Using the verse function

11.6 Solution

11.7 Discussion

11.7.1 Defining the arguments

11.7.2 Counting down

11.7.3 Test-Driven Development

11.7.4 The verse function

11.7.5 Iterating through the verses

11.7.6 1500 other solutions

11.8 Review

11.9 Going Further

12 Ransom: Randomly capitalizing text

13 Twelve Days of Christmas: Algorithm design

14 Rhymer: Using regular expressions to create rhyming words

15 The Kentucky Friar: More regular expressions

16 The Scrambler: Randomly reordering the middles of words

17 Mad Libs: Using regular expressions

18 Gematria: Numeric encoding of text using ASCII values

19 Workout of the Day: Parsing CSV file, creating text table output

20 Password Strength: Generating a secure and memorable password

21 Tic-Tac-Toe: Exploring state

22 Tic-Tac-Toe Redux: An interactive version with type hints

Appendixes

Appendix A: Using argparse

A.1 Types of arguments

A.2 Starting off with new.py

A.3 Using argparse

A.3.1 Creating the parser

A.3.2 A positional parameter

A.3.3 An optional string parameter

A.3.4 An optional numeric parameter

A.3.5 An optional file parameter

A.3.6 A flag

A.3.7 Returning from get_args

A.4 Examples using argparse

A.4.1 A single, positional argument

A.4.2 Two different positional arguments

A.4.3 Restricting values using choices

A.4.4 Two of the same positional arguments

A.4.5 One or more of the same positional arguments

A.4.6 File arguments

A.4.7 Manually checking arguments

A.4.8 Automatic help

A.5 Summary

About the Technology

Simple yet powerful, Python is one of the world’s most popular programming languages. You can use Python to write everything from simple utility programs to complex web applications and deep learning models. Whether you’re a researcher, a marketer, a data scientist, or a professional developer, Python is a fantastic skill to have in your toolbox.

About the book

Tiny Python Projects teaches you the big ideas of Python programming through small puzzles, tasks, and games. Each chapter challenges you with a fun new Python program for you to write and run, including a Shakespearean insult generator, an unbreakable password creator, and various text encoders. As you practice core Python language features and coding skills, you’ll also explore the principles of test-driven development by running your programs against a suite of specially designed tests. Designed for reading cover to cover or just dipping into the chapters that interest you, this entertaining book will have you parsing command-line arguments, getting interactive user input, and understanding many other programming techniques that scale easily from tiny Python projects to big ones!

What's inside

  • Write command-line Python programs
  • Process a variety of command-line arguments, options, and flags
  • Write and run tests for programs and functions
  • Manipulate Python data structures including strings, lists, tuples, and dictionaries
  • Use list and dictionary comprehensions and higher-order functions like map and filter
  • Write and use regular expressions
  • Use and control of randomness
  • Downloadable testing suites for each project

About the reader

For readers with beginning programming skills in Python or another language.

About the author

Ken Youens-Clark works at the University of Arizona as a Senior Scientific Programmer. He has an MS in Biosystems Engineering and has been programming for over 20 years.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $39.99 pBook + eBook + liveBook
MEAP eBook $31.99 pdf + ePub + kindle + liveBook
Prices displayed in rupees will be charged in USD when you check out.

placing your order...

Don't refresh or navigate away from the page.

FREE domestic shipping on three or more pBooks