Hello again, world! As a teenage programmer and wannabe hacker in the late 1990s, I would pore over the latest issues of 2600: The Hacker Quarterly. One day, I finally summoned the courage to attend the magazine’s monthly meetup in my city and was in awe of how knowledgeable everyone else seemed. (Later, I’d realize that many of them had more confidence than actual knowledge.) I spent the entire meeting nodding along to what others were saying, trying to keep up with their conversations. I left that meetup determined to spend every waking hour studying computing, programming, and network security so I could join the discussions at the next month’s meetup.
At the next meetup, I continued to just nod and feel dumb compared to everyone else. So again I resolved to study and become “smart enough” to keep up. Month after month, I would increase my knowledge but always felt behind. I began to realize the enormity of the computing field and worried I would never know enough.
I knew more about programming than my high school friends but certainly not enough to get a job as a software developer. In the 1990s, Google, YouTube, and Wikipedia didn’t exist. But even if those resources were available, I wouldn’t have known how to use them; I wouldn’t have been sure what to study next. Instead, I learned how to write Hello, world! programs in different programming languages but still felt I wasn’t making real progress. I didn’t know how to move beyond the basics.
There’s so much more to software development than loops and functions. But once you’ve completed a beginner course or read an introductory programming book, your search for more guidance leads to yet another Hello, world! tutorial. Programmers often call this period the desert of despair: the time you spend wandering aimlessly through different learning materials, feeling like you’re not improving. You become too advanced for beginner materials but too inexperienced to tackle more complex topics.
Those in this desert experience a strong sense of impostor syndrome. You don’t feel like a “real” programmer or know how to craft code the way “real” programmers do. I wrote this book to address this audience. If you’ve learned the basics of Python, this book should help you become a more capable software developer and lose this sense of despair.
Who Should Read This Book and Why
This book targets those who have completed a basic Python tutorial and want to know more. The tutorial you learned from could have been my previous book, Automate the Boring Stuff with Python (No Starch Press, 2019), a book such as Python Crash Course (No Starch Press, 2019) by Eric Matthes, or an online course.
These tutorials might have hooked you on programming, but you still need more skills. If you feel like you’re not yet at the professional programmer level but don’t know how to get to that level, this is the book for you.
Or perhaps you were introduced to programming via another language besides Python and you want to jump right in to Python and its ecosystem of tools without retreading the same Hello, world! basics. If so, you don’t need to read hundreds of pages that explain basic syntax; instead, skimming the “Learn Python in Y Minutes” article at https://learnxinyminutes.com/docs/python/ or Eric Matthes’s “Python Crash Course—Cheat Sheet” page at https://ehmatthes.github.io/pcc/cheatsheets/README.html will suffice before you tackle this book.
About This Book
This book covers more than just deeper-level Python syntax. It also discusses using the command line and the command line tools that professional developers use, such as code formatters, linters, and version control. I explain what makes code readable and how you can write clean code. I’ve featured a few programming projects, so you can see these principles applied in actual software. Although this isn’t a computer science textbook, I also explain Big O algorithm analysis and object-oriented design.
No single book can transform a person into a professional software developer, but I’ve written this book to further your knowledge toward that end. I introduce several topics that you might only otherwise discover, piecemeal, through hard-earned experience. After completing this book, your footing will be on a firmer foundation so you’ll be better equipped to take on new challenges.
Although I recommend you read the chapters in this book in order, feel free to skip to whichever chapters capture your interest:
Part I: Getting Started
Chapter 1: Dealing with Errors and Asking for Help Shows you how to effectively ask questions and find answers on your own. It also teaches you how to read error messages and the etiquette for asking for help online.
Chapter 2: Environment Setup and the Command Line Explains how to navigate the command line along with setting up your development environment and the PATH environment variable.
Part II: Best Practices, Tools, and Techniques
Chapter 3: Code Formatting with Black Describes the PEP 8 style guide and how to format your code to make it more readable. You’ll learn how to automate this process using the Black code-formatting tool.
Chapter 4: Choosing Understandable Names Describes how you should name your variables and functions to improve code readability.
Chapter 5: Finding Code Smells Lists several potential red flags that could indicate the existence of bugs in your code.
Chapter 6: Writing Pythonic Code Details several ways to write idiomatic Python code and what makes for Pythonic code.
Chapter 7: Programming Jargon Explains technical terms used in the programming field and terms that are commonly confused with each other.
Chapter 8: Common Python Gotchas Covers common sources of confusion and bugs in the Python language and how to correct them, as well as coding strategies to avoid.
Chapter 9: Esoteric Python Oddities Covers several odd quirks of the Python language, such as string interning and the antigravity Easter egg, that you might not otherwise notice. You’ll get an advanced understanding of how Python works by figuring out why some data types and operators result in such unexpected behavior.
Chapter 10: Writing Effective Functions Details how to structure your functions for the most utility and readability. You’ll learn about the * and ** argument syntax, the trade-offs between large and small functions, and functional programming techniques, such as lambda functions.
Chapter 11: Comments, Docstrings, and Type Hints Covers the importance of the non-code parts of your program and how they affect maintainability. It includes how often you should write comments and docstrings, and how to make them informative. The chapter also discusses type hints and how to use static analyzers, such as Mypy, to detect bugs.
Chapter 12: Organizing Your Code Projects with Git Describes using the Git version control tool to record the history of changes you make to your source code and recover previous versions of your work or track down when a bug first appeared. It also touches on how to structure your code projects’ files using the Cookiecutter tool.
Chapter 13: Measuring Performance and Big O Algorithm Analysis Explains how to objectively measure your code’s speed using the timeit and cProfile modules. In addition, it covers Big O algorithm analysis and how it lets you predict the way your code’s performance slows down as the amount of data it has to process grows.
Chapter 14: Practice Projects Has you apply the techniques you learned in this part by writing two command line games: the Tower of Hanoi, a puzzle game involving moving disks from one tower to the next, and the classic Four-in-a-Row board game for two players.
Part III: Object-Oriented Python
Chapter 15: Object-Oriented Programming and Classes Defines the role of object-oriented programming (OOP) because it’s often misunderstood. Many developers overuse OOP techniques in their code because they believe it’s what everyone else does, but this leads to complicated source code. This chapter teaches you how to write classes, but more important, it teaches you why you should and shouldn’t use them.
Chapter 16: Object-Oriented Programming and Inheritance Explains class inheritance and its utility for code reuse.
Chapter 17: Pythonic OOP: Properties and Dunder Methods Covers the Python-specific features in object-oriented design, such as properties, dunder methods, and operator overloading.
Your Programming Journey
The journey from novice to capable programmer can often feel like attempting to drink from a fire hose. With so many resources to choose from, you might worry that you’re wasting time on suboptimal programming guides.
After you finish reading this book (or even while you’re reading this book), I recommend following up by reading these additional introductory materials:
Python Crash Course (No Starch Press, 2019) by Eric Matthes is a book for beginners, but its project-based approach gives even experienced programmers a taste of Python’s Pygame, matplotlib, and Django libraries.
Impractical Python Projects (No Starch Press, 2018) by Lee Vaughan provides a project-based approach to expand your Python skills. The programs you’ll create by following the instructions in this book are fun and great programming practice.
Serious Python (No Starch Press, 2018) by Julien Danjou describes the steps you need to take to progress from a garage project hobbyist to a knowledgeable software developer who follows industry best practices and writes code that can scale.
But the technical aspects of Python are only one of its strengths. The programming language has attracted a diverse community responsible for creating a friendly, accessible body of documentation and support that no other programming ecosystem has matched. The annual PyCon conference, along with the many regional PyCons, hosts a wide variety of talks for all experience levels. The PyCon organizers make these talks available online for free at https://pyvideo.org/. The Tags page lets you easily find talks on topics that correspond to your interests.
To take a deeper dive into the advanced features of Python’s syntax and standard library, I recommend reading the following titles:
Effective Python (Addison-Wesley Professional, 2019) by Brett Slatkin is an impressive collection of Pythonic best practices and language features.
Python Cookbook (O’Reilly Media, 2013) by David Beazley and Brian K. Jones offers an extensive list of code snippets to upgrade any Python novice’s repertoire.
Fluent Python (O’Reilly Media, 2021) by Luciano Ramalho is a masterwork for exploring the intricacies of the Python language, and although its near-800-page size might be intimidating, it’s well worth the effort.
Good luck on your programming journey. Let’s get started!