unrequited narcissism

Archives: how-to
Archives: how-to
February 13, 2006
February 13, 2006
files and regular expressions how-to

Back to Python! Our previous installments involved installing Python and variables, control structures and functions. I know that the control structure entry might have been intimidating to the non-programmers who are reading this. All I can suggest is that you ask questions — and not worry about it too much. Programming languages are meant to be readable by humans; with enough exposure to them, they do eventually begin to make an intuitive sort of sense. In the short term, I'd suggest that you just try to figure out what this (intentionally inefficient) code does:

i = 1
while(i<10):
   if((i%2)==0):
      print i
   i = i + 1

(remember: % is the modulus sign — x % y gives you the remainder from dividing x by y)

If you feel comfortable reading that code, I'd say we can proceed. If not, ask me some questions via comments or email.

MORE...
comments [22] trackBack [0] posted by tom - link
February 10, 2006
February 10, 2006
python: a recursion diversion how-to

As Ben pointed out, I promised an alternate solution to the programming task I gave at the end of the last installment of this little tutorial series. Now that I've been properly goaded, here it is.

MORE...
comments [9] trackBack [0] posted by tom - link
January 28, 2006
January 28, 2006
variables, control structures and functions how-to

Alright! Let's do some programming. This is going to be kind of a long post, but it will (hopefully) include all of the basic concepts that you'll need to grasp in order to write a computer program. I'd suggest coming back to it a couple of times if you don't have the stamina to power through. I wanted to get it all out at once, though — I always find it frustrating to be given piece after tiny, useless pieces, patiently waiting until things start making sense.

Let's get on with it...

MORE...
comments [10] trackBack [0] posted by tom - link
January 22, 2006
January 22, 2006
delays how-to

Sorry for not yet coming through on the next installment of our little Python how-to. Unfortunately, work has reared its head, and the time I'd set aside for writing it is getting eaten up by more pressing issues. But it's coming! Soon!

comments [0] trackBack [0] posted by tom - link
January 19, 2006
January 19, 2006
installing and testing python how-to

This should be pretty easy.

Windows
You have two options. The basic distribution of Python (2.4.2) is available here. However, there's also a free IDE (integrated development environment) available here. The latter option (ActivePython) is probably easier to work with, and doubtless has a lot of cool features. Let's go with that, why don't we.

Mac
OS X 10.2 and higher comes with Python pre-installed. You don't have to do anything — but I'd recommend that you go here, download the installer, and run it. For one thing, this will update your version of Python. For another, it will provide you with an IDE of your own (PythonIDE). More importantly, it'll provide a package manager, which will be important later.

Linux/Other
You guys probably know how to fend for yourselves. Source tarballs can be found here.

Testing It
Let's put together a Python program. I promised interesting applications, but it'd be a shame to skip past Hello World -- it's a rite of passage. Open up ActivePython or the PythonIDE and create a new file (File|New). Enter the following:

print "hello world"

It's hardly worth explaining, but I may as well: this single line calls the print function and passes it a string literal with the contents hello world. Don't worry about what a function is or what a string is or what a literal is (if you do know these things, feel free to feel smug about that fact). Just know that saying print "something" spits 'something' out to the screen, and that the quotation marks in the program aren't part of the output — in this case, they let Python know that it should treat the text hello world differently than it treats the text print.

Once you've got that entered (raise your hand if you're still working), save the file somewhere, then hit the "Run All" button in PythonIDE (Mac) or the "Check"/Little Running Guy button in ActivePython (Windows). ActivePython will pop up a "Run Script" box — just hit return.

The program should spit out the expected string in an output window (PythonIDE will create a new window; ActivePython sends output to the "Interactive Window" that should have opened when you started the program).

Astounding, isn't it? Well, don't worry, we'll be doing real programming soon. For now, just make sure that you understand how to create, load and save Python files — it's all done in exactly the same way as you'd manipulate documents in MS Word.

