Python Tricks: The Book
For online information and ordering of this and other books by Dan Bader, please visit dbader.org. For more information, please contact Dan Bader at mail@dbader.org.
Copyright Dan Bader (dbader.org), 20162017
ISBN: 9781775093305 (paperback)
ISBN: 9781775093312 (electronic)
Cover design by Anja Pircher Design (anjapircher.com)
Python and the Python logos are trademarks or registered trademarks of the Python Software Foundation, used by Dan Bader with permission from the Foundation.
Thank you for downloading this ebook. This ebook is licensed for your personal enjoyment only. This ebook may not be re-sold or given away to other people. If you would like to share this book with another person, please purchase an additional copy for each recipient. If youre reading this book and did not purchase it, or it was not purchased for your use only, then please return to dbader.org/pytricks-book and purchase your own copy. Thank you for respecting the hard work behind this book.
Updated 2017-10-27 I would like to thank Michael Howitz, Johnathan Willitts, Julian Orbach, Johnny Giorgis, Bob White, Daniel Meyer, Michael Stueben, Smital Desai, Andreas Kreisig, David Perkins, Jay Prakash Singh, and Ben Felder for their excellent feedback.
What Pythonistas Say About Python Tricks: The Book
Im often amazed at some of the answers coworkers give me. Dict comprehensions, lambdas, and generators often pepper their feedback. I am always impressed and yet flabbergasted at how powerful Python is when you know these tricks and can implement them correctly.
Your book was exactly what I wanted to help get me from a bewildered powershell scripter to someone who knows how and when to use these Pythonic tricks everyone has been talking about.
Daniel Meyer, Sr. Desktop Administrator at Tesla Inc.
He showed me the book via video conferencing and I sort of skimmed through it as he flipped the pages for me, and I was immediately curious to read more.
That same afternoon I purchased my own copy and proceeded to read your explanation for the way dictionaries are created in Python and later that day, as I met a different co-worker for coffee, I used the same trick on him :)
He then sprung a different question on the same principle, and because of the way you explained things in your book, I was able tonot* guess the result but correctly answer what the outcome would be. That means that you did a great job at explaining things :)*
Og Maciel, Python Developer at Red Hat
It is not just code samples, it discusses relevant implementation details comprehensibly. What really matters though is that this book makes you write better Python code!
Bob Belderbos, Engineer at Oracle & Co-Founder of PyBites
Foreword
Its been almost ten years since I first got acquainted with Python as a programming language. When I first learned Python many years ago, it was with a little reluctance. I had been programming in a different language before, and all of the sudden at work, I was assigned to a different team where everyone used Python. That was the beginning of my own Python journey.
When I was first introduced to Python, I was told that it was going to be easy, that I should be able to pick it up quickly. When I asked my colleagues for resources for learning Python, all they gave me was a link to Pythons official documentation. Reading the documentation was confusing at first, and it really took me a while before I even felt comfortable navigating through it. Often I found myself needing to look for answers in StackOverflow.
Coming from a different programming language, I wasnt looking for just any resource for learning how to program or what classes and objects are. I was looking for specific resources that would teach me the features of Python, what sets it apart, and how writing in Python is different than writing code in another language.
It really has taken me many years to fully appreciate this language. As I read Dans book, I kept thinking that I wished I had access to a book like this when I started learning Python many years ago.
For example, one of the many unique Python features that surprised me at first were list comprehensions. As Dan mentions in the book, a tell of someone who just came to Python from a different language is the way they use for-loops. I recall one of the earliest code review comments I got when I started programming in Python was, Why not use list comprehension here? Dan explains this concept clearly in section 6, starting by showing how to loop the Pythonic way and building it all the way up to iterators and generators.
In chapter 2.5, Dan discusses the different ways to do string formatting in Python. String formatting is one of those things that defy the Zen of Python, that there should only be one obvious way to do things. Dan shows us the different ways, including my favorite new addition to the language, the f-strings, and he also explains the pros and cons of each method.
The Pythonic Productivity Techniques section is another great resource. It covers aspects beyond the Python programming language, and also includes tips on how to debug your programs, how to manage the dependencies, and gives you a peek inside Python bytecode.
It truly is an honor and my pleasure to introduce this book, Python Tricks, by my friend, Dan Bader.
By contributing to Python as a CPython core developer, I get connected to many members of the community. In my journey, I found mentors, allies, and made many new friends. They remind me that Python is not just about the code, Python is a community.
Mastering Python programming isnt just about grasping the theoretical aspects of the language. Its just as much about understanding and adopting the conventions and best practices used by its community.
Dans book will help you on this journey. Im convinced that youll be more confident when writing Python programs after reading it.
Mariatta Wijaya, Python Core Developer (mariatta.ca)
Introduction
1.1 Whats a Python Trick?
Python Trick: A short Python code snippet meant as a teaching tool. A Python Trick either teaches an aspect of Python with a simple illustration, or it serves as a motivating example, enabling you to dig deeper and develop an intuitive understanding.
Python Tricks started out as a short series of code screenshots that I shared on Twitter for a week. To my surprise, they got rave responses and were shared and retweeted for days on end.
More and more developers started asking me for a way to get the whole series. Actually, I only had a few of these tricks lined up, spanning a variety of Python-related topics. There wasnt a master plan behind them. They were just a fun little Twitter experiment.
But from these inquiries I got the sense that my short-and-sweet code examples would be worth exploring as a teaching tool. Eventually I set out to create a few more Python Tricks and shared them in an email series. Within a few days, several hundred Python developers had signed up and I was just blown away by that response.
Over the following days and weeks, a steady stream of Python developers reached out to me. They thanked me for making a part of the language they were struggling to understand click for them. Hearing this feedback felt awesome. I thought these Python Tricks were just code screenshots, but so many developers were getting a lot of value out of them.