Hour 1. Installing and Running Python


What You’ll Learn in This Hour:

Image How to determine what operating system you’re running

Image How to install and run Python

Image How to input basic commands into Python


Installing Python is one of the most important things you’ll be doing in this book. Without it, you can’t complete the rest of this book! Make sure to take your time in this hour. If you can’t pass the exercises at the end of the hour, you’ll have problems with every hour after this one.

Discovering Your Operating System

Many people know what kind of computer they have, but have no idea about the specific operating system that’s installed on it. Knowing what operating system you’re running is vital to learning how to program because it might change what you need to download or how you access certain parts of the system.

In general, if your computer was made by Apple (for example, if it’s a MacBook or PowerBook), it’s running Mac OS. Most other personal computers are running Windows.

If you ever have issues, you will need to know exactly what version of your OS you’re running. On a Mac, click the Apple icon in your menu bar and select About this Mac. A window will pop up with some information about your computer, including the exact version of your OS (see Figure 1.1).

Image

FIGURE 1.1 Finding the exact version of Mac OS.

If you’re running a Windows machine, click your Start menu and find the Command Prompt program under Accessories. Clicking it will open the command prompt for your computer (see Figure 1.2).

Image

FIGURE 1.2 Finding Command Prompt in Windows.

If you’re having trouble finding the command prompt, search for “cmd” in the Start menu’s search or run box.

Once Command Prompt is open, type systeminfo and press Enter. A bunch of data will print out, but what you need is at the top. Scroll up and look for a line starting with “OS Name.” In Figure 1.3, the version is Microsoft Windows 7 Home Premium.

Image

FIGURE 1.3 Finding the Windows version in systeminfo.

Now that you’re clear about what operating system you’re running, let’s install Python and a text editor. We’ll cover Windows first. If you’re using a Mac, go ahead and skip to the Mac section.

Setting Up Python on Windows

In this section, we’ll guide you through installing Python on your Windows machine. Python 2.7 will run on Windows 2000, XP, Vista, and Windows 7 and 8. If your computer was bought after 2000, you’re probably running one of these operating systems.

As for memory and hard drive space, Python is designed to run on little memory and take up little space. If you’re running any Windows release after XP, you’ll be fine.

Installing Python on Windows

Go to http://www.python.org/getit/ in any browser. There, you’ll see a list of various downloads for Python. Some of the downloads are for other operating systems, some are the code that makes Python, and some are Python installers made by other companies. We’re only interested in the one that will install Python on a Windows machine.

Look for “Python 2.7.5 Windows Installer (Windows binary -- does not include source), as shown in Figure 1.4. The last two numbers (the five and the seven) might change, but you should definitely get the package starting with the two. Python 3 is out, but this book is written for Python 2 (for more information about why we’re using version 2 rather than version 3, check the “Q&A” section of this hour). There are some subtle differences between the two that might get confusing down the road if you install the wrong version.

Image

FIGURE 1.4 Windows Installer on Python.org.

Click the link to download the installer. Once it’s done, click the downloaded installer to install Python. You should accept most of the default settings. The only one you should consider is whether you want to install Python just for you or for all users. If you’re the only person on your computer, the question is moot, but if you share it with others (and they have their own logins), you should decide if you want to install Python for them as well. If you’re not sure, install Python for all users because it doesn’t significantly change the way their computer works and only adds a few new items to the Start menu.

If you’re on Windows Vista or later, you’ll likely get a pop-up asking if you want to allow the installer to make changes to your computer. Click Allow (or Okay, or whatever seems to be an affirmative response) to allow the installation to continue.

Once the Python installation is complete, you should have some new items under your Start menu. If you don’t have the items in Figure 1.5, try to install Python again. You may have canceled the installation at some point by accident.

Image

FIGURE 1.5 New Start menu items.

Running Python on Windows

For the book’s early hours, we’ll be running Python through the Python shell. The Python installer has given us two tools that make getting to the shell pretty easy: a link to the command line and a program called IDLE, shown in Figure 1.6. From here on out, when you’re asked to open a Python shell, open IDLE. Sometimes you’ll be asked to run a file. In that case, open the file in IDLE and then either select Run Module under the Run menu or press F5.

Image

FIGURE 1.6 The Python shell in IDLE.

When you open IDLE, you’ll see a screen like the one in Figure 1.6. This is called the Python shell. Here, Python is waiting for you to type in commands, which it will execute right away. Go ahead and enter the following and press Enter:

print "Hello, world!"

Your screen should look like IDLE in Figure 1.7.

Image

FIGURE 1.7 A line of Python code in IDLE.

