Shared publicly  - 
 
(EDIT: This is a long post)

I am spoiled rotten.

Starting around my second semester at University of Central Arkansas I began teaching myself #Python. Coming from some experience in PHP, and a few semesters of C++, and writing a single guessing game in Perl many years prior, I thought it was an interesting language. The importance of white space threw me at first, but it wasn't difficult to get used to - I was usually fastidious about formatting my C/PHP code anyway.
I began to use Python (especially the interactive interpreter) in classes at school, and it was helpful when trying to learn visualize data structures, computer architecture, and a variety of other concepts. I began contributing to the tutor@python.org list, and answering as many questions as I could (as evidenced by my placing in the top 10 contributors to the list for a few years). Through that venue I was able to learn more about Python because the act of researching questions well enough to answer them forced me to learn about topics that I was unfamiliar with.
When faced with an assignment at school, if I had the option I would do it in Python. Sometimes I would do it in Python first anyway, and simply translate it to the required language.

The power and expressiveness of the language was a huge advantage. Orthogonality describes the ability to combine a small number of features together in a large number of ways. Lisp is an example of a highly orthogonal language - when you boil it down it consists of an operator and a list of values to apply that operator to. Adding numbers together is as easy as (+ 1 2 3). Yet in Lisp you can do some pretty amazing things by combining functions/operators and lists of arguments.
Python is also highly orthogonal. In the Python 3.2 interpreter I have installed:

>>> import keyword

>>> len(keyword.kwlist)

33

