Neko learns some new tricks!

You ask somebody for directions to reach the
nearest post office and he tells you:


Just go lrl.

What? lrl? What is that?

He tells you that it means “go left then right and left”!

You walk away smiling (or angry) … the fellow must surely
be a joker …

This is one small problem with human languages like English
- you can’t simply go ahead and invent words of your own, even
if you are the Queen of England!

But programming languages have no such restrictions.

Let’s say you want to make Neko move four places to the
right. You are tired of saying:

right()
right()
right()
right()

You want to invent a new “word” which has the meaning “go right
four times”. Here is how you do it. At the Python prompt, type:

def right4():
    right()
    right()
    right()
    right()

Here is what you will see on the screen:

>>> def right4():
...    right()
...    right()
...    right()
...    right()
...
>>>

You have to be a bit careful when typing this. After you
type the first line and hit the “enter” key, Python will show
three dots (…) in the next line. Now you have to type a
`tab’ (the tab key is above the Caps lock key towards the
left side of the keyboard) and only then type `right()’. Repeat
this process three time. Leave the last line (the line after
the last `right’ ) empty - simply type an enter and you
should once again see the symbols >>>.

What is the meaning of all this?

We are asking Python to “define” (thats what the “def”
is for) a new function called right4 - think of this
new function to be like a new word in English.

Now, any word should have a meaning, isn’t it?

The meaning of `right4′ is - just go right four times!

Once you have defined this new function, it can be used
exactly like the other functions which we know - right, up,
down, etc. Try typing this at the Python prompt:

right4()

And magic, you see Neko running four places right!

By the way, there is nothing special in the name `right4′.
You can call it `kangaroo’ and it will work perfectly well!

Programming languages, like human languages, have what is
called `Grammar’. In English, you can’t say:


He office to comes late.

You have to follow certain rules when writing sentences. Similarly,
when you want to create a new function in Python, the following
rules have to be strictly followed:

  • The first line should be like this - the word `def’, followed
    by the name of the new function, followed by brackets, followed by
    a `colon’ (:) sign.
  • When you hit enter after typing the first line, Python shows
    you three dots - this is to tell you that now you are in the
    `body’ of the function (that is, you are going to say what this
    new word actually means). Now, you should ALWAYS type a `tab’
    character and then only type anything else. The `tab’ key will
    insert a few `spaces’.
  • After you finish typing the `body’ of the function (in our
    case, four consecutive right’s), you should type an empty line.
    This will result in Python printing >>> once again - which
    means we have now succesfully added a new word! Whatever we type
    next will NOT be considered as the meaning of the new function
    `right4′.

Fun with new functions!

Now that you know how to teach new tricks to Neko, you can try
some fun experiments!

Teach Neko how to go ahead eight cells (OK, you will
now go ahead and define a new function right8 in which
you will call `right’ eight times … but you are
smart - so you will think of something better)!

Teach Neko to go down four cells. And similarly, to go up
and left four cells each.

Teach Neko to go around a square.

Neko thinks he is a Knight … so teach him how to
move like a Knight (Big Hint: think of Chess)

Invent a few other tricks of your own!

2 Comments

  1. Anish Bhaskaran
    Posted January 28, 2008 at 6:15 am | Permalink

    Just a simple thing : links on the left side are not in order.

    As I see it the link to this page comes third instead of 5th.

  2. Anish Bhaskaran
    Posted January 28, 2008 at 6:18 am | Permalink

    Just noticed the drop down menu has got it correct. Sorry I didn’t notice that the links under the Pages heading are alphabetically sorted. :)

    Great going sir.

Post a Comment

Your email is never published nor shared.