Friday, May 19, 2006 and python

It's recently been said in the python community, most notably by Guido himself, that Python needs an evangelist. I agree with that. I feel the same way about Zope3 (despite some of my recent seeming frustrations). What keeps python from having the same buzz that Java generated back in the day? What keeps Zope3 from having the same buzz that RoR gets? I think, really, it's just evangelism. I think Zope3 has too steep a learning curve (speaking as someone who is really pretty comfortable with Zope3 all things considered), but that can be remedied.

So, evangelists. I grew up in a frighteningly charismatic christian church, so i've seen my share of real, honest-to-god evangelists. Usually they have shocking hair, boistrous voices, and a disturbing glaze to their eyes that indicates they are not entirely "there." So I don't want to be one of those. Nor do I want to be "The Python Evangelist", if for no other reason than it would be amazingly presumptuous. I'm just not the most qualified person for the job.

But, I would *love* to be "one of the python evangelists" (notice the difference in case sensitivity). I'd like to be one of the Zope3 evangelists too, but there are some things I need to figure out about where I stand on the platform for that to be realistic. It does a lot of things that I love, and it's architecture is better than any i've seen (and i've looked). To that end, I was made aware that had listed for a Python guide. Last night I put together a quick "intro to python, with hello world" piece, and sent it off as my writing sample. I think it would be an interesting opportunity.

Assuming the likely case that other, more qualified people will apply for and get the Guide position, I've decided to post my writing sample here, just for kicks. Below is the writeup.

What is Python? At its most concise, Python is a programming language. It is an interactive, interpreted, dynamically typed, object-oriented language, with similarities to Perl, Ruby, and Java. But unless you know those languages already, or have a pretty good technology background, none of that will really mean much to you. So a better question might be "Why Python?"

Why Python? While there are plenty of potent technical reasons to suggest Python, the reasons that matter most to anyone as they start out with a new language usually fall into the following categories: simplicity of syntax, ease of portability, and library support for common tasks. Python's syntax makes it simple and enjoyable to pick up and use the language. This is important not only to keep you coding and enjoying the code you write, but is a serious consideration for maintainability and longevity of a program. Relatedly, the fact that there are implementations of Python for Windows, Linux/Unix, and MacOS make most Python code extremely portable. Finally, and most importantly as your skill with any language grows, the extensive core and third party libraries for Python mean that you are rarely forced to solve a problem "the hard way" (unless you want to, which Python certainly has the capacity to do), with support for everything from web, email, and database applications, to GUI and game programming.

That all sounds pretty good, but what about getting our hands dirty? No one ever learned anything worth knowing until they ended up doing it themselves. So let’s look at a little bit of code. If you don't have Python installed, there are a number of places to get it. Both the Python website and ActiveState have installers and instructions for Python, though if you're on a *nix box, the odds are you already have Python installed by the distribution. If not, there are downloads and instructions available for those platforms as well.

Once you have Python installed, let’s get to the interactive interpreter. In *nix you need only to type "python", and it will bring you to this shell. In Windows, you'll need to go to Start->Programs->Python->Python(command line). This should bring up a window with the Python version and build information at the top, and a line with >>> prompt below it.

Let's establish that we're in a friendly environment. Type "help" at the prompt. It should present you with a nice message on how to use the help command. Try it the first way, with no arguments. Type "help()". The parentheses tell the interpreter (the bit running in the background) that you want to call the command "help" with no arguments. It should present you with a few paragraphs letting you know some basic information about Python, and the help utility. Feel free to come back later, but for now, just type "quit".

Now it’s time to really type some code. Because "Hello World" is the most common first program to write in a new language, we'll only flout the convention a little by changing the phrase a bit. Type in the following:

>>> greeting = "hello from python!"

What did we do here? It seems simple, and, remarkably, it is simple. We merely created a variable named "greeting" and assigned a string to it by using the equal sign. A string is just any piece of text-like data, and "=" is just the assignment operator. It says "whatever is on the left hand side is now set, or assigned, to whatever value is on the right hand side." As you might expect, the value on the left hand side has to be a variable name. A variable is just an identifier that can have things assigned to it. We could just as easily have made "greeting" the variable "hi" or "good_day" or "helloWorld". (Purists will have to forgive me here. Technically, "greeting" is not a variable, but an object of type "string", with a __repr__ value that happens to look like string data, and the assignment operator helped us create the new object. But object theory would be getting a little ahead of ourselves, so we're going to keep saying variable for now).

So now we have a variable, and that variable has a value. While there are a nigh-limitless number of things we can do with a piece of text, let’s just, for now, print it out to the screen. Printing something out to the screen is as simple as using the "print" command. Just like we used the "help" command earlier, we will tell the interpreter that we intend to invoke the print command, but this time with an argument. At the prompt, type the following:

>>> print(greeting)

hello from python!

Notice that "hello from python!" showed up immediately below it? The "print" command told the interactive interpreter that you wanted to have the value assigned to the variable "greeting" echoed out to the screen. By placing parentheses around the variable in question, the command "print" knew that you intended that as the first and only argument.

There are a few things to know about the interactive interpreter to make life easier. The first is that any value typed at the prompt, if it is able to be meaningfully represented as a piece of text, will echo back out to the screen. Try it like this:

>>> greeting

'hello from python!'

Notice, however, a subtle difference between this output, and the output of the print command above. The output of the "print" command does not have quotes around it. It is, in fact, the direct result of "print". The second output is not actually the result of any specific Python command, but is rather a string representation of the data referenced by the variable "greeting". When you typed it directly, the interpreter showed you that it was, in fact, a piece of string data, by enclosing it in quotes. Pretty nifty. The other things to know about the interpreter are fairly straightforward, and are, in effect, just other ways of looking at what we've already seen. Try typing a mathematical expression at the prompt. Something exotic, like:

>>> 1+1


The interpreter isn't really doing anything spectacular here, though it is cool. The interactive interpreter knows that 1 is a numeric type. It also knows that when the plus sign "+" is used between two numeric types, what is expected of it is to add those values together in a numeric fashion, and present a result. To see a counter example, try this:

>>> "1" + "1"


Without getting into the more complicated reasons about why this is happening, by putting quotes around both "1"s, we've told the interpreter that we want these values to be treated like text. The plus sign in this case says to concatenate the values. Also notice that when we perform operations at the interactive prompt, any value that we don't assign back into a variable shows once, and disappears. While we may know that 1+1=2, the interpreter has no record of the value "2" anywhere. if we wanted that value for later use, we would have needed to assign it into some other variable, like so:

>>> sum = 1 + 1

>>> print(sum)


That's probably about enough for now. With what you've got you can already do some interesting things. Try out "help()" again, and look over what it says. At the "help>" prompt, type "str" (string, like the greeting we used) and look over some of the things you can do with strings. Try it with "int" too. Try to use other math operators (+, -, *, /) and see what happens. Try them on strings, see what happens when you multiply a letter by a number. I think you'll be pleasantly surprised.


Post a Comment

Links to this post:

Create a Link

<< Home