ARE YOU AN intrepid adventurer? Do you love trying new things and learning new skills? Would you like to bring your own ideas to life with technology? If the answer is a resounding “Yes!” then this is the book for you. Your adventures with Python start here! During your adventures you’ll learn a lot about programming with Python. You’ll create some really fun programs along the way, including games and animations.
Before you take your first step, take some time with this introduction to understand what you’ll be doing and answers some of the questions you might have.
First off, what is programming? Put simply, programming is giving the computer a set of instructions to do something you want. For example, when you play a computer game programs determine what the game will do when you press a button and what is displayed on the screen.
Just as programming is giving the computer a set of instructions, a program is a set of instructions that has been written by a programmer.
You write these instructions using a programming language. There are many different programming languages, which you can think of as similar to human languages like English or French. Each programming language has its own way of doing things but they all share the same fundamental concepts.
As a programmer, you have a lot of choice when choosing a programming language to use. So why choose Python? Well, Python is a good language to learn for beginners. It is designed so that it is relatively easy for humans to read. The layout and words it uses for its instructions make it easier to learn than other languages, such as Java or C++. Python is even used in the real world—in fact, it is one of the most popular programming languages used by professional programmers, who use it to do all sorts of things, from analysing scientific data to making games. You can see an example of some Python code in Figure I-1.
This introduction is intended to help you get set up with Python on your computer. It’s here to make sure you start your journey on the right foot and have everything you need to get going.
As I’ve mentioned, the Python programming language is an excellent programming language for beginners. Python is designed so that it is easy to read by people. The way that Python is laid out and the commands it uses all make it straightforward to read and understand.
You can do some amazing things with Python. Here are just some of the many things it is used for:
Adventures in Python is for any young person who has an interest in making things happen on a computer. You might perhaps be unsure of how to get started or want to develop your current skills further. Whatever your reasons, this book will be your guide for a journey with Python. Your adventures will take you from installing Python, through learning the basics of programming, to discovering how to create your own project. By the end of your adventures you will have acquired the skills you need to create whatever you want!
With this book you will discover some of the amazing things you can do with Python. You’ll learn about the fundamental programming concepts needed to design and make your own programs.
The Python projects in this book are arranged in adventures. All the projects in an adventure are grouped by a similar topic. For example, in Adventure 7 you’ll create several programs that show you how to use the mouse with your Python programs.
The programming concepts you’ll learn include Python code that enables your programs to make decisions, allows your programs to repeat complex tasks in a short amount of time and a whole lot more. By building on top of these concepts in your adventures you’ll also develop graphical user interfaces, animations and games.
Python is a programming language that is cross-platform. This means that it can run on computers with different operating systems. For example, you can write a program on a computer that runs Windows 7 and it will also work on an Apple Mac. There are a couple of slight differences in the way that Python works on different operating systems, but you won’t come across these in this book, and they aren’t that common.
Another benefit of Python being cross-platform is that you can use this book whether you use Windows, Mac or Linux—the programs will work just the same on all of them.
So, all you need for your adventures is a computer running Windows, Mac or Linux, an Internet connection, a mouse, keyboard and computer monitor. That’s it! You don’t need anything else.
This book contains Python programs that teach you the basics of the Python programming language and the different things it can do. As you’re led through some really fun and creative programs, you’ll learn how to use Python.
Adventure 1 introduces Python and shows you how to create programs that use text. You’ll create some cool programs, like a control panel for an imaginary spaceship. In Adventure 2, you’ll create drawings using Python’s built-in turtle
module. You’ll make pictures and build on the things you learned about Python in Adventure 1.
Adventures 3 and 4 introduce you to making programs that use graphical interfaces that include familiar things like buttons, text boxes and sliders.
From the Adventure 5 onwards, you’ll be introduced to the PyGame
library for Python. The PyGame
library makes it easier to do a wide range of things with Python, including making computer games of your own. You’ll find instructions on how to install PyGame
in the Appendix.
In Adventure 5, you’ll use some of PyGame
’s features to draw shapes and make animations, and in Adventure 6 you’ll find out how to use PyGame
to detect keyboard presses for input in your programs, allowing you to take control of what happens when the program runs. In Adventure 7, you’ll learn how to use the mouse with your programs.
Adventure 8 shows you how to use images in your PyGame
program and guides you through a series of fun programs, including controlling a character to walk across the window using the keyboard. In Adventure 9, you’ll learn how to use sounds and music with PyGame
so you can add music and sounds to your games.
Adventure 10 takes you on a final, big adventure in which you will combine many of the things you learned in your earlier adventures to create a game in which two players compete to collect coins. The game wraps up what you’ve learned–all in a single program. Through your adventures, you’ll learn a lot about programming.
Time to get going!
This section will show you how to set up Python on your computer. Choose the section that matches your operating system (OS) to get started.
Microsoft Windows 8 is an operating system that is widely used all over the world. At the time this book is being written, Windows 8 is the operating system that is on most new computers. If you’re using Windows 8 on your computer, follow these instructions to install Python.
Installing Python on Windows 8 is quite straightforward. Follow these steps to install it:
python.org/downloads
.Now that Python is installed, you can open it. IDLE is a program that allows you to run, make and edit Python programs. It includes lots of helpful features to make creating programs straightforward. Remember these steps to open Python with IDLE as you’ll need them throughout your adventures.
To open IDLE, follow these steps:
Despite not being the most recent version of Windows, Microsoft Windows 7 is still very widely used. All the screenshots in this book were taken on Windows 7. If your computer uses Windows 7, here’s how to install Python.
Follow these step-by-step instructions to install Python on Windows 7:
www.python.org/downloads
to go to the Downloads page on the Python website.Downloads
folder and double-click on the python-2.7.8.msi
file.IDLE is the name of a program that allows you to run, make and edit Python programs. It includes lots of helpful features to make creating programs straightforward. Remember these steps to open Python with IDLE as you’ll need them throughout your adventures:
Python 2.7
folder to show all the options for using Python. The list of programs should be in alphabetical order.The operating system that comes with Apple Mac computers is Mac OS X. There are many different versions of this operating system, and any version of Mac OS X after and including 10.6 will work with these installation instructions. To install Python on your Mac, follow the instructions in this section.
Follow these steps to install Python on your computer:
www.python.org/download
and press the Enter key to go to the Download page for Python.Python 2.7.8 Mac OS X 64-bit/32-bit x86-64/i386
file. Click on it to download it (see Figure 1-8). Be a bit careful here—there are several versions of Python listed, so make sure you click on the one that that says Python 2.7.8 and Mac.
Downloads
folder.python-2.7.8-macosx10.6.dmg
.Python.mpkg
and click on Open With⇒Installer (see Figure I-9).
IDLE is the name of a program that allows you to run, make and edit Python programs. It includes lots of helpful features to make creating programs straightforward. These steps show you how to open IDLE. Remember these steps, as you will be using them throughout the book:
Applications
folder on the sidebar to show all the applications installed on your computer.Operating systems that are based on Linux are free to install and use on your computer. There are many different distributions of Linux, each with its own unique flavour. For this set of instructions, I’ve chosen to show you how to install Python on the Ubuntu 14.04 distribution. These instructions should work on most other distributions, though you might find they don’t work on all of them.
Ubuntu 14.04 comes with Python 2.7 pre-installed. Despite this, you still need to install IDLE. IDLE is the name of a program that allows you to run, make and edit Python programs. It includes lots of helpful features to make creating programs straightforward. Follow these steps to install IDLE for Python 2.7:
apt-get update && sudo apt-get upgrade
sudo apt-get install idle-python2.7
The next steps show you how to open IDLE. Remember these steps, as you will use them throughout your adventures:
The Raspberry Pi is a small, low-cost computer that is great for teaching people how to program. Python was the main language chosen for the Raspberry Pi when it was first developed, and many of the Raspberry Pi’s features are designed to use Python.
If you have a Raspberry Pi, you’re lucky—Python is already installed on the Raspberry Pi by default on the main Raspbian operating system, so you don’t have to do anything to install it!
There are two versions of Python on the Raspberry Pi: Python 2.7 and Python 3. This book uses Python 2.7 so make sure you choose that one.
Make sure you have Raspbian installed on your SD card before following these instructions. There are a few different ways you can use Python on the Raspberry Pi. The simplest way is to use IDLE.
To open IDLE:
Now that you have Python installed on your computer, let’s test it out with two short programs. This first program doesn’t do much. All it does is display a message. Even so, it’s one of the most useful things you can learn, and you can combine it with other things to do a lot.
Time to write your first program. Here’s how:
print
) will display whatever you write in quotation marks after it. Start by placing your cursor after the >>>
at the start of the first line and type in this command:
print "Time for some adventures"
print
command is used to display text on the next line of the Python shell. So, after you press the Enter key, the text you put in the speech marks should appear on the next line of the Python shell (see Figure I-15). In Python, bits of text are called strings. Whatever is inside the string between the quotation marks will appear on the next line. Try changing the message in the string to whatever you want. For example, you could change it to your name.This program is straightforward, but probably not that impressive. With only a slight change you can do so much more.
This next program builds on the previous one. Instead of displaying a single message on the screen, it displays an infinite number of messages. Follow these steps:
>>>
). Type this line:
while True: print "Time for some adventures"
while True
part of the command you typed makes the print
command repeat forever! You’ll learn more about this during your adventures later in the book.Great work! You’ve completed your first two programs with Python.
Throughout this book, you’ll find references to the Adventures in Python companion website, www.wiley.com/go/adventuresinpython
. (It’s a good idea to bookmark that site now, so you can return to it whenever you need to.) The website includes video tutorials to help you out if you get stuck, code files for some of the more extensive projects and a list of links to locations to download any applications you need during your journey.
Throughout the book, there are notes to guide and support you. They use the following key:
Throughout the book, you will also find two sets of sidebars. Challenge sidebars ask you how you might expand on the projects in the book to make changes or add new features. Digging into the Code sidebars explain some of the special syntax or programming language, to give you a better understanding of the computer language. When you see this arrow in the code (), that means you should type the multiple code lines as one long line in Python. The code is too long to fit on one line in this book, but should be entered as one line in Python.
There are many great resources available that you might find interesting and useful along your adventures with Python:
www.python.org
) is a great place to find out more about the Python programming language. It also has very extensive documentation, which is super-useful when you’re writing your own programs.www.codecademy.com
) has an excellent online Python tutorial that you can use to test your knowledge of the language.www.adafruit.com/blog
) and learning site (https://learn.adafruit.com
) are excellent places to find inspiration for your Python programs. Although the site mainly deals with electronics, there are plenty of projects that use Python programming and other programming languages to control things. Also, if you have a Raspberry Pi, it is the best place online to learn how to use your Raspberry Pi.www.fullstackpython.com
) is an excellent place for beginners to hone their skills and knowledge. Once you’ve completed the adventures in this book, this website has an excellent list of other tutorials you can try with Python, along with other useful programming information.www.pythonweekly.com
) is a weekly newsletter that showcases Python tutorials, projects and news. Although some of it may be a bit complex for beginners, you can find lots of interesting tutorials, such as creating your own games and animations.www.effbot.org/tkinterbook/
) has excellent documentation for the Tkinter
library, which is used in some of the adventures in this book. Once you’ve completed this book, you might find this website really useful for creating your own programs.PyGame
’s official documentation (www.pygame.org/docs/
) is a great place to find out more about the PyGame
library. Not only does it include details of all the features of PyGame
, but it also has lots of sample projects that show you how other people have used PyGame
in their programs.If you would like to contact the author directly, drop him an email at [email protected]
.
Time to start your adventures!
3.145.35.247