############# Basic Notes on Tuples and Arrays # Previously we learned that Python has a type of expression called "tuples", # which are just sequences of expressions grouped into a single expression. m = (3,5,7,9) # m is a tuple of integers print len(m) # prints 4, which is the length of the tuple. # we can also access the individual members of the tuple m by specifying an # *index*. The first element of the tuple is the zeroth element, and the # last element has index len(m)-1: print m[0] # prints 3, the first element print m[3] # prints 9, the last element. ### Arrays # Arrays are similar to tuples with one big exception. Elements of an # array can CHANGE. Syntactically, arrays are indicated by square brackets # instead of parentheses. p = ["abc",23] # arrays (and tuples) can contain elements of different type. n = [2,3,5,7,11] # typical loop that visits each element of the array: i = 0 # starting index while i<=len(n)-1: # same as i=0: print s[i], i -= 1 # while loop will print s backwards ## But strings are not arrays because you can't change a character in a ## string: s[0] = 'z' will give error. #### Note on for loops. #A while loop can always be used to "traverse" an array. However, there is #another convenient form that is sometimes used: A = [2,3,5,7,11] for x in A: print x, #for loop # This will print 2 3 4 7 11 # Note that it is the CONTENTS of the array, not the indices, that are # printed. Therefore, there is no need to increment x (no x+=1) inside # the body of the for loop. The update part is automated by the for-loop. # In contrast, what does the following print: x = 0 while x