Thirty-three keywords. That's it. That's an absurdly low number. Yet those 33 keywords can be combined to perform insanely powerful tasks. Dropbox. Google App Engine. Django. Zope. Pylons. Pygame. Blender. EVE Online. Myst V. Industrial Light & Magic. These are just some of the few companies or programs that use Python (see: http://wiki.python.org/moin/OrganizationsUsingPython) to perform complicated tasks.

I think the biggest thing about Python is that it's a language that gets out of your way. To illustrate what I mean, let me give you an example. Say, just for kicks and giggles, I want to sum up a bunch of numbers but I don't want to switch over to my calculator program. If I'm using Python, this is all I have to do:

print(sum((1,2,3))

and I'm done.

I suppose you could argue that sum isn't a keyword, even if it is a built-in function, so you can't really compare that against other languages. Well, fine, let's take a look at what it would take to write that function in Python and Visual Basic.

---------------------------
Python:

def sum(mylist):
total = 0
for num in mylist:
total += num
return total

Visual Basic - without type inference:

Function Sum(ByVal mylist as IEnumerable) as Integer
Dim total as Integer = 0
For Each num as Integer In mylist
total += num
Next
Return total
End Function

Visual Basic - with type inference:

Function Sum(ByVal mylist as IEnumerable) as Integer
Dim total = 0
For Each num In mylist
total += num
Next
Return total
End Function
---------------------------

Notice first of all that there is really only one way to write the function in Python. I can't think of any other (sane) way that you might want to write the sum function - unless you were to include documentation, but that's beside the point here.

Someone who has no experience programming could probably take a look at the first VB function and have a fair idea what's going on, but I suspect that it wouldn't take much to understand the Python version either. Let's take it line by line as if we had no real programming knowledge, but a good grasp of the English language and a good understanding of mathematics.

---------------------------

def sum(mylist):

Well, I'm not sure what def means. It could either have something to do with hip hop and urban music, but I doubt that. Oh, definition starts with def - maybe it's short for a definition of something. And since sum comes next, maybe I'm defining sum. I guess mylist must be some sort of list - probably something that I can sum up. And I can understand that the colon means that whatever follows is the definition - that's what I see in my dictionary anyways. Cool, I've got this. Now for the VB version.
Function Sum(ByVal mylist As IEnumerable) as Integer

Function? What does that mean? Am I using a function? Making one? I'm not really sure. I guess it has something to do with adding, since I've got Sum there. I don't know what ByVal means, but I can see mylist. mylist As IEnumerable? Well, I know that enumerable means it's some type of collection whose values I can look at. So mylist and IEnumerable - do those mean the same thing? I'm not really sure. Well, what's next? As Integer. Huh... well maybe that's what sum is? This programming thing sure is hard.

total = 0

Totals, hey, I know those. And I know that this total must equal zero. Cool.

Dim total As Integer = 0

Dim? Like dimension? Oh, hey, as integer, maybe I'm saying this number is an integer? Oh, ok, I see what was going on up there I think. As for this dimension thing, maybe the dimension of total is 0? I guess that makes sense....

for num in mylist:

Well, I guess we're going to look at the numbers in this list. That makes sense.

For Each num As Integer In mylist

Ah, yeah there's that "As Integer" thing again, so num must be an integer, and mylist an IEnumerable, whatever that I means. And maybe Sum is an integer too? I'm still not sure what that function thing means. For Each, well we're definitely going to look at each number in mylist.

total += num (VB and Python)

Wait, what? Plus equals? What in the world does that mean? Better that just a regular equals? Oh wait, this is summing, maybe I'm adding the value of num to the total? I bet that's it. (same for vb)

Next (VB only)

Well, I guess that means we're looking at the next value in the list now? I guess we have to say that when we want to look at the next value. I think I got it.

R(r)eturn total (both versions)

Ah, well this must be how we get total. Since it says I guess we'll be going back somewhere, or maybe giving total back to wherever we came from. Sounds good, since we're adding the values I guess someone wants them.

End Function (VB only

Oh, maybe that whole thing was a Function! Since functions are things that something can do, I guess this thing is adding numbers and returning them, so that's something that I can do now. Well, that makes more sense.

---------------------------

Notice the extra complexity contained in the VB program? Even if you don't need to understand them completely, you need to be exposed to Functions, ByVal, Integers (types), continuation (of course our mythical student has the wrong idea about what Next really means).

With Python, even though you're using all of these things, you don't have to explicitly care about them. We naturally use indentation to denote relationship (think of the last outline you wrote), and conventionally use the colon (:) to denote an even more specific type of relationship.

Now, this was an extremely simple example. You learn about adding lists of numbers as young children. When it gets more complicated, Python doesn't get much more complicated. It provides some terse syntax, but when you understand something as simple as () is the equivalent of tuple(), [] is the equivalent of list(), and {} can be used for set() or dict() (a dictionary), then you're pretty well on your way to understanding enough to be dangerous. It allows you to write things in Python without being overly concerned about the syntax. Because it is so terse, you can effectively ignore the syntax and concentrate on what you want to do with the data that you're working with.

This is a fairly complicated example, but it illustrates what I mean. In this particular case, I wanted to compare the data contained in a couple of csv files. This is a quick one-off script that I needed for a specific task and for what it does it excels. But I shudder to think at what you would have to write to create the same tool in Java, C(anything), or VB:

#######################

import csv
import sys
from itertools import groupby

reader = csv.reader(open('ours.csv', 'r'), delimiter=',', quotechar='"')
our_dat = [row for row in reader][9:]

reader = csv.reader(open('his.csv', 'r'), delimiter=',', quotechar='"')
his_dat = [row for row in reader][9:]

ours = sorted((row[2].strip(), float(row[8]), float(row[10])) for row in our_dat)
his = sorted((row[2].strip(), float(row[8]), float(row[10])) for row in his_dat)

def contained(row, data):
for _row in data:
if (row[2].strip() == _row[2].strip() and
row[3].strip() == _row[3].strip() and
row[4].strip() == _row[4].strip() and
[x.strip() for x in row[7:11]] == [x.strip() for x in row[7:11]]):
return True


f = open('diff2.txt', 'w')
print("Ours", file=f)
for row in our_dat:
if not contained(row, his_dat):
print("{2}, {3}, {4}, {7}, {8}, {9}, {10}, {11}_".format(*row), file=f)

print('\n' * 3, 'His', file=f)
for row in his_dat:
if not contained(row, our_dat):
print("{2}, {3}, {4}, {7}, {8}, {9}, {10}, {11}_".format(*row), file=f)

f.close()


f = open('diffs.txt', 'w')

count = 0
total_b = 0
for x in ours:
if his.count(x) < 1:
print(x, his.count(x), ours.count(x), file=f)
count += 1
total_b += x[1]

print('\n'*3 ,"His: ", file=f)

count = 0
total_a = 0
for x in his:
if ours.count(x) < 1:
print(x, his.count(x), ours.count(x), file=f)
count += 1
total_a += x[1]

a, b =sum(x[1] for x in ours) , sum(x[1] for x in his)
print(a)
print(b)
print(a - b)
print(total_b - total_a)
#######################

Sixty-one lines, several of them packed with a load of operations that would take 10 lines or more in one of the "conventional" languages. And when you're creating larger, even more complicated programs, writing in a language that allows you to write an algorithm for your problem rather than writing an algorithm for your language is a huge advantage.

So I'm having to re-adjust the way I think, back to programming my algorithm for my language instead of my problem. I'm getting better, but Python still spoils me.

And you know what? I'm fine with that.
1
Add a comment...