If you've picked up this book, then you are taking your first step toward building amazing projects using code. Some of you might want to make games, while others might want to learn more about how all of your favorite websites and apps actually work. If you follow the exercises in this book, you'll be able to do the following:
Create fun games to play with your family and friends
Learn about the inner workings of your apps
Learn how to take charge of your computer
In this book, you will learn Python code. Specifically, you will learn how to design a computer program from the very beginning. It doesn't matter if you have never coded before because each exercise in this book is designed to get you ready to code. If you have coded before, you will find that this book has some really helpful exercises that can help make your code even better. Additionally, there are some more advanced projects toward the end of the book, which you should definitely take a look at!
It can be used in order to go through millions of lines of data
Python can search for information on a website without having to go to the website itself
It is even used to host and design websites
So, what will it take to learn Python? If you have never programmed, you will probably want to follow each lesson in order so that you can build the skills you need to make a game or another kind of computer program. The final project in this book will be a game. If you have some other programming experience, such as making modifications to your computer games, using programs such as Scratch or Logo or trying some of the free programming classes on the Internet, then you might decide to skim this book first to see what you already know. It is still recommended that you follow the contents of this book in the order they are presented, as each project builds on the projects that were explained in the previous chapter.
Python teaches all of the basics of an object-oriented programming language, and it is still very powerful. In fact, many Internet companies, most notably Mozilla Firefox and Google, use Python in part or all of their products! Python has also been used to build Django, a free framework to make websites.
It has also been used to build many small video games by people learning about it as well as more advanced programmers. Finally, Python can be used to quickly read and analyze millions of lines of data very quickly! By learning Python, you will be prepared to build a variety of interesting projects, and you will gain the skills necessary to learn other programming languages if you choose to do so.
A computer that can run Windows 7 or higher, Mac OS X 10.6 or higher, or Ubuntu 12.4 or higher. You may also use a Raspberry Pi as it comes preinstalled with Python, pygame, and the other software needed to complete the projects in this book.
An Internet connection is necessary as some of the software you need to install on your computer might not be installed already. For example, Windows operating systems do not come with Python preinstalled, so an Internet connection will be needed; pygame is also not preinstalled on Windows, Mac, or Linux systems.
Along with an Internet connection, you will also need a web browser, such as Firefox, Safari, Chrome, or Internet Explorer, which will allow you to visit the Python documentation pages.
All of the code samples in this book are available for download on the Packt Publishing website.
There are many different computer operating systems, but the most common operating systems are Macintosh (Mac), Windows, and Linux. You should follow the installation steps that go with your operating system. There are some subtle but important differences between the systems.
For the projects in this book, we will be using Python 2.7. While there are higher versions than this (3.x), these versions do not work dependably with pygame on Windows, Mac, or Ubuntu Linux as yet. However, this book will be written to use conventions that work in both versions of Python so that projects are easily completed on Raspberry Pi (which uses Python 3.x that's been specially configured with pygame) with just a few modifications. These modifications will be duly noted.
Mac and Linux systems share enough similarities that people who use either Mac or Linux can follow the same set of instructions. These instructions will make note of any differences between Mac and Ubuntu Linux.
Mac and Ubuntu Linux users have Python by default, but finding Python is tricky if you don't know where to look. There is a program called Terminal on both Mac and Linux operating systems. This program allows you to exercise a lot of control over your computer in these ways:
On a Mac, go to Finder | Applications | Utilities and click on Terminal. The terminal application will open up, and you should see a small, white window on your screen.
Ubuntu users can search for
terminalon their desktops, and the program will show up in their Start menu. When you click on the terminal, you will see a small, black window on your screen.
A text editor is a helpful tool for writing and editing Python programs. The terminal is a nice place to test snippets of Python code, but when we want to edit and save the code in order to use it over again, we will need a text editor. Although both Mac and Linux systems come with a text editor, there are some very nice, free editors that have good features. jEdit is one of these editors.
For Mac and Linux, go to http://www.jedit.org/ and download jEdit. Follow the installation instructions.
To successfully complete all of the exercises in this book, you will often need to keep both the terminal and text editor open at the same time on your screen.
Windows users, this setup might require help from your parents. Since Python is not installed by default on Windows, some system adjustments need to be made to successfully run Python on your computer. If you are feeling uncertain about performing these system changes yourself, make sure to ask for help:
First, you will need to download version 2.7.11 of Python.
Use the official Python website for the latest releases for Windows at https://www.python.org/downloads/release/python-2711/.
Download x86only if your computer is running 32-bit Windows. Most users will download the x86-64 version of Python.
Choose the executable installer, and you will see the download progress.
An install prompt will come up, and when it does, look at the bottom of the window and click on the box next to Add Python 2.x to Path. Then, select Install Now.
Follow the installation instructions. Each step may take a few minutes. Once the installation is done, you will have an icon for Python 2.7.11, which you can find by searching for
Pythonin the Windows search bar. This will open a special Python shell from where you can run and test the Python code.
Go to the search bar at the bottom of the screen and search for
When you do, you will see the command prompt desktop app appear. Click on this app to open the command prompt, which looks like this:
In Windows, Notepad is the default text editor. However, Notepad++ is a much better substitute.
Go to https://notepad-plus-plus.org/and download the latest version.
Once the program has been downloaded, click on Run.
Notepad++ looks like this:
Now that you are set up, it is time to write your first line of code in Python! This line of code is almost a tradition for people who are programming for the first time, and it allows us to use one of the most basic, but most useful, functions in the Python language.
First, you need to start running a Python shell. On Mac or Linux, open your terminal and type this:
In the Mac or Ubuntu terminal, your resulting Python shell will look like this:
In Windows, type
Python in the search bar at the bottom of the page. Then, select Python 2.7.11 from your apps. You will also have a Python shell open:
Once you have typed this, double-check to make sure that all of the spaces are exactly as they've been written. In Python, every space actually matters. Every punctuation mark matters. Once you have checked your code, hit Enter.
What is your result or the output of your code? If the output looks like the following image, then great! You typed all of your code properly so the computer will understand what you want it to do. The expected output will be similar to what is shown here:
For Windows users, the output window will look like this:
If you still have a problem, compare your code to the sample input code and fix any mistakes. Then, try to run the code again.
Before we get started on any large projects, we need to make a work folder. In the next chapter, you will start writing whole files of code that need to be run; therefore, we will need a place to put those files. Since you are setting up now, let's make a folder.
If you are very good at getting around your computer, you can put your folder wherever you want to.
If you are not too good at getting around your computer, you will probably want to put your folder on your desktop.
On both Mac and Windows machines, you can right-click somewhere on your desktop wallpaper, and a box will pop up with several options. One of the options will say New, and when you hover over New, you will get several other options. Choose New Folder, and a new folder icon will appear on your desktop. It will be named
untitled, so you should give it a better name.
To find your folder in the Mac or Linux terminals, respectively, open one of them and perform these steps:
cd ..command until you are at the root, which is often the name you have given to your computer. You may need to run the command three or four times.
Now, you will be able to run python programs by typing
cd ..command until you are at the root or
C:\>. You may need to run the command three or four times.
Now, you will be able to run Python programs by typing
Now that you have finished this chapter, can you answer these questions?
Q1. What is a terminal (Mac/Linux) or command prompt (Windows)?
A terminal is used to put data into or get data out of a computer without using the icons on the desktop.
A terminal can be used to write computer programs.
A terminal can be used to do complex work, such as giving hints on Python code.
A terminal can do all of the above.
Q2. When you first open the terminal/command prompt, what do you need to do so that you can start reading and writing the Python code?
Start typing the code.
Type the word
Wait for Python to start.
None of the above; do something different.
Q3. How is the Python shell different from the command line?
They are exactly the same.
The command line cannot run Python commands.
The Python shell is started by typing the word
pythoninto the command line.
The Python shell can be used to test lines of Python code.
If you are reading this, it is because you have made it through some of the tricky work of getting ready to learn to program projects with Python. Congratulations! Setting it up is always tough. Hopefully, you learned a bit more about the tools on your computer, such as the text editor and terminal that every programmer uses to do their daily work. Also, you learned about the Python
print() function, and you should now be able to print out messages in your Python shell. The fun is just beginning as we have so much more to learn!
In the next chapter, you will learn about the building blocks of Python programs. We'll start with variables and learn about all the different kinds of information we can put in them. Then, we will build some functions that put these variables together and help us make blocks of code that have special jobs. Finally, we will even learn how to make a computer ask a user questions and store their answers so that our programs can become interactive!