Thursday, August 4, 2011

Checking it twice : Playing with snakes 3

Currently house sitting again, for other family members, for other animals. Funny how being unemployed comes with perks of getting picked up and dropped off then told not to burn the house down. That aside I was thinking of continuing my playing with snakes tutorial today with basic loops before realizing just how python works. Like it or not I find this somewhat therapeutic to go over basic programing things so I'll be doing this till basic fighter is done. So with that, lists in python.

If you are familiar with other programing languages you may know the concept of arrays. Python has a similar concept that is effectively used in the same way but there are some very key differences that would take too long to explain. Effectively a list is a collection of variables, that are able to be called up through their 'position' in the list. Array deceleration, assignment, and use are slightly different than regular variables so I'll explain by showing. Please type the following.

alist=[1,2,3]
print alist[0]
print alist[1]
print alist[2]

A list is made, and then broken

Lists are signified by the Square brackets. Inside is each "item" in the list separated by commas. So this list is assigned to the alist varaible, and inisde the list is 3 items of 1,2, and 3. We are using intagers for this but anything can be put into a list, just like anything can be put into a variable. In fact you can mix types up in a list if you feel so inclined.  After that We start manually printing out the items in the list starting at index '0'.

To call an item from a list you place the index you want in square brackets immediately following it. That simple. Now you may wonder why this is worthwhile but when I get onto loops and other such controls you'll see.

Now this is where quite a few errors turn up. Humans like counting starting at 1, computers like to start at their location. I could get into the philosophy about this but that could take a while. For now remeber 0 is the starting position.

The other thing to note is what happens when we ask for the '4th' item in a list of 3. We get an out of index error. This is a bad thing, not as bad as it used to be. In the older days and in other systems when we ask outside of index we are getting unknown data that was right after the array or lists memory location. A pandoras box of unknown things that may be other variables, left over data from previous allocations, Other processes in worst case, or who knows what else. Nowadays we get errors and the program crashes to save us from ourselves. Because of the fact we would like to know the end there is a simple solution, we ask for how many items are in the list, we do this easily by typing the following.

len(alist)

By using the method len and passing along a list we will get the number of items in the list returned. With this we know if we ask for that number or higher we'll have bad things happen. Now another thing that is useful to know. We do not have to individually go through the list, we can in fact print it out in a format. Like with numbers we can use the str function with a list passed to have it printed out as a string. Try the following.

print str(alist)

With that you get a result much like if you were to set up a list. Now one last thing I would like to say about this before ending. Python has a method that produces a list of numbers, this is the range method. There are a few differnt ways to use the range method but we'll just use the most basic. Please type the following.

blist=range(3)
print str(blist)


Like this range produces X numbers starting at zero much like the index. So it will produce all the numbers from 0 to X-1 where X is the number you put into the method. You can do more fancy things with it but I'll show that latter.

With that out of the way I guess I can ask two things. Any of you out there have programing experience? The other question being what things do you find relaxing that seem odd?

6 comments:

  1. If I was to housesit there is a sad but very real chance I might fail to not burn the house down. I think I've played with lists before, I've played with variables at least, thanks again for a good tutorial ^^

    ReplyDelete
  2. The real kicker would be if you had to take care of someone's pet snake.

    ReplyDelete
  3. @mark
    They're basically cats with all the fun cat qualities removed. It's not the worst thing.

    ReplyDelete
  4. Not familiar enough with programming to understand most of this but maybe if I keep coming back I'll learn a thing or two.

    ReplyDelete
  5. That have inflamed desire in my breast. Following!

    ReplyDelete