Every programmer has a story about how they learned to write their first program. I started programming as a child, when my father was working for Digital Equipment Corporation, one of the pioneering companies of the modern computing era. I wrote my first program on a kit computer that my dad had assembled in our basement. The computer consisted of nothing more than a bare motherboard connected to a keyboard without a case, and its monitor was a bare cathode ray tube. My initial program was a simple number guessing game, which looked something like this:
I'm thinking of a number! Try to guess the number I'm thinking of: 25
Too low! Guess again: 50
Too high! Guess again: 42
That's it! Would you like to play again? (yes/no) no
Thanks for playing!
I’ll always remember how satisfied I felt, watching my family play a game that I created and that worked as I intended it to.
That early experience had a lasting impact. There’s real satisfaction in building something with a purpose, that solves a problem. The software I write now meets more significant needs than my childhood efforts did, but the sense of satisfaction I get from creating a program that works is still largely the same.
The goal of this book is to bring you up to speed with Python as quickly as possible so you can build programs that work—games, data visualizations, and web applications—while developing a foundation in programming that will serve you well for the rest of your life. Python Crash Course is written for people of any age who have never programmed in Python or have never programmed at all. This book is for those who want to learn the basics of programming quickly so they can focus on interesting projects, and those who like to test their understanding of new concepts by solving meaningful problems. Python Crash Course is also perfect for teachers at all levels who want to offer their students a project-based introduction to programming. If you’re taking a college class and want a friendlier introduction to Python than the text you’ve been assigned, this book can make your class easier as well. If you’re looking to change careers, Python Crash Course can help you make the transition into a more satisfying career track. It has worked well for a wide variety of readers, with a broad range of goals.
The purpose of this book is to make you a good programmer in general and a good Python programmer in particular. You’ll learn efficiently and adopt good habits as you gain a solid foundation in general programming concepts. After working your way through Python Crash Course, you should be ready to move on to more advanced Python techniques, and your next programming language will be even easier to grasp.
In Part I of this book, you’ll learn basic programming concepts you need to know to write Python programs. These concepts are the same as those you’d learn when starting out in almost any programming language. You’ll learn about different kinds of data and the ways you can store data in your programs. You’ll build collections of data, such as lists and dictionaries, and you’ll work through those collections in efficient ways. You’ll learn to use while loops and if statements to test for certain conditions, so you can run specific sections of code while those conditions are true and run other sections when they’re not—a technique that helps you automate many processes.
You’ll learn to accept input from users to make your programs interactive, and to keep your programs running as long as the user wants. You’ll explore how to write functions that make parts of your program reusable, so you only have to write blocks of code that perform certain actions once, while using that code as many times as you need. You’ll then extend this concept to more complicated behavior with classes, making fairly simple programs respond to a variety of situations. You’ll learn to write programs that handle common errors gracefully. After working through each of these basic concepts, you’ll write a number of increasingly complex programs using what you’ve learned. Finally, you’ll take your first step toward intermediate programming by learning how to write tests for your code, so you can develop your programs further without worrying about introducing bugs. All the information in Part I will prepare you for taking on larger, more complex projects.
In Part II, you’ll apply what you learned in Part I to three projects. You can do any or all of these projects, in whichever order works best for you. In the first project, in Chapters 12–14, you’ll create a Space Invaders–style shooting game called Alien Invasion, which includes several increasingly difficult levels of game play. After you’ve completed this project, you should be well on your way to being able to develop your own 2D games. Even if you don’t aspire to become a game programmer, working through this project is an enjoyable way to tie together much of what you’ll learn in Part I.
The second project, in Chapters 15–17, introduces you to data visualization. Data scientists use a variety of visualization techniques to help make sense of the vast amount of information available to them. You’ll work with datasets that you generate through code, datasets that you download from online sources, and datasets your programs download automatically. After you’ve completed this project, you’ll be able to write programs that sift through large datasets and make visual representations of many different kinds of information.
In the third project, in Chapters 18–20, you’ll build a small web application called Learning Log. This project allows you to keep an organized journal of information you’ve learned about a specific topic. You’ll be able to keep separate logs for different topics and allow others to create an account and start their own journals. You’ll also learn how to deploy your project so anyone can access it online, from anywhere in the world.
No Starch Press has more information about this book available online at https://nostarch.com/python-crash-course-3rd-edition.
I also maintain an extensive set of supplementary resources at https://ehmatthes.github.io/pcc_3e. These resources include the following:
Setup instructions The setup instructions online are identical to what’s in the book, but they include active links you can click for all the different steps. If you’re having any setup issues, refer to this resource.
Updates Python, like all languages, is constantly evolving. I maintain a thorough set of updates, so if anything isn’t working, check here to see whether instructions have changed.
Solutions to exercises You should spend significant time on your own attempting the exercises in the “Try It Yourself” sections. However, if you’re stuck and can’t make any progress, solutions to most of the exercises are online.
Cheat sheets A full set of downloadable cheat sheets for quick reference to major concepts is also online.
Every year, I consider whether to continue using Python or move on to a different language, perhaps one that’s newer to the programming world. But I continue to focus on Python for many reasons. Python is an incredibly efficient language: your programs will do more in fewer lines of code than many other languages would require. Python’s syntax will also help you write “clean” code. Your code will be easier to read, easier to debug, and easier to extend and build upon, compared to other languages.
People use Python for many purposes: to make games, build web applications, solve business problems, and develop internal tools at all kinds of interesting companies. Python is also used heavily in scientific fields, for academic research and applied work.
One of the most important reasons I continue to use Python is because of the Python community, which includes an incredibly diverse and welcoming group of people. Community is essential to programmers because programming isn’t a solitary pursuit. Most of us, even the most experienced programmers, need to ask advice from others who have already solved similar problems. Having a well-connected and supportive community is critical to helping you solve problems, and the Python community is fully supportive of people who are learning Python as their first programming language or coming to Python with a background in other languages.
Python is a great language to learn, so let’s get started!