front matter

preface

Why write Python?

Python is an excellent, general-purpose programming language. You can write a program to send secret messages to your friends or to play chess. There are Python modules to help you wrangle complex scientific data, explore machine learning algorithms, and generate publication-ready graphics. Many college-level computer science programs have moved away from languages like C and Java to Python as their introductory language because Python is a relatively easy language to learn. We can use Python to study fundamental and powerful ideas from computer science. As I show you ideas like regular expressions and higher-order functions, I hope to encourage you to study further.

Why did I write this book?

Over the years, I’ve had many opportunities to help people learn programming, and I always find it rewarding. The structure of this book comes from my own experience in the classroom, where I think formal specifications and tests can be useful aids in learning how to break a program into smaller problems that need to be solved to create the whole program.

The biggest barrier to entry I’ve found when I’m learning a new language is that small concepts of the language are usually presented outside of any useful context. Most programming language tutorials will start with printing “HELLO, WORLD!” (and this is book is no exception). Usually that’s pretty simple. After that, I usually struggle to write a complete program that will accept some arguments and do something useful. In this book, I’ll show you many, many examples of programs that do useful things, in the hopes that you can modify these programs to make more programs for your own use.

More than anything, I think you need to practice. It’s like the old joke: “What’s the way to Carnegie Hall? Practice, practice, practice.” These coding challenges are short enough that you could probably finish each in a few hours or days. This is more material than I could work through in a semester-long university-level class, so I imagine the whole book will take you several months. I hope you will solve the problems, then think about them, and then return later to see if you can solve them differently, maybe using a more advanced technique or making them run faster.

acknowledgments

This being my first book, it has been interesting to note the many people who have helped me create it. It all started with a call with Mike Stephens, the acquisitions editor for Manning, who entertained the idea of a book on learning how to produce serious, tested software by writing silly games and puzzles. That eventually led to a call with Marjan Bace, the publisher, who was enthusiastic about using test-driven development ideas to motivate readers to actively engage with writing the programs.

My first development editor, Susanna Kline, had to help me wrestle the first few chapters of the book into something people would actually want to read. My second development editor, Elesha Hyde, provided patient and thoughtful guidance through months of writing, editing, and reviews. I thank my technical editors, Scott Chaussee, Al Scherer, and Mathijs Affourtit, for carefully checking all my code and text for mistakes. I appreciated the efforts of Manning’s MEAP team, especially Mehmed Pasic for producing the PDFs and giving me technical guidance on how to use AsciiDoc. I would also like to thank my project editor Deirdre Hiam, my copyeditor Andy Carroll, my proofreader Katie Tennant, and my review editor Aleksandar Dragosavljevic´. Also, the readers of the liveBook edition and the many technical reviewers who provided such great feedback: Amanda Debler, Conor Redmond, Drew Leon, Joaquin Beltran, José Apablaza, Kimberly Winston-Jackson, Maciej Jurkowski, Mafinar Khan, Manuel Ricardo Gonzalez Cova, Marcel van den Brink, Marcin Se˛k, Mathijs Affourtit, Paul R Hendrik, Shayn Cornwell, Víctor M. Pérez.

I especially want to acknowledge the countless people who create the open source software upon which all of this is built. From the people who maintain the Python language and modules and documentation to the countless hackers who answer questions on the internet, I thank you for all that you do.

Of course, none of this would have ever been possible without the love and support of my family, especially my wife, Lori Kindler, who has been an unbelievable source of love and support for over 27 years. (I’m still really, really sorry about wrecking on my mountain bike and the year it took for me to recover!) Our three children bring me such challenges and joy, and I hope that I am making them proud. They constantly have to feign interest in topics they know and care nothing about, and they have shown such patience for the many hours I’ve spent writing this book.

about this book

Who should read this book

After you read this book and write all the programs, I would hope that you will be a zealot for creating programs that are documented, tested, and reproducible.

I think my ideal reader is someone who’s been trying to learn to code well but isn’t quite sure how to level up. Perhaps you are someone who’s been playing with Python or some other language that has a similar syntax, like Java(Script) or Perl. Maybe you’ve cut your teeth on something really different, like Haskell or Scheme, and you’re wondering how to translate your ideas to Python. Maybe you’ve been writing Python for a while and are looking for interesting challenges with enough structure to help you know when you’re moving in the right direction.

