Wednesday, October 1, 2008

Chapter 3 Continued..

The type of bracket you use is very important. You have seen us use () when we are inserting data. When you print off the data inside the brackets like  monkey = ("banana", "smoothie", "tastes good"), you get everything inside of that bracket. In Python, you can reference the values inside.

print "I want to access the second value which is %s" %monkey[1]
I want to access the second value which is smoothie

Remember that the counter starts at 0 , so if you wanted to get the word banana the ending would say %monkey[0] .

If you forget how many values are inside the bracket, you can type in the len to find out how many is in there. An example is print "%d" % len(monkey) which will spit out the result 3

You actually access a tuple through another tuple. Don't try to picture it now, it is better to look at it in layers.

monkey = ("banana", "smoothie", "tastes good")
cheetah = (monkey, "fast")
print "%s" % cheetah [0][0]
    banana
print "%s" %cheetah[0][1]
    smoothie

One specific rule involving tuples is that if you have only one element, you must follow it will a comma.    monkey = ("banana",)


No comments: