Computer Programming and Python
- Understanding Computer Programming
- Setting Things Up
- Writing Your First Python Program
Captain Code is a welcoming, engaging, and fun introduction to becoming a coder. This excerpt introduces what computer programming is and provides the steps for installing Python.
Hello, and welcome. We love coding, and by the time you’re done with this book we think you will, too. We’re going to help you learn to code the way that we learned, by doing—no long lectures, no reading lots of instructions, and no drawn-out explanations. In every chapter you’ll get to do stuff, and you’ll learn while doing.
Except for this chapter, sorry. :-( Before you start developing your coding superpowers it’s really important to understand what coding actually is, so we’ll spend just a few minutes reviewing that. And then we’ll help you install the software you’ll need to get started. But, after that, it’s all doing. Promise.
Understanding Computer Programming
Let’s start by taking a few minutes to understand what computer programming actually is, and to do that we need to talk about computers.
What is a computer?
You’ve seen computers, and they are all basically the same; they have screens, a keyboard, mouse or touchpad, and some fold in half, like a notebook. Right? No, wrong! The truth is that most of the computers that you’ve seen and used look nothing at all like that. Really.
For example, your gaming consoles are all computers. So are smartphones, smartwatches, and smart TVs. (Actually, anything with “smart” in the name is pretty much guaranteed to be a computer.) Those video doorbells that show you who’s at the door? Those are computers. Vacuuming robots, fancy touchscreen thermostats, and display consoles in cars are all computers, too. Drones are computers with propellers, and Tesla cars are computers with seats and wheels. Those cool rovers NASA sent to Mars are computers. ATMs at the bank that you can use to withdraw cash from an account? Those are computers, as are self-checkout machines at your local grocery store. You get the idea. There are lots and lots of computers out there, and most look nothing like what we typically call a computer.
So, what makes all of these devices computers? They are all computers because inside of them is a microprocessor, a computer chip that functions as the device’s brain. The microprocessor controls everything the device does. The display, motors, inputs, sensors, speakers, and more are all controlled by one or more microprocessors.
So now that you know what computers are, answer these questions: Are computers smart? Is your gaming console actually smart? What about your smartphone or tablet? What do you think?
Well, sorry to be the bearer of bad news, but the answer is no, not at all. Computers are not very smart. In fact, quite the opposite, despite the word “smart” in some of their names, computers are actually rather dumb and useless.
Why are computers not smart? Because as powerful as they are, they themselves don’t know how to do anything. They can’t display videos on a screen, they can’t respond to mouse clicks or joystick controls, they can’t connect to the Internet, they can’t understand what you type, they can’t play your favorite games or let you video conference. By themselves computers can’t do anything useful at all…So, yep, not very smart.
But computers do indeed do all sorts of wonderful things, so how do they know how to do them? They can do those things because someone taught the computers how to do them. Someone gave the computers very specific instructions teaching them how to do all things we expect our favorite devices to do. And those instructions are indeed pretty smart.
But the real smarts belong to whomever actually created the instructions. So who does that? Who gets to teach all of the computers around us how to do fun and useful things? That’s the job of computer programmers, and computer programming is just that, teaching computers how to do stuff.
How do we talk to computers?
When you talk to your friends, they understand what you are saying (well, hopefully). The reason that they understand you is that you are communicating in the same language, one you both understand. This is obviously important. If you spoke to someone who only understood another language, you’d not really be communicating.
Communicating with a computer is much the same. If you are going to give a computer instructions, you need to provide those instructions in a language that the computer understands. You are going to use a computer programming language, and much like spoken languages, programming languages have words and rules for how they are used.
There are lots and lots of different computer languages. Some have very specific uses, some are more general purpose. And most programmers learn and use multiple languages; that way they can pick the best one for any particular situation. If the idea of having to learn lots of languages sounds scary, don’t worry, we have some good news for you:
Exact language details, the words you use and how you use them (programmers call this syntax) differ from one language to the next. But unlike spoken languages, computer programming languages tend to have very few words and rules, so you’ll master them pretty quickly.
In addition, pretty much all programming languages do the same basic things (and we’ll cover all of those in this book). This means that once you master one programming language, learning the next one is much easier.
You never need to memorize a programming language. If you ever need a reminder for how to perform a specific task in a specific language, do what professional programmers do: Google it.
There is one important difference between spoken languages and programming languages, and that is the listener. When you message a friend, you can misspell words (don’t), omit punctuation (don’t do that, either), even send incomplete sentences (ugh, no, please), and your friend will still understand you. Computers are less forgiving (not smart, remember?) and if you miss a . or a }, they’ll get upset and not know what you want them to do. This is probably the single biggest source of frustration for beginning coders. So why did we point this out among this list of good news? Because editors (the tools you use to write your code…more on that soon) are really good at catching those typos, and that makes things a whole lot less frustrating.
Now that you know what a programming language is, and why they are similar to spoken languages, we are going to let you in on a secret; well, two secrets, actually. We are going to tell you exactly what computer programming is and what programmers do:
Imagine that you knew every word in your spoken language. You studied your dictionary backward and forward and mastered every single word, including pronunciations and definitions. Would that make you a best-selling author? Would knowing all the words mean that you could write the next blockbuster movie script? Of course not. Knowing the words is one thing. Knowing how to use those words by creatively combining them in any of infinite combinations, well, that’s obviously very different. Computer languages are much the same. Knowing the syntax is easy (especially as computer languages have far fewer words than spoken languages). What makes an experienced and skilled programmer is knowing how to use those language elements to artfully solve problems, and that’s exactly what we’re going to help you learn. And that is a skill that takes time and practice.
Once more, think about spoken languages. Is there a single right way to share an idea using words? No, of course not. If that were the case, all movies and books would be the same, and that would be horrible! A language is a tool, and authors get to use that tool to craft all sorts of wonderful and unique experiences. Using a programming language is much the same. There is no single right way to write code or solve any specific problem. Rather, there are as many solutions as there are coders. We’ll show you lots of techniques and solutions in this book, and you are free to use them in your own coding. But, over time, you’ll find your own creative ways to solve problems, just like professional coders do, because coding is all about creative problem solving.
What is Python?
All of this brings us to Python, which is <make a drumroll sound here please> … a programming language.
Yes, Python is a language, and it is used to provide instructions to a computer. Python is not a new programming language, it’s actually been around for 30 years or so. But it is super popular, and even powers some of your favorite sites and apps. Why is Python so popular?
Python is really easy to use, no complicated tools or setups, just write a few lines of code and off you go. Really! In fact, you’ll have written code that works before you finish this chapter!
Python’s creators worked really hard to create a language whose syntax is really easy to read. Reading Python code is a lot like reading English. Most other programming languages are far more complicated to read. (Trust us on that one.)
Those syntax rules we mentioned before, the ones that frustrate most beginning coders? Python’s syntax is one of the easiest of any programming language, and we all like fewer rules, right?
Simple to use and having less rigid rules are great, but what really makes Python so much fun to work with is all of the libraries it makes available. We’ll discuss libraries later in the book, but for now, all you need to know is that libraries are sets of code that other programmers have written for you; just download them and use them. So, tasks that might be complicated (for example, embedding Google maps and directions in an app or detecting when a car hits an obstacle in a game) are so much simpler. You don’t have to start from scratch—you get to build on top of what other smart coders have kindly shared.
So we are going to learn Python together. But, as we explained before, what we’ll learn are the concepts and techniques that are useful in all languages. Once you’re done with this book, you will be able to apply your hard-earned knowledge and expertise to whatever language you decide to use next.