This is a book that will teach you to write well-structured, documented, testable code in Python. The material introduces best practices from industry such as test-driven development--that’s when the tests for a program exist even before the program itself is written! I will show you how to read documentation and Python Enhancement Proposals (PEPs) and how to write idiomatic code that other Python programmers would immediately recognize and understand.

This is probably not an ideal book for the absolute beginning programmer. I assume no prior knowledge of the Python language specifically, because I’m thinking of someone who is coming from another language. If you’ve never written a program in any language at all, you might do well to come back to this material when you are comfortable with ideas like variables, loops, and functions.

How this book is organized: A roadmap

The book is written with chapters building on previous chapters, so I really recommend you start at the beginning and work sequentially through the material.

  • Every program uses command-line arguments, so we start off discussing how to use argparse to handle this. Every program is also tested, so you’ll have to learn how to install and use pytest. The introduction and chapter 1 will get you up and running.

  • Chapters 2-4 discuss the basic Python structures like strings, lists, and dictionaries.

  • Chapters 5 and 6 move into how we can work with files as input and output and how files are related to “standard in” and “standard out” (STDIN/STDOUT).

  • Chapters 7 and 8 start combining ideas so you can write more complicated programs.

  • Chapters 9 and 10 introduce the random module and how to control and test random events.

  • In chapters 11-13 you’ll learn more about compartmentalizing code into functions and how to write and run tests for them.

  • In chapters 14-18 we’ll start digging into denser topics like higher-order functions as well as regular expressions to find patterns of text.

  • In chapters 19-22 we’ll start writing more complex, “real-world” programs that will put all your skills together while pushing your knowledge of the Python language and testing.

About the code

Every program and test shown in the book can be found at https://github.com/kyclark/tiny_python_projects.

Software/hardware requirements

All the program were written and tested with Python 3.8, but version 3.6 would be sufficient for almost every program. Several additional modules are required, such as pytest for running the tests. There are instructions for how to use the pip module to install these.

liveBook discussion forum

Purchase of Tiny Python Projects includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum, go to https://livebook.manning.com/book/tiny-python-projects/welcome/v-6. You can also learn more about Manning's forums and the rules of conduct at https:// livebook.manning.com/#!/discussion.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking him some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

Other online resources

One element missing from many programming courses is a demonstration of how one can go from having no program to having one that works. In my classroom teaching, I spend a lot of time showing students how to start writing a program and then how to work through the process of adding and testing new features. I’ve recorded videos for each chapter and shared them at www.youtube.com/user/kyclark. There is a playlist for each chapter, and the videos follow the pattern of each chapter by introducing the problem and the language features you might use to write your program, followed by a discussion of the solution(s).

about the author

My name is Ken Youens-Clark. I work as a Senior Scientific Programmer at the University of Arizona. Most of my career has been spent working in bioinformatics, using computer science ideas to study biological data.

I began my undergraduate degree as a Jazz Studies major on the drum set at the University of North Texas in 1990. I changed my major a few times and eventually ended up with a BA in English literature in 1995. I didn’t really have a plan for my career, but I did like computers.

Around 1995, I stared tinkering with databases and HTML at my first job out of college, building the company’s mailing list and first website. I was definitely hooked! After that, I managed to learned Visual Basic on Windows 3.1 and, during the next few years, I programmed in several languages and companies before landing in a bioinformatics group at Cold Spring Harbor Laboratory in 2001, led by Lincoln Stein, a prominent author of books and modules in Perl and an early advocate for open software, data, and science. In 2014 I moved to Tucson, AZ, to work at the University of Arizona, where I completed my MS in Biosystems Engineering in 2019.

When I’m not coding, I like playing music, riding bikes, cooking, reading, and being with my wife and children.

about the cover

The figure on the cover of Tiny Python Projects is captioned “Femme Turc allant par les rues,” or “Turkish woman going through the streets.” The illustration is taken from a collection of dress costumes from various countries by Jacques Grasset de Saint-Sauveur (1757-1810), titled Costumes de Différents Pays, published in France in 1788. Each illustration is finely drawn and colored by hand. The rich variety of Grasset de Saint-Sauveur’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by their dress.

The way we dress has changed since then, and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns, regions, or countries. Perhaps we have traded cultural diversity for a more varied personal life--certainly for a more varied and fast-paced technological life.

At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Grasset de Saint-Sauveur’s pictures.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.218.215