Mrtn's blog

I have NO idea what I am doing...

Posts tagged 1

0 notes &

Learning Python in 5 days - Day 1

So here I am, on a mission to learn Python.

As all good citizens, I start out by simply DDG’ing (DDG=DuckDuckGo which is kinda Google for hipsters) anyhow, I start by searching for “Python”, and I find the official website for the Python programming language This reveales that there are two different versions of Python available for download (2.7.3 & 3.2.3). At first I’m a bit confuzzled (confused+puzzled, from the film Mary and Max (2009)) at first, but the Python website says;

Start with one of these versions for learning Python or if you want the most stability;
they're both considered stable production releases.
If you don't know which version to use, start with Python 2.7;
more existing third party software is compatible with Python 2 than Python 3 right now.

"If you don’t know which version to use, start with Python 2.7", I see. Now, meanwhile I go ahead and download 2.7, I’m going to dig a little bit deeper regarding the difference between the two versions.

There is actually written a page about choosing between the two on the Python website, to be more accurate here, I’m not going to go into detail about the changes (you can read the “What’s new in Python 3.0" if you want too) but as they write it;

Short version: Python 2.x is the status quo, Python 3.x is the present and future of the language.

Okay, since I’m just learning, I should technically be learning the newest and best in the language. But considering code-samples and most guidance on the internet would be using 2.x, I might as well go with that. I’m just going to learn the basics anyway, rite?

So what is the first thing I start out with when trying to learn a new language? I’ll head over to Wikipedia and simply look up how the syntax is. Wikipedia even got a whole page dedicated to “Python syntax and semantics”, perfect!

First of all, variable declaration:

From other languages I’m used to, declaring variables is done much like these:

int number = 10; // C#/ActionScript 3 *shrugs*/ tons of others
$number = (int) 10; // PHP

But Python is a bit different. You don’t need variable-declaration in Python (whaat?!). Found through StackOverflow is this: "Other languages has variables", that text is a part of the “Code Like A Pythonista: Idiomatic Python" written by David Goodger at Python. The text pretty much defines how you should write your code.

Reading the text written by David Goodger I think I’ve grasped the basics of how to at least write “pretty” code.

Okay, we’ve scraped the variable deceleration, what’s next? Loops and statements!


How I usually do them:

    echo/print/println x;

How Python does them:

    print x
    x=x+1 ''' Python doesn't support x++/x-- '''


How I usually do them:

// PHP
for(var i=0;i<10){
     echo i;

How Python does them:

for i in range(1, 5):
    print i
    ''' done '''


How I usually do them:

    print/echo/etc "X == Y";
    // else

How Python does them:

if x == y:
    print "x == y";
    ''' else '''


How I usually do them:

// PHP
    case 0:
         // do this
    case 1:
         // do this
         // if x != 0 or 1

How Python does them:

Python doesn’t! Switch-cases isn’t a part of Python, but there are workarounds, but switch-statements shouldn’t really be needed.

Now, I think that’s close to everything I want to figure out when it comes to statements. Onward to… Functions!

From what I’m used to, functions are basically written like:

function function_name(variables, it, takes){
    return variables + it + takes;
// and is called using
function(x, y, z);

But Python works a little bit different, instead of all the parentheses and curly brackets, we simply do:

def function_name(variables, it, takes):
    return variables + it + takes
// and is called using
function(x, y, z)

Simple, right?!

But how about classes? Simple, they are written like this:

class Foo:
    def bar(self, x, y, z):
        print x+y+z
// and then we just initialize the Foo class:
x= Foo();
// then we call the bar function, y, z)

That wasn’t hard either! Seems good (and easy)!

Now that I got most of that out of the way, I should be able to start creating small program that actually do stuff! With some help from my friend DuckDuckGo we should be able to start on that XML-parser! I’ll save that up for the next post :D

I’ll wrap things up with a quote from “Code like a Pythonista: Idiomatic Python”:

Don’t reinvent the wheel

Before writing any code,

  • Check Python’s standard library.
  • Check the Python Package Index (the “Cheese Stop”).
  • Search the web. Google is your friend.

And the soundtrack for this post is Python by Dodge and Fuski, I do love me some dubstep!

Also, I think there is a need for a little disclaimer here. Some of the material above might not be 100% correct, I have tested all the code though, and the Python Shell runs them just fine. But if you find anything that’s wrong just drop a comment below and I’ll look into it!

Filed under Python programming Learning Python in 5 days day 1 code