learning is fun! sort of!

posted by tom / January 15, 2006 /

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

It Uses Weird Syntax

Oh, please. Like you don't indent anyway.

Posted by: ben wolfson on January 15, 2006 07:55 PM

Indenting is good. But {} braces are definitely more of a standard. And using colons? Very Visual Basic. It's tacky. Besides, beginners' code tends to be messily laid-out, for whatever reason.

The weirdest syntax, though, comes in when you start using some of Python's cooler features, like list comprehensions. Those are mostly just time savers, though. I'm going to ignore them in favor of less efficient but clearer ways of doing things.

Posted by: tom on January 15, 2006 08:04 PM

I don't know Python so I'm looking forward to this as an excuse to check it out.

Regarding whitespace, does it differentiate between types of whitespace (i.e., tabs vs. spaces)? Relying on whitespace for control is generally a Bad Thing but differentiating between different kinds is pretty unforgivable (e.g., make)

Posted by: Becks on January 15, 2006 08:16 PM

Becks, I'm not sure; I think that if you mix tabs and spaces in a single file, it won't accept that. And you have to be consistent about the amount you indent, at least within suites.

List comprehensions are borrowed more or less directly from Haskell, which has a more mathematical notation for them (some examples). Haskell also has significant whitespace, btw.

The next version of python is growing some syntax, btw: generator comprehensions, function modifiers introduced with an @name before the function definition, and I think generators were getting changed somehow to become full-fledged coroutines, but that may have been put back on the table. Oh, and exception handling is being changed so that you can have an except: and a finally: in the same suite, and a conditional expression. (Actually these might not be in the very next version, but it's they've been accepted for future inclusion.)

Posted by: ben wolfson on January 15, 2006 08:34 PM

Thanks, Ben.

Posted by: Becks on January 15, 2006 08:47 PM

More informative:

Tabs or Spaces?

Never mix tabs and spaces.

The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.

(from.)

So I guess you can mix them, if you want.

Posted by: ben wolfson on January 15, 2006 09:05 PM

A language that allows you to mix them doesn't bother me as much as one that treats the differently. make only recognizes lines as part of a command when they are indented with tabs, not spaces. I can't tell you how many times I've seen someone screw up a build because they went in and edited a makefile with their editor setting on "convert tabs to spaces" or accidentally inserted a space at the beginning of a line. NOT a fun bug to track down.

Posted by: Becks on January 15, 2006 09:29 PM

Becks, that's why I LOVE emacs's make-mode, which lights up your troublesome whitespace in bright magenta or some other god-awful thing.

Although truthfully I haven't used make in years since I got hooked on Java.

I was planning on teaching myself some Python, so I'll be watching this with some interest, although I admit to be put off by, for instance, Paul Graham's Python-snobbery.

Posted by: Matt #3 on January 17, 2006 02:44 PM

Post A Comment

Name


Email Address


URL


Comments


Remember info?



Google Analytics