Getting Started With Python Programming
This guide takes you through the process of getting started with programming using the Python language. The sections flow from one to the next so it's recommended to read through this document in order from top to bottom.
Confusingly, Python has two "current" versions (2.7.1 and 3.2) and they are not fully compatible. We recommend using version 3.2. (This is the version that our Introduction to Programming course will be using.)
Python is open-source software and is free to install and use. Here are installation instructions for Windows or Mac. (Linux users: you're essentially on your own -- go to http://www.python.org/download/releases/3.2/ and select the package that works best for you.)
- Download the installer:
- For Windows: click on the following link to download the installer: http://www.python.org/ftp/python/3.2/python-3.2.msi.
- For Mac OS X: click on the following link to download the installer: http://www.python.org/ftp/python/3.2/python-3.2-macosx10.3.dmg.
- In either case, your browser should pop up a box asking you if you want to save the file. Click the "save file" option. Depending on how your browser is configured, you may be asked where to save the file. If this is the case, keep track of where you save the installer.
- Find where the installer was downloaded and double click on it to run it. On most browsers, you should simply be able to double-click the installer from the browser's "downloads" window or menu. You may also have to click "Run" or "Yes" to a security window -- do this if necessary.
- The setup wizard should launch. You should just click "Next" for every option in the setup wizard (i.e. use the defaults), unless you have some specific reason not to.
Using the Python Shell
The program that you'll use to run Python is called IDLE. It may be listed on your computer as "IDLE (Python GUI)".
- On a Mac, IDLE should be in the Applications folder.
- On Windows, IDLE should be accessible from the Start menu in a folder named "Python 3.2" (or something similar).
When you first open IDLE, you'll see the Python Shell:
(The screenshots in this article are taken using IDLE on a Mac with the font increased. Thus IDLE may look a little bit different for you but should still function similarly.)
Note that the first line is the version of Python, which is 3.1.2 in the screenshot but should be 3.2 if you installed it as directed above. Another thing to note is that in the lower left hand corner of the Python Shell you can see that it says "Ln: 4 Col: 4". This is just telling you where in the document your cursor is. In this case it's on line 4 and over in column 4. (The line and column number may be slightly different for your installation.)
The Python Shell is very useful for quick math and short sequences of commands:
Here we see a number of familiar operations: + for addition, - for subtraction, * for multiplication, and / for division. The last operation shown in the example, denoted by **, happens to be exponentiation. One neat feature to note about Python is that it can store arbitrarily large numbers (limited by the amount of memory your computer has). Trying some hefty exponentiation, we can see that we can compute with some pretty big numbers such as as illustrated below.
While Python can make for a pretty good calculator, it can do a whole lot more. One example is when dealing with strings as follows:
Here we are concatenating the three strings "python", "is", and "cool" by using the + operator. Notice that previously we used + to add numbers but now with strings, Python concatenates them! You may also note that the output of the operation gives us a string with single quotes around it. In Python, you are able to use single quotes or double quotes to denote a string. You can use them interchangeably.
As a final example, we can even write code in the Python Shell that extends beyond a single line as shown below. We also see our first example of a loop.
As you type the above, the Python Shell will automatically indent the second line for you. To let the Python Shell know that you're done and are ready for it to run your code, you'll need to put in an extra blank line by hitting the Enter key again. At that point it should run your code and print your output.
Take some time to play around with the Python Shell. You'll want to go through a more extensive introduction to programming to learn the full extent of what you can do with Python, but you can still do some pretty nifty stuff by just playing around. The Python Shell also has an extensive built-in help system -- just type help() at the ">>>" prompt to get started and then follow the instructions it gives you.
The IDLE Text Editor and Your First Python Program
For most programming needs, you'll want to edit your program in a separate document and then run it. Happily, IDLE comes with its own built-in text editor.
To get started, go to the File menu of the Python Shell and click on "New Window". This should give you a blank document with the title "Untitled" as shown below:
You'll need to save your file before running it, so you might as well save it now. Make sure that you name your file with a name that ends in .py, so that your computer knows it is a Python program. Here we save ours as test.py:
To get acquainted with the text editor, let's write our first Python program! Let's write a program to the following task:
Find the sum of all the positive multiples of 3 below 1000.
We solve this by keeping a running total: we'll start with the smallest positive multiple of 3 and go up one multiple at a time keeping track of the sum, stopping once we hit 1000. We can do this with the following code:
Notice that as you type the above code, the keywords ("while" and "print") will automatically get colored -- this makes the code easier to read. Also, after typing the line "while i < 1000:", the editor will automatically start indenting for you. When you get to the line "print(total)", you'll need to use the backspace key to remove the indentation. It is important that the code look exactly like it does in the screenshot above: in Python, proper indentation is very important!
This program basically works by incrementing by 3 every time and adding it to the . The notation might be intimidating at first. However, the statement is just a shorthand for .
Now that we've written this code, we probably want to run it and test it out. We can do so by going to the Run menu and hitting Run Module (shortcut F5). The program should execute and print out the answer to the Python Shell:
The RESTART line just means that Python is clearing all the work you've previously done before it starts running your program. Then, the program runs and we get our answer, 166833. If instead you get an error message or a different answer, check that your program exactly matches the screenshot above, and try it again.
Now that you've learned the very basics of getting Python going, there's a bunch of tutorials you can look at which are listed on the Python website. Go check them out!