As you can see, a tuple is another "container" variable,
which can contain one or more values. In the example above, "CreepyCrawlies" is a tuple containing the values "spiders", "ants" and "lizards".
Unlike lists, which are enclosed within square braces, tuples are enclosed within parentheses, with values separated by commas. The various elements of the tuple are accessed via an index number, with the first element starting at zero. So, to access the element "spiders", you would use the notation
- essentially, the tuple name followed by the index number
enclosed within square braces. You'll remember this "zero-based indexing" from lists.
Defining a tuple is simple - simply assign values (enclosed in square braces) to a variable, as illustrated below:
>>> pasta = ("macaroni", "spaghetti", "lasagne", "fettucine")
The rules for choosing a tuple name are the same as those for
any other Python variable - it must begin with a letter, and can optionally be followed by more letters and numbers. Like a list, a tuple can mix different types of elements - the following example creates a tuple containing strings, numbers, lists and even another tuple.