Python: Getting Started

Awww… isn’t she the cutest little thing?

Python is both a cuddly pet (when in ball form) and an awesome programming language. I’m well aware that most people don’t equate “awesome” and “computer programming”, but that’s simply because most people are ignorant fools. So, unless you already define those two terms in the same units (of awesome) or are happy being an ignoramus, read on. While the following will tell you only how to get Python and start fiddling, chances are good that even this tiny speck of knowledge will increase your sexual desirability to such a degree that you will be required to defend yourself against the constant advances of members of both sex. Consider yourself forewarned.

First things first: What operating system are you running? If you have the misfortune of being OS-capped by MacOS I would start by throwing that expensive cultural icon out the window and buying a computer that you can, you know, take the battery out of or something. Throw it out with a bright colored screen in front of you, so that people watching will see your silhouette, think it’s one of the commands that they normally follow from their Apple overlords, and do the same. Now that that’s done, I will never have to discuss Macs again. However, I will still do so, and often, but only to express a loathing that could not even be held back by supernatural forces, if those forces existed. In fact, if you diluted my loathing and slapped it around a bit, you would end up with a potent tonic that would turn Eeyore manic.

Ok, back to Python. If you’re running Linux, then you’re already doing everything right. If you’re not, I suggest you get to work learning Ubuntu. It’s free, easy to use, free, doesn’t cost any money, and you don’t have to pay for it. No matter your distribution, most Linux systems come with Python. Just open up a terminal and type python into it and WHAJAM you’ll be running it in Interactive Mode. What is this Interactive Mode? Well, we’ll get there. But first:

Since Mac’s aren’t worth the burnt tabletops their exploding batteries leave in their wakes, I assume that if you are not running Linux then you must be on Windows. And hopefully Windows 7 or XP (Vista is the third Matrix movie in Windows OS trio). To get Python on Windows, just download the installer from the Python website. Install the thing (hopefully I don’t need to tell you how to do that… but if I do just leave a note in the comments) and you’re ready to go. Simple, right?

A quick note on versions: Python 3.x is (as of this post) the current version, and 3 is backwards-incompatible. If you aren’t familiar with that term, “backwards-incompatible” basically means that code written for Python 2 might not work with Python 3. The same thing happens when you buy a Wii and it can’t play your N64 games. Both situations are equally as lame. Anyway, get Python 3. There is less documentation for it, and how-to’s are currently harder to come by than for version 2.x, but you want to ride the wave of the future, right?

Now that you have Python (or know that you already had it) we can hit the basics. There are several ways that you can run Python things. The first way is “interactively”. This means exactly what you think: you type code and get to immediately see the results. This method is great for learning and trying stuff, but is a terrible idea for writing actual programs. Since we’re into learning here, we’ll start with interactive.

You can run Python interactively via IDLE (or in the console in Linux; just open up a terminal and type python). In Windows, go to Start>Programs>Python3.x>IDLE (or a similar path, depending on your Windows version). In Ubuntu, go to the Ubuntu Icon>Programming>IDLE. Now you should be looking at a window like this:

This is where the magic happens. The >>> thing points to where you type in your code. Before I send you off into the wild, with nothing but a kick in the pants and some links to useful websites, let me show you a few brief reasons Python is awesome. Try these little pieces of code, followed by the “Enter” key to see the results (don’t include the numbers):

  1. import antigravity
  2. print( “hello world!” )
  3. print( ‘hello world!’ )
  4. print(input(‘Echo this input: ‘))
  5. for letter in “hello world!”: print(letter*3)
  6. ‘hello world!'[:5]
  7. ‘hello world!'[5:]
  8. ‘hello world!'[::-1]
Bitchin’, yes? Okay, so you probably don’t care much about the print() function unless you already enjoy programming. But you’ll learn the significance soon enough.

Now, go play! Here are some useful websites and books that I’ve been using to teach myself, and I’ll throw some posts up every so often on solving little problems using Python:
Note that some of these refer to Python 2.x. The code will still mostly work, but you’ll definitely run into this problem:
In 2.x, the print() function is just print ‘blah blah’. In 3.x, you have to surround the ‘blah’ with parentheses: print(‘blah blah’). Also, in 3.x there is no raw_input(). The function input() does the same as what raw_input() does in 2.x.
Good luck!