Variables as a index
Getting values in and out of an array is all about the index! You can use any expression to represent the index. Python will evaluate it and substitute the answer as the index. Look at the code below –
numbers = [3,6,1,9,2] print numbers numbers[1+1] = 20 print numbers x = 3 numbers[x] = 30 print numbers numbers[x - x] = 40 print numbers
Can you see why the values change the way they do? Using variables instead of a hard coded number (known as a literal) is one of the reasons arrays are so powerful. Add the following two lines of code to the above code sample to move on.
numbers[len(numbers)-1] = 99 print numbers
Output: (clear)











