Dead Simple Python
Idiomatic Python for the Impatient Programmer
Jason C. McDonald
Dead Simple Python. Copyright 2023 by Jason C. McDonald.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
First printing
26 25 24 23 22 1 2 3 4 5
ISBN 13: 978-1-7185-0092-1 (print)
ISBN 13: 978-1-7185-0093-8 (ebook)
Publisher: William Pollock
Production Manager: Rachel Monaghan
Developmental Editor: Frances Saux
Production Editor: Rachel Monaghan
Cover and Interior Design: Octopod Studios
Cover Illustrator: Gina Redman
Technical Reviewers: Steven Bingler with Daniel Foerster, Ryan Palo, Denis Pobedrya, and Simon de Vlieger
Copyeditors: Doug McNair with Anne McDonald
Compositor: Ashley McKevitt, Happenstance Type-O-Rama
Proofreader: James M. Fraleigh
Indexer: JoAnne Burek
For information on distribution, bulk sales, corporate sales, or translations, please contact No Starch Press, Inc. directly at info@nostarch.com or:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Names: McDonald, Jason C., 1992- author.
Title: Dead simple Python : idiomatic Python for the impatient programmer / Jason C. McDonald.
Description: San Francisco, CA : No Starch Press, Inc., [2023] | Includes index.
Identifiers: LCCN 2022018822 (print) | LCCN 2022018823 (ebook) | ISBN 9781718500921 (print) | ISBN
9781718500938 (ebook)
Subjects: LCSH: Python (Computer program language) | Computer programming.
Classification: LCC QA76.73.P98 M397 2023 (print) | LCC QA76.73.P98
(ebook) | DDC 005.13/3--dc23/eng/20220616
LC record available at https://lccn.loc.gov/2022018822
LC ebook record available at https://lccn.loc.gov/2022018823
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
In loving memory of Chris Fox Frasier.
Itll always be lern u a snek to you.
About the Author
Jason C. McDonald is a software developer, speaker, and author of both fiction and nonfiction. By day, he works as a software engineer. By night, he is the founder of MousePaw Media (https://mousepawmedia.com/), an open source software company where he trains software development interns. You can usually find him haunting his local coffee shop.
About the Technical Reviewers
Steven Bingler is a software engineer in Boston, Massachusetts, with a masters degree in electrical engineering. He is an experienced technical reviewer who, in his spare time, enjoys cycling, bouldering, and finding new places to eat.
Denis Pobedrya, better known as deniska on the Libera.IRC network, is familiar with some darker corners of Pythons data model. He is a self-proclaimed jack-of-all-trades, working in many jobs (including warehouse management). He is currently a Python backend developer for a marketplace service.
Ryan Palo is a mechanical engineer turned software developer writing Python at Flashpoint. He has a masters in computer science (Intelligent Systems). He loves sharing his passion for physics, math, writing, and code, any way he can, primarily through his blog, https://www.assertnotmagic.com/. He can usually be found at home with his wife, daughters, cat, dog, various assorted Linux devices, and about eight too many ukuleles.
For Daniel Foerster, technical editing for Dead Simple Python was a labor of friendship and of passion. He was blessed to encounter Python as a young teen and hasnt met another language that fits his brain so well since. After years of leading development efforts in Python and getting involved with teaching programming in and beyond the workplace, Daniel has turned his vocational emphasis toward pedagogy.
Simon de Vlieger, better known in some circles as supakeen, is a part of the furniture on the Python IRC community. He is a senior programmer with an interest in Python, C, and experimental languages, plus an affinity for software security. Hes very much at home on the AVR and other tiny embedded chips. He is employed by Red Hat, where he currently works on building Linux in a sane and repeatable fashion.
Foreword
Dead simple concurrency? Seriously?
That was my reaction when I first heard about this book. Python in general is a wonderful language and easier to grok than most, but many (or even most) things in the world of programming are still anything but dead simple.
But then Jason explained what he had in mind in writing a book with those two words in the title. Its not dead simple in the sense of being a dumbed-down book that presents Python in overly simplistic terms. Rather, its dead simple in the sense of being a book that people will read and say, This topic initially seemed hard to understand, but now that youve explained it, its really obvious to me. Having studied and taught coding in general and Python in particular for 30 years, Id say that this is absolutely the best goal for any form of teaching: to present the concepts so clearly that the way even the tricky topics work seems inevitable.
But thats not the only area where Dead Simple Python is ambitious. The sheer amount of material presented here is equally impressive. This book covers virtually every aspect of Python that someone who wants to write useful code might need. These include everything from the basics of variables, data structures, and loops, to, yes, things like concurrency and parallelism. As Ive looked through this book, every single chapter has impressed me with its completeness and wealth of detail.
So, does this book really make concurrency (and everything else) seem dead simple? After years of teaching and writing, I know I cant answer that question for anyone else, but I can say for sure that the examples are engaging and well thought out, the explanations are clear and accessible, and the coverage is outstanding. Thats a really tough combination to beat.
Naomi Ceder
Acknowledgments
Youve heard the expression It takes a village to raise a child. In the same way, it takes a village to write a book. In the case of Dead Simple Python, several villages were involved!
The Python community, especially those residing in #python
on Libera.Chat IRC, have been been supporting, informing, and challenging me ever since my first Hello, world in Python. Most of my knowledge about this language traces back to the lovable nerds in that chatroom. Im thankful for all the feedback and insight theyve provided on