Congratulations! You’ve written your first line of Python code!

Installing a Text Editor on Windows

IDLE comes with a text editor, but you might want one that’s a bit more robust to use in the book’s later hours. For that, Notepad++ is a good option. It’s available for free at http://notepad-plus-plus.org. For the moment, though, IDLE’s text editor should be fine.

It’s important never to open a Python file with a word processing program such as WordPad or Word. They have a tendency to wreak havoc with formatting and insert items that you may not be able to see. Once they’re in there, it can be difficult to find them and remove them.

Getting Around the File System

Though we’ll be working with Python through IDLE in the beginning, eventually you’ll need to get around your computer via the terminal.

Open a command prompt (this is the window you opened earlier to get information about your system). You should see something like this:

C:UsersYourName> _

Where your cursor is currently blinking is called your command line (though you’ll often see it referred to as your “prompt”). The text can be customized, but on most Windows computers, it’s set to your current directory (another word for “folder”).

To see what your current directory is, use the cd command:

C:UsersYourName> cd
C:UsersYourName

If you want to move to another directory, add that directory after the cd command:

C:UsersYourName> cd Downloads
C:UsersYourNameDownloads>

You can also use the full path of the directory you want to move to (that’s a line that contains every nested directory):

C:UsersYourName> cd c:UsersYourKid
C:UsersYourKid>

You can also get a list of everything in a directory by using the dir command. If you use the command on its own, it will give you a list of the files in your current directory. If you give the command a directory, it will return all the contents of that directory.

C:UsersYourNameprojects> dir
c:UsersYourNameprojects> dir
 Volume in drive C is TI105970W0D
 Volume Serial Number is 52G3-1C5A

 Directory of c:UsersYourNameprojects

12/08/2012  09:38 AM    <DIR>          .
12/08/2012  09:38 AM    <DIR>          ..
12/08/2012  09:36 AM    <DIR>          rogue
06/20/2012  02:24 PM               198 todo.txt
12/08/2012  09:36 AM    <DIR>          website
               1 File(s)            198 bytes
               4 Dir(s)  79,784,599,552 bytes free

Each line tells you the following:

Image When the file was created

Image Whether it’s a directory (indicated by <DIR>)

Image How big the file is

Image What the directory or file is called

If you want to make a new directory, use the mkdir command. This command requires that you tell it what you want to name the directory. If you just use the command on its own, you’ll get an error.

C:UsersYourNameprojects> mkdir python
C:UsersYourNameprojects> dir
Volume in drive C is TI105970W0D
 Volume Serial Number is 52G3-1C5A

 Directory of c:UsersKatieprojects

12/08/2012  09:40 AM    <DIR>          .
12/08/2012  09:40 AM    <DIR>          ..
12/08/2012  09:40 AM    <DIR>          python
12/08/2012  09:36 AM    <DIR>          rogue
06/20/2012  02:24 PM               198 todo.txt
12/08/2012  09:36 AM    <DIR>          website
               1 File(s)            198 bytes
5 Dir(s)  79,784,603,648 bytes free

Now that you know how to get around on your computer through the command prompt, feel free to move to the “Try It Yourself” section.

Setting Up Python on a Mac

In this section, we will go over setting up Python on your Mac and installing a text editor. If you’re using a Windows machine, feel free to skip this section.

Installing Python on a Mac

If you’re running a Mac, you already have Python installed! There’s no need to download anything extra. Though there are some slight differences between the types of Python on older Macs, those differences shouldn’t affect the activities we’ll be doing in this book.

Running Python on a Mac

Whenever you’re asked to run the Python shell, you’ll need to start up IDLE. Sometimes, you’ll be asked to run a file. In that case, start up IDLE and open the file (look under the File menu). Once the file is open, make sure to select the window with the code you want to run and then select Run Module under the Run menu.

In order to get IDLE running, you’ll need to open up a terminal window. Click the search icon in your toolbar and search for “terminal.” You should see something like the screen shown in Figure 1.8.

Image

FIGURE 1.8 Finding the terminal.

Clicking Terminal will bring up a terminal window like the one shown in Figure 1.9. A terminal window gives you access to your computer through the command line.

Image

FIGURE 1.9 The terminal window.

We’ll go over some of the things you can do in the terminal later. For now, let’s start up IDLE. On the command line, type idle and press Return. A new program will start up that looks like the screen in Figure 1.10. This is the Python shell. Python is actively running and waiting for you to input commands.

Image

FIGURE 1.10 The Python shell in IDLE.

Go ahead and type print “Hello, world!” and press Return. You should see something like Figure 1.11.

Image

