If you’re not a programmer, you won’t be able to understand half of this post. You could try though, but I’m not forcing anyone.

The gist of this whole post was that all I did was write a few painfully simple lines of Python, have an idea to run it through the parser, do so, and beam at myself because the parser did things right. I was that proud simply because I’ve had absolutely no prior knowledge of Python at all. If something like this has occurred to you before, let me know through commenting :D

Here’s my story in full. Be warned that it might slightly bore you in the first couple paragraphs. So yesterday I was just messing with my WordPress demo installation

I was writing this post on code snippets, just got done with my PHP snippet. Then I was considering adding in a couple more snippets for different languages: Java and Python.

Unfortunately, Potassium currently doesn’t have a custom <pre> background image for Python (though it has one for Java) so I finally decided to drop both languages because I was too darn lazy to think of a snippet for Java since I’m only a beginner in that language. Hell, I didn’t even know any of Python at all!

I read only a couple of pages of Python tutorials which I didn’t even understand… or at least I didn’t think I did. I wrote a basic function just for the snippets post… and that was exactly when I finally chose to drop the two languages from the post.

But, since I had Python and its command line interpreter installed (in order to use things like the really cool iPod shuffle Database Builder and the mighty fine Blender and others), I thought, why don’t I give my function and script a whirl in the Python interpreter? So I did.

It worked! It was a ridiculously simple function, constituting only six lines excluding whitespace and comments. With those, along with one more line just to test the function, since declaring just a function without doing anything else would only leave the interpreter quiet, it’s less than twenty lines.

Can you believe it? The only thing I knew about Python was that it’s free software and was installed on my PC and I heard it was easy to pick up, especially for beginning programmers. And suddenly I read barely three pages, proceeded to throw in less than 20 lines of code into a file and the interpreter carried out my instruction duly.

If you’re curious, here’s my script, a function that just calculates the factorial of any integer (the idea was taken from a Java intro tutorial which covered a factorial class). All of it. Verbatim. Straight from my file.

# Factorial function
def factorial(n):

    result = 1

    # To calculate the factorial of a number, take
    # all integers from 1 to itself and multiply them
    for i in range(n):
        j = i + 1
        result *= j

    return result

# Test our function with 100 integers
for i in range(100):
    print "%d! = %d" % (i, factorial(i))

print "done"

Well I cheated. I got to know the syntax of the last line (which incidentally does work similarly to PHP’s printf() function) by looking at the iPod shuffle DB Builder’s source code :P But that was all.

And yes, as of now I know Potassium still doesn’t recognize Python snippets. But it will… probably in Potassium 1.0.3 or even 1.1.0 should I deem fit.

Again, if something like my story has happened to you before, go ahead and leave a comment.

Back to top