Also worth noting: these files are simply text files (usually with names ending in .py; that's just the convention). You can edit them in Notepad or TextEdit if you'd like, and run them from the command line by typing "python nameofprogram.py" (assuming you're in the same directory as the file, and the Python executable has been correctly placed in your path by the ActivePython or MacPython installer).

That's it for now. Next up, the meat of any programming language: variables, control structures and functions (not necessarily in that order). By the end of that lesson, you'll be ready to create some of the world's crappiest videogames. Let me know in comments if you had trouble with the installation or with Hello World.

comments [9] trackBack [0] posted by tom - link
January 18, 2006
January 18, 2006
pythonathon how-to

I'm sorry that the tutorial has been slightly delayed. The first entry has been living happily in Movable Type for a few days now, waiting for me to find time to test its contents on a Windows machine. Sadly, travel, work and Belgian beers have gotten in the way. Tonight looks to be pleasantly uneventful, though. I should be able to put something up this evening.

comments [12] trackBack [0] posted by tom - link
January 15, 2006
January 15, 2006
learning is fun! sort of! how-to

Alright, let's get things started — or at least planned. As I mentioned in a previous entry and in comments, I'm going to try to put together a concise set of posts aimed at getting users with no programming knowledge up to speed at doing cool things on and to the internet. I'm hoping to end up with something that would've suited my fourteen year-old self perfectly. This post will outline what I intend to do and why I'm going to do it. It'll ramble on for a bit, but the actual tutorial is going to be as concise and clear as I can make it.

I'm going to do this in Python, mostly because I don't know the language and would like an excuse to learn it. But there are non-selfish reasons for the choice, too:

It's So Hot Right Now
Google loves Python. Adrian Holovaty loves Python. Everyone loves Python. It's not the only trendy language at the moment (see also: Ruby, C#). But rest assured, there are tech managers out there right now who would be impressed to see Python on your resume (frequently despite not actually knowing what it is).

It's Cross-Platform
There are good implementations for OS X, Windows and Linux. You should, in theory, be able to take a program from one platform to the other and have it Just Work. And there are tools available for each that'll let you produce native standalone applications, letting you run your programs on computers where Python isn't installed.

It's Got The Most Dirty-Joke-Friendly Name Of Any Programming Language
Taking the crown from LISP, I suppose.

There's one big downside, though, that's worth pointing out:

It Uses Weird Syntax
Many languages have adopted a syntax similar to what's used in the C programming language. You could look at a Java, C++, C#, ECMAScript or even Perl snippet of code and not be able to tell what language it was written in. That's not the case with Python — unlike those other languages, it relies on whitespace (e.g. tabs, spaces and line breaks) to determine how things work. That's unusual, and it means you'll probably have a slightly harder time learning your second programming language if Python is your first.

Here's the outline I've got in mind:

  1. Installing Python And Making Sure It Works

  2. Variables, Control Structures And Functions

  3. Regular Expressions and File Input/Output

  4. Interacting With Websites

  5. Putting It All Together To Do Something Cool

Finally, I should note that there are a ton of other Python tutorials out there — I haven't completely gone through Mark Pilgrim's Dive Into Python, but if it's as good as his GreaseMonkey book, I can highly recommend it. When things don't make sense or you want to go in a new direction, you should look through some of the material linked from python.org. This will almost certainly not end up being the best tutorial out there; my hope is just that it'll be one of the most pragmatic. And, of course, we can deal with questions in the comment sections.

comments [8] trackBack [0] posted by tom - link
  Powered by Movable Type 3.2
 

Warning: include(../analytics.php) [function.include]: failed to open stream: No such file or directory in /home/metamon/public_html/zunta/blog/archives/tech/howto/index.php on line 929

Warning: include(../analytics.php) [function.include]: failed to open stream: No such file or directory in /home/metamon/public_html/zunta/blog/archives/tech/howto/index.php on line 929

Warning: include() [function.include]: Failed opening '../analytics.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/metamon/public_html/zunta/blog/archives/tech/howto/index.php on line 929