Page 167 -
P. 167

create and extend an array


           Python gives you arrays with lists                         Python coders typically use the word “array" to
                                                                      more correctly refer to a list that contains only

           Sometimes, different programming languages have different   data of one type, like a bunch of strings or a
                                                                      bunch of numbers. And Python comes with a built-
           names for roughly the same thing. For example, in Python most   in technology called “array” for just that purpose.
           programmers think array when they are actually using a Python
           list. For our purposes, think of Python lists and arrays as the   However, as lists are very similar and much more
           essentially same thing.                                    flexible, we prefer to use them, so you don't need
                                                                      to worry about this distinction for now.
          You create an array in Python like this:
                                                                  my_words

              my_words = ["Dudes", "and"]
                                                                         "Dudes"                 "and"
           Give the              Assign a list                                                     1
           array a name.         of values to it.                           0



           This creates a two-item array and gives it the name my_words.
           To look at individual items in the array, index the element
           required:
                                  >>> print(my_words[0])
                                  Dudes
                                  >>> print(my_words[1])
                                  and

          You can read individual pieces of data from inside the array using an
           index, just like you read individual characters from inside a string.

          As with strings, the index for the first piece of data is 0. The second
           piece has index 1, and so on.



           Arrays can be extended

           But what if you need to add some extra information to an array?
           Like strings, arrays come with a bunch of built-in methods. Use the
           append() method to add an extra element onto the end of the array:

                                  >>> my_words.append("Bettys")
                                  >>> print(my_words[2])
                                  Bettys                                                   The array has grown
                                                                                           by one data element.
                               my_words


                                       "Dudes"                 "and"              "Bettys"

                                          0                      1                      2
           132    Chapter 4
   162   163   164   165   166   167   168   169   170   171   172