PROJECT 1
In this project, you’re introduced to Python: where it’s used and what it’s used for. I explain the two current versions of Python. This book is focused on Python 2.7 and I explain why. With my help during this chapter, you install a copy of Python 2.7 (if you don’t already have it installed) and fire it up. I also tell you how to stop once you’ve started.
This project also shows you how to get Python’s documentation, both built in to Python and online. I give you ways to search online for answers to your Python problems, just in case you’ve never searched the Internet before. You also read about the Python community, which is one place you can go for help or new ideas. All that, but no actual programming? No worries. Actual programming starts in Project 2.
TL;DR: If you’ve already installed Python, and you can start and stop it, then skip to Project 2.
Python is a programming language written by a person called Guido van Rossum in the 1990s. Programming languages allow you to control what a computer does and the way it does it.
Some of the things that make Python totes awesome (also known as “really helpful and lots of fun”) are:
dummies.com/go/pythonforkids
for you.) You can even run Python programs on Android and iOS tablets. I even used my Android tablet to code some of the early projects in this book.Python is used just about everywhere.
I could go on. The point is that Python will apply to whatever you’re interested in, no matter what it is.
You do these things while you work through this book:
www.dummies.com/go/pythonforkids
for that project.)When you’ve honed your mad skills and are ready to move on, there’ll be other things you can do:
Whatever you want it to do, there’s a good chance someone has already written code to do it or to help you do it yourself.
That title is just to impress your parents. (I hope they’re not reading this part. But look: If they don’t see that title, tell them that this book has a pedagogical approach — ped-uh-goj-i-cul. It means education or teaching.)
The point of this book is to give you a chunk of information about the programming concepts that you need to program in Python. The book is for you — a kid who can learn Python.
Thorough, yes. Will I include everything? No way. Many aspects of Python have lots of options. If I took you through all the possibilities of each option, you’d fall asleep (or throw this book out the window). If you do either of those things, then you won’t be learning.
As you read, remember that I’ve tried to introduce you to enough information so you can be a Python programmer, but not so much that you’d need superhuman powers to get through it. Expand on your own using the documentation and help.
You start pretty slowly with core (main) principles. If you think things aren’t going fast enough, skip ahead! The examples are generally self contained. This means that you end up with many smaller projects rather than a few larger projects. I did that on purpose so you can do the projects in any order you like. You’ve got enough people telling you what to do. You can go where you want to in this book.
The earlier projects use plain English, rather than technical words. As you go through the book, you’ll see more jargon. You’ll also get less hand holding. You’ll have to work harder the further you get through the book.
The projects try to show you realistic programming without boring you to tears. When you write your own code to solve your own problems, you’ll need methods and approaches (tools) that get the job done. I teach you these tools by walking you through each project, step by step. Try every step — don’t skip any.
If you want to run the working program, skip to the end of the project and cut and paste. If you want to learn Python, then consider each project a journey, not a destination. Work through the projects with me and type them in yourself.
Hopefully, you can use these projects for something in your everyday life. Maybe they’ll help with your homework or let you store private notes. I start small and dream big. Please dream big with me while you’re getting the concepts in the first projects.
To find and start Python on Mac OSX computers, follow these steps:
Type the word terminal.
Or, from the Finder, select Finder ⇒ Go ⇒ Utilities ⇒ Terminal.
The Terminal window opens.
In the terminal, type python.
The Python interpreter that’s built in to Mac OSX opens.
Unfortunately, Python doesn’t come on Windows. If you’re running Windows, then you need to download and install Python by following the instructions here. Installing Python on Windows isn’t difficult. If you can download a file from a website, you have the skills to install Python.
Fortunately, the Python Foundation (the peeps who guide the development of Python) makes installable files available from its website.
When I did the installation, I found that Firefox and Internet Explorer responded differently to the Python download website, so the instructions are based on which of these browsers to use. If you use a whole other browser altogether, try the Internet Explorer instructions.
To install Python on a Windows machine with Firefox, follow these steps:
www.python.org/downloads
.Click the button that says Download Python 2.7.9.
Or, if it’s there, click a more recent version number that starts with 2.7.
Clicking this button automatically downloads and saves an msi file for you. If not, try the instructions for Internet Explorer. See Figure 1-1.
Click the file called python-2.7.9.msi (or the more recent version, if you downloaded one).
Python 2.7.9 installs on your computer.
To install Python on a Windows machine with Internet Explorer, follow these steps:
www.python.org/downloads
.From the menu bar, select Downloads ⇒ Windows.
You can see the menu options in Figure 1-2.
Scroll down to the heading Python 2.7.9-2014-12-10.
Or scroll to a more recent version, which starts with Python 2.7, if one is available.
Under this heading, click the link titled Download Windows x86 MSI Installer.
See Figure 1-3. This is a link for a 32-bit installation, which makes things work better with third-party libraries. Use the 32-bit installer even if you have a 64-bit machine and even if you have no idea what this paragraph is talking about.
If you’re asked to choose whether to run or save the file, choose Run.
This downloads python2.7.9.msi and starts running the installer.
If you’re running Linux, confirm that you have version 2.7.9 of Python installed, rather than version 3. This shouldn’t be a problem because Python 2.7 is installed by default in recent versions of OpenSuSE, Ubuntu, and Red Hat Fedora.
In the nutty odd case when someone has Python 3 but not Python 2.7, read your distribution’s documentation for how to use the package manager and get Python 2.7 and IDLE.
After you’ve downloaded Python, it’s a good idea to pin it to your Start menu. That way you can find it more easily for the rest of this book.
Type Python in the Start menu’s search bar, or click All Programs. In the folder Python 2.7, you should find the following entries (see Figure 1-4):
Of these, you’ll use:
To make IDLE and the command line easier to find, pin them to your Start menu:
Select Pin to Start Menu.
You should see the entries at the top of your Start menu. If you prefer, you can pin them to your taskbar.
You can’t use this book unless you can start Python. Click Python (command line) that you pinned to your Start menu.
You get a window that looks like Figure 1-6.
Python comes with its own help. In fact, in Figure 1-6 it even tells you about it in the welcome message. If you type help and press Enter, you get more help options. Type help() (including the parentheses) to get interactive help like what you see in Figure 1-7.
You can write help text for your own programs. You do it in Project 5.
You can’t use this book unless you can start Python, but you’ll want to stop Python after you’ve started it. If you can’t wait to start programming, skip ahead to Project 2.
You can put the kibosh on Python from the command line by doing one of the following:
As I mention, this book doesn’t cover every single possible potential thing you could do with Python. I just can’t do that in a single book.
Instead, this book exposes you to programming in Python. Sort of like the measles. Use this book as a starting point. If you want more information, you should check out
The Python documentation pages are available at https://docs.python.org/2.7/
. The most helpful sections are listed here:
https://docs.python.org/2.7/reference/index.html
https://docs.python.org/2.7/library/index.html
The documentation shows the feature you looked for and a code template for how to use the feature. (See this book’s Introduction for how to read code templates.) These docs have a lot of information, but they assume you know how programmers write documentation. This often makes them hard to understand.
Reading the Python documentation is a skill that you have to master if you want to become a Python master. At the moment, though, don’t worry that it seems like it’s written in a foreign language. Just work through it slowly. As you read more of it, you’ll be able to get more information from it. Soon, docs you couldn’t understand at all will become mind-numbingly boring. Then you’ll know you’re making progress!
The second form of help is Python’s introspection features. Introspection means that the program can tell you about itself. Introspection has a lot of different parts. You’ve already met one of them — the help feature. To understand the others you need to know more about Python, so I introduce the rest as you work your way through the book.
What I like to call “Professor Internet” is the third form of help. When you search the Internet, make sure you include Python as one of your search terms, followed by the thing you’re wondering about. If you know that it’s part of or related to something else, then include that other thing as a search term as well. For example, don’t search for print, because that’s too broad. Search instead for python print. (Capitalization won’t matter.) Another example is if you were interested in the Button widget of the Tkinter toolset (which you meet at dummies.com/go/pythonforkids
) you’d search for python button tkinter.
If ads are in your results, skip them.
Source code is the final form of help. Keep in mind that Python is an interpreted language. This means that what you read is what the computer executes. As a result, if you want to know how some third-party module does something (the Python core isn’t written in Python, so that won’t help), you can literally look at it and see exactly how it did it. You won’t understand it at first, but no matter. Dive in. It starts making sense after a while.
Check out the Python community that’s out there. Look for Internet forums related to Python. The Stackoverflow website (http://stackoverflow.com/questions/tagged/python
) is helpful when I’m figuring out problems.
Often, an Internet search will turn up questions that someone else has already asked. If you know the answer to a question, answer it. If you’re correct, and thorough, you’ll get a good reputation.
Python changes, or evolves, when you use proposal documents called Python Enhancement Proposals (known as PEPs). Each PEP proposes some change to Python; the change is either used or ignored. They may provide you some history about how one feature or another was added to Python.
You can ignore most PEPs, but do check out the following two when you’re ready to move on from the projects in this book:
Planet Python (http://planetpython.org
) puts together lots of blogs that are Python related. Many of the posts may be hard to understand as a beginner, but like everything else, stick with it — they’ll become clear. A lot of top-notch Python programmers have their blogs on Planet Python. You can learn heaps from keeping an eye on them. I have.
Python programmers regularly get together at conferences around the world. These conferences (confs) are called PyCon, and their location is added to the name. For example, the one in Australia is called PyCon-AU. See www.pycon.org
for a list of locations.
I’m not suggesting you hop a plane to Australia to attend PyCon-AU (or even attend one that’s close to you). However, I am suggesting you hop online and check out the videos from these confs. The videos from various PyCons appear over time at http://pyvideo.org
. Go there, browse, find something that looks interesting, and watch. It’s a great way to get a quick introduction (or an in-depth look) at something new.
There are two main kinds of errors:
A syntax error basically means you made a punctuation mistake. These are the most common errors. Get used to reading what you’ve typed very closely and comparing it carefully to what you should have typed.
If you have trouble getting one of the code examples to run properly, the first thing to do is make sure that you’ve retyped the code exactly as it’s written.
When Python runs into an error, it tries to give you a clue about what went wrong and where. Try to figure out Python’s message. It will usually give you a good place to start.
Sometimes you get an error because you’ve assumed something that is incorrect. If you can’t see what’s wrong, think about the assumptions you’ve made. If you’re not making any progress, do something else and reset your brain. Go drink some water, feed Fluffy, or stand up and walk around. Do something that gets your mind on something else, but doesn’t steal your brain. (Hopping onto Instagram isn’t a good idea, for example.) When you’ve finished that, come back to your Python problem. Fresh eyes and a fresh mind will help you see the problem differently and hopefully lead to a solution.
If all else fails, bring Fluffy (or your dog, or goldfish, or your pet rock) nearby when you’re coding. If you come to a problem you can’t solve, stop what you’re doing and explain the problem to your pet or object.
You need to express your problem using words because this causes a different part of your brain to kick in and you think differently about the problem. If you’re like me and you’re not the talking type, keep a programming journal. The concept is still the same — write out an explanation of the problem in your journal and why you can’t crack it. I promise you this will help you solve many difficult problems.
Reading books isn’t enough. You have to do. Be the book, feel the book, become one with …. (Scratch that last bit. I was channeling a movie there for a second.)
Seriously. You need to actually do things if you want to learn. No one ever learned anything by simply reading. It’s true of Python, and it’s true of anything else you want to learn.
Do the code as you read the book. Don’t just cut and paste. Type the whole thing in again yourself, at least for the early projects. That way, you read the code that’s been written and somewhat understand why it’s been written that way. Copying and pasting won’t get you close to the code. Go on. Snuggle up. Freestyle it if you want to add your own changes. You learn faster if you add your own stuff. Own the code!
If you do, you’re gonna whoops. If you’re freestyling, don’t worry about breaking your code or getting things wrong. (Use Save As to make backups.)
Everyone who writes code gets it wrong the first time. Their logic is wrong; they misspell something. Whatever.
This is totally normal. In fact, writing programs is a process, and it has wrong turns and setbacks. Professional programmers make mistakes every day. That doesn’t bother them because they code in small pieces that they can handle, and they test what they do.
When you get something wrong, or your code doesn’t work like you expected, take the time to understand why. If you think long and hard enough, you’ll work it out.
Getting things wrong isn’t enough to teach you. It’s part of a path to thinking and understanding. That’s where the learning comes from. See how it all builds on itself? Sort of like Python.
The Japanese have a most excellent word — ganbatte. It means a whole heap of different things like cheer up, good luck, do your best, be strong, keep at it, keep your chin up, be courageous, don’t give up, and you can do it. When you start learning a programming language, you have big dreams but small skills. It can feel even tougher because the examples of computer programs you see were made by teams of people working for months on end. Plus, a lot of what makes programs seem special these days is graphic artists, and that’s a whole new ballgame!
Hang in there! Your effort will be rewarded.
In this project you: