Introduction

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.

What Is Programming?

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.

image

FIGURE I-1 The code of a program that generates a circular progress bar. You will learn how to create programs like this one in your adventures.

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.

What Is Python and What Can You Do With It?

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:

  • Making windows with buttons and text fields
  • Creating computer games
  • Making animations
  • Building websites
  • Analysing scientific data
  • Hacking computers

Who Should Read This Book?

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!

What You Will Learn

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.

What You Will Need for the Projects

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.

How This Book Is Organised

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!

Setting Up Python

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.

Windows 8

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

Installing Python on Windows 8 is quite straightforward. Follow these steps to install it:

  1. Open a web browser (such as Internet Explorer, Chrome or Firefox).
  2. In the web browser, click in the address bar.
  3. Type this text into the address bar: python.org/downloads.
  4. Press the Enter key on the keyboard.
  5. When the page loads, click on Download Python 2.7.8 (sometimes the file will download when you click this link, if it does skip Step 6).
  6. Scroll down to the Download section and click on Windows x86 MSI Installer (see Figure I-2).
  7. When the file has downloaded, open it.
  8. When the installer window opens, select Install for all users and click next.
  9. Click next again.
  10. Click next a third time!
  11. When asked if you want the program to install software on your computer, click Yes.
  12. Wait for it to install.
  13. Once it is installed, click Finish (see Figure I-3).
  14. Python is now installed on your computer.
image

FIGURE I-2 Downloading Python for Windows 8

image

FIGURE I-3 Python has been installed on Windows 8.

Opening

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:

  1. Press the Windows key on your keyboard.
  2. Type IDLE and select the Idle (Python GUI) option. IDLE is one of the ways you can use Python on your computer, and the program has many features that help you when you write your programs.
  3. IDLE will now open—you are ready to start using Python (see Figure I-4)! It’s time to try your first program, so skip to “Creating Your First Program” at the end of this chapter.
image

FIGURE I-4 IDLE installed and Python opened on Windows 8

Windows 7

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.

Installing

Follow these step-by-step instructions to install Python on Windows 7:

  1. Open a web browser (such as Internet Explorer, Chrome or Firefox).
  2. In the address bar, type www.python.org/downloads to go to the Downloads page on the Python website.
  3. Once the page has loaded, click on the button labeled Download Python 2.7.8 ( Figure I-5). Sometimes the file will download when you click this link, if it does skip Step 4.
  4. Scroll down to the Download section and click on Windows x86 MSI Installer.
  5. Wait for the file to download, then go to your Downloads folder and double-click on the python-2.7.8.msi file.
  6. A dialog box might appear asking if you are sure you want to run the program. If this happens, click the Run button to continue.
  7. The installer will ask you if you want to install Python for all users on the computer, or just the current user. Choose the option to Install For All Users, and click Next.
  8. Click Next again . . . and again!
  9. A dialog box will appear asking if you want to allow the file to make changes to your computer. Click Yes.
  10. Python will now install on your computer. Wait until this finishes before doing anything else.
  11. Once the installation completes (see Figure I-6), click Finish and follow the steps in the next section to open Python.
image

FIGURE I-5 Downloading Python for Windows 7

image

FIGURE I-6 Python has been installed on Windows 7.

Opening

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:

  1. You can open Python from the Start menu. Click the Start button in the bottom-left corner of the screen.
  2. Click on All Programs to bring up the list of programs installed on your computer.
  3. Select the Python 2.7 folder to show all the options for using Python. The list of programs should be in alphabetical order.
  4. Click on the IDLE (Python GUI) icon in the start menu. IDLE is one of the ways you can use Python on your computer, and the program has many features that help you when you write your programs.
  5. Wait for IDLE to open (see Figure 1-7). Congratulations! You are ready to start using Python! It’s time to try your first program, so skip to “Creating Your First Program” at the end of this chapter.
image

FIGURE I-7 IDLE installed and Python opened on Windows 7

Mac OS X

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.

Installing

Follow these steps to install Python on your computer:

  1. Open a web browser (such as Safari, Chrome or Firefox).
  2. In the address bar, type www.python.org/download and press the Enter key to go to the Download page for Python.
  3. Click on the button that says Download Python 2.7.8 (sometimes the file will download when you click this link, if it does skip Step 4).
  4. When the page has loaded, scroll down to find the 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.
    image

    FIGURE I-8 Downloading Python for Mac OS X

  5. When the file has downloaded, click on the Finder icon and go to your Downloads folder.
  6. Find the file you just downloaded. It should be called python-2.7.8-macosx10.6.dmg.
  7. Double-click on the file to open it.
  8. Right-click on Python.mpkg and click on Open With⇒Installer (see Figure I-9).
    image

    FIGURE I-9 Installing Python on a Mac

  9. When asked if you want to open it, click Open. Then click Continue three times and then click Agree.
  10. Next, click Install and when prompted type in your password and click Install Software.
  11. Wait while Python installs.
  12. When the installation completes (see Figure I-10) you can now use Python on your computer. (It’s also a good idea to remove the installer that you no longer need.) Click Close.
    image

    FIGURE I-10 Python has been installed on the Mac

  13. On the desktop, right-click on the Python 2.7.8 disk image and select Eject.