FIGURE 1.11 A line of Python code in IDLE.

Congratulations! You’ve written your first line of Python code!

By default, the font is set a bit small for some monitors. If you want to change the font size, go to Configure IDLE under the Options menu. There, you can make the font as big as you need.

Installing a Text Editor on a Mac

In the book’s early hours, you’ll be working with the shell. As your programs grow, however, you’ll need a text editor that is geared toward writing code. A great free text editor is TextWrangler, found at http://www.barebones.com/products/textwrangler/download.html. Download the disk image (that’s the installer), and once it’s done downloading, click it to install.

It’s very important that you do not open your code in any word processing program such as Word or TextEdit. Programs like that can reformat your code and insert items you can’t see. In the best case, your code will look ugly. In the worst, and most common case, your code will simply refuse to run.

Getting Around the File System

Though we’ll be working with Python through IDLE in the beginning, eventually you’ll need to get around your computer via the terminal.

Open a terminal window. You should see something like this:

ComputerName: ~$ _

Where your cursor is currently blinking is called your command line (though you’ll often see it referred to as your “prompt”). The text can be customized, but on most Macs, it’s set to your computer’s name and your current directory (another word for “folder”). The tilde (~) is a shortcut for your home directory, which is often /Users/Yourusername/.

To see what your current directory is, use the pwd command:

ComputerName: ~$  pwd
/Users/YourName/

If you want to move to another directory, use the cd command:

ComputerName: ~$ cd Desktop
ComputerName: ~/Desktop/$

You can also use the full path of the directory you want to move to (that’s a line that contains every nested directory):

ComputerName: ~$ cd /Users/YourKid/
ComputerName: /Users/YourKid$

Note that you’ll often hear people call directories “folders.” These are synonyms, and are often used interchangeably.

You can also get a list of everything in a directory by using the ls command. If you use the command on its own, it will give you a list of the files in your current directory. If you give it a directory, it will return all the contents of that directory.

ComputerName: ~$ ls
ariel.pubkey.asc        Documents               server-misc
asn1                    ldap                    sh-lost
bin                     linux                   signatures
ca-admin                logs                    sounds

ComputerName: ~$ ls Documents
homework1.doc
resume.doc
todo.txt

If you want to make a new directory, use the mkdir command. This command requires that you give it some sort of value, so it knows what to call the directory.

ComputerName: ~$  mkdir projects
ComputerName: ~$ ls[ADD]

Summary

In this hour, we installed Python on your machine, installed a text editor, and tried out a few Python commands. You also learned what operating system you’re running, and you learned some basics about how to move around in the file system.

Q&A

Q. Why am I getting Python 2.7 rather than Python 3?

A. Whenever a new version of Python comes out, it takes a while for everyone who has written libraries that use Python to catch up. Python 3 is great, but some of the libraries we’ll be using later haven’t moved over to it yet.

Q. Will I have to start learning all over again when everyone moves to Python 3?

A. Not at all! Much of the functionality from Python 2 has been moved over to Python 3. The vast majority of Python 3 will be very familiar to you, once you’ve completed all 24 hour lessons in this book. Once you feel ready to look into Python 3, check out the guide on Python.org. It will catch you up on all the changes that have been implemented.

Q. Are there any operating systems besides Mac and Windows?

A. There are! Linux is a popular operating system among Python developers. Linux comes in many flavors—from those designed for enormous enterprise systems to those designed for schools and children. These are called “distributions.” With the exception of a few made for large businesses, all Linux distributions are free. Some, such as Ubuntu, even have an installer that allows Windows users to dual boot: A user can start up his or her machine in Ubuntu or Windows. For more on Linux, check out the “Next Steps” section at the end of this book.

Workshop

The Workshop contains quiz questions and exercises to help you solidify your understanding of the material covered. Try to answer all questions before looking at the answers that follow.

Quiz

1. What version of Python am I running?

2. What is another word for “folder”?

3. True or false? If I want to edit code in a text editor, I should use Microsoft Word or WordPad.

Answers

1. Python 2.7. There might be another number after the 7 (for example, 2.7.5), but that number can be ignored.

2. Directory. This is how we’ll be referring to folders in this book.

3. False! Rich editors such as Word and WordPad will wreak havoc on your code! Use a code editor such as Notepad++ for Windows or TextWrangler on the Mac.

Exercises

1. Through the command line, create a new folder called “projects” in your home folder. Then, change into that directory and create another folder called “python”.

2. In your text editor, create a new file called hello.py in your new python directory (the one you made in Exercise 1). Type print "Hello, world" into it and then save and close it. In your command line, see how big the file is.

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

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