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 linearly.
1. Download the installer:
- For Windows 32-bit (if you're uncertain, this is probably the version you should download) : http://www.python.org/ftp/python/3.1.2/python-3.1.2.msi
- For Windows 64-bit: http://www.python.org/ftp/python/3.1.2/python-3.1.2.amd64.msi
- For Mac OS X: http://www.python.org/ftp/python/3.1.2/python-3.1.2-macosx10.3-2010-03-24.dmg
2. Run the installer.
3. Click next for every option in the setup wizard (i.e. use the defaults).
Using the Python Shell
The Python installation comes with an easy-to-use text editor called IDLE. You should start by playing around with this. You should be able to find IDLE wherever Python was installed. On Mac this should be in the Applications folder while on Windows it should be accessible from the Start menu in a folder named Python or something close to that.
When you first open IDLE, you'll see the Python Shell:
Note that 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. 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 Python Shell is very useful for quick math and short sequences of commands:
Here we see a number of familiar operations: + for add, - 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.
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 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. In the file menu click on New Window which 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 you get proper syntax highlighting. 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.
Intuitively, one would start with the smallest positive multiple of 3 and go up one multiple at a time keeping track of the sum stopping once he hits 1000. We can do this with the following code:
You probably haven't seen the notation +=. In Python, the statement is a shorthand for such that we're basically just incrementing by 3 on each iteration of the loop.
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:
Now that you've gotten started, you may want to take this program a step further and solve the first problem from the Project Euler website (which is a great source of programming problems).
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!