Opening

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:

  1. Open a new finder window and click on the Applications folder on the sidebar to show all the applications installed on your computer.
  2. In the list of installed applications, find Python 2.7. Double-click on Python 2.7 to open the folder.
  3. Double-click on IDLE. Wait until IDLE loads. IDLE is one of the ways you can use Python on your computer, and the program has many features that help you when you write your programs.
  4. When IDLE has loaded, you’re ready to start using Python (see Figure I-11)! It’s time to try your first program, so skip to “Creating Your First Program” at the end of this chapter.
image

FIGURE I-11 IDLE installed and Python opened on Mac

Linux (Ubuntu)

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.

Installing

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:

  1. Click on the Dash button, or press the super key, to open the Ubuntu dashboard.
  2. In the search box of the dashboard, type terminal and click on the Terminal program to open it.
  3. Once the Terminal is open, type in the following:

    apt-get update && sudo apt-get upgrade

  4. Press Enter and type in your password if asked (see Figure I-12). Wait for a bit while the update takes place. Type in Y and press Enter if asked.
    image

    FIGURE I-12 The installation process in Linux

  5. Wait for the upgrades to install. When this is complete, type in the following to install IDLE (see Figure I-13):

    sudo apt-get install idle-python2.7

    image

    FIGURE I-13 Installing IDLE on Linux

  6. If you are asked if you want to continue, type Y and press Enter.
  7. Wait while IDLE installs. IDLE is one of the ways you can use Python on your computer, and the program has many features that help you when you write your programs. When this finishes, you are ready to open IDLE to write and run Python programs on your computer.

Opening

The next steps show you how to open IDLE. Remember these steps, as you will use them throughout your adventures:

  1. Click on the Dash icon to open the dashboard.
  2. In the search field, type IDLE and click on the IDLE icon when it is found.
  3. IDLE will now open—you are ready to start using Python (see Figure I-14)! It’s time to try your first program, so skip to “Creating Your First Program” at the end of this chapter.
image

FIGURE I-14 IDLE installed and Python opened on Linux

Raspberry Pi

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.

Installing

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.

Opening

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:

  1. Switch on your Raspberry Pi. You can find guides by searching online if you’re not sure how to do this.
  2. When asked for your username, type pi and then type the password raspberry.
  3. After logging on, type startx to start the desktop.
  4. Once the desktop has loaded, you can open IDLE. On the desktop you will see IDLE and IDLE 3. Double-click on IDLE—don’t touch IDLE 3! IDLE 3 is for a different version of Python that you won’t be using in this book. IDLE is one of the ways you can use Python on your computer, and the program has many features that help you when you write your programs.
  5. IDLE may take some time to load, so be patient. The Raspberry Pi is a lot slower than most other computers.
  6. When IDLE is open, you are ready to start using Python! It’s time to try your first program, so move on to “Creating Your First Program” at the end of this chapter.

Creating Your First Program

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:

  1. Open IDLE. If you’re not sure how to do this, look at the instructions for your operating system earlier in this introduction.
  2. When IDLE loads, it will open the Python shell. The shell is a space where you can type in commands for your Python programs, one line at a time. You can tell that you are using the shell because each line starts with a >>>. This is called the command prompt.
  3. Here is your first command for the Python shell. It may not do that much but every adventure starts with one small step! This command (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"

  4. Press Enter.
  5. The 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.
image

FIGURE I-15 After you press the Enter key, the text inside the speech marks appears on the next line of the Python shell

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:

  1. Open IDLE if it is not already open.
  2. Click in the shell after the command prompt (>>>). Type this line:

    while True: print "Time for some adventures"

  3. Press the Enter key twice and see what happens (see Figure I-16). Within a short space of time, the phrase “Time for some adventures” should appear on every line of the shell. New lines will be added several times every second. This is because the 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.
  4. But how do you stop the command from repeating? Go to the menu at the top of the shell and select Shell⇒Restart Shell. Try changing the program to display whatever you want.
image

FIGURE I-16 The phrase appears on every line of the shell

Great work! You’ve completed your first two programs with Python.

The Companion Website

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.

Conventions

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 (arrow), 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.

Reaching Out

There are many great resources available that you might find interesting and useful along your adventures with Python:

  • The official Python website (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.
  • Codecademy (www.codecademy.com) has an excellent online Python tutorial that you can use to test your knowledge of the language.
  • Adafruit’s blog (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.
  • Full Stack Python (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.
  • Python Weekly (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.
  • Effbot (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!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.247