|
1 |
| -TODO: add information on tuples concept |
| 1 | +In Python, a [tuple](https://github.com/exercism/v3/blob/master/languages/python/reference/concepts/builtin_types/tuple.md) is an immutable collection of items in _sequence_. Like most collections (_see the built-ins [`list`](https://github.com/exercism/v3/blob/master/languages/python/reference/concepts/builtin_types/list.md), [`dict`](https://github.com/exercism/v3/blob/master/languages/python/reference/concepts/builtin_types/dict.md) and [`set`](https://github.com/exercism/v3/blob/master/languages/python/reference/concepts/builtin_types/set.md)_), tuples can hold any (or multiple) data type(s) -- including other tuples. Like any [sequence](https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range), items are referenced by 0-based index number, and can be copied in whole or in part via _slice notation_. Tuples support all [common sequence operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations). |
| 2 | + |
| 3 | +Tuples take up very little memory space compared to other collection types and have constant (_O(1)_) access time. However, they cannot be resized, sorted, or altered once created, so are less flexible when frequent changes or updates to data are needed. |
| 4 | + |
| 5 | +## Tuple Construction |
| 6 | + |
| 7 | +Tuples can be formed in multiple ways, using either the `tuple` class constructor or the `tuple` literal declaration. |
| 8 | + |
| 9 | +### Using the `tuple()` constructor empty or with an _iterable_: |
| 10 | + |
| 11 | +```python |
| 12 | +>>> no_elements = tuple() |
| 13 | +() |
| 14 | + |
| 15 | +#the string elements (characters) are iterated through and added to the tuple |
| 16 | +>>> multiple_elements_string = tuple("Timbuktu") |
| 17 | +('T', 'i', 'm', 'b', 'u', 'k', 't', 'u') |
| 18 | + |
| 19 | +>>> multiple_elements_list = tuple(["Parrot", "Bird", 334782]) |
| 20 | +("Parrot", "Bird", 334782) |
| 21 | + |
| 22 | +>>> multiple_elements_set = tuple({2, 3, 5, 7, 11}) |
| 23 | +(2,3,5,7,11) |
| 24 | + |
| 25 | + |
| 26 | +""" |
| 27 | +The iteration default for dictionaries is over the keys. |
| 28 | +To include both keys and values in a tuple made from a dictionary, use dict.items(), |
| 29 | +which returns a list of (key, value) tuples. |
| 30 | +""" |
| 31 | +source_data = {"fish": "gold", "monkey": "brown"} |
| 32 | +>>> multiple_elements_dict_1 = tuple(source_data) |
| 33 | +('fish', 'monkey') |
| 34 | + |
| 35 | +>>> multiple_elements_dict_2 = tuple(source_data.items()) |
| 36 | +(('fish', 'gold'), ('monkey', 'brown')) |
| 37 | + |
| 38 | + |
| 39 | +""" |
| 40 | +because the tuple constructor only takes _iterables_ (or nothing), it is much easier to create |
| 41 | + a one-tuple via the literal method. |
| 42 | +""" |
| 43 | +>>> one_element = tuple([16]) |
| 44 | +(16,) |
| 45 | + |
| 46 | +``` |
| 47 | + |
| 48 | +#### Declaring a tuple as a _literal_ : |
| 49 | + |
| 50 | +```python |
| 51 | +>>> no_elements = () |
| 52 | +() |
| 53 | + |
| 54 | +>>> one_element = ("Guava",) |
| 55 | +("Guava",) |
| 56 | + |
| 57 | +>>> elements_separated_with_commas = "Parrot", "Bird", 334782 |
| 58 | +("Parrot", "Bird", 334782) |
| 59 | + |
| 60 | +>>> elements_with_commas_and_parentheses = ("Triangle", 60, 60, 60) |
| 61 | +("Triangle", 60, 60, 60) |
| 62 | + |
| 63 | +>>> nested_data_structures = ({"fish": "gold", "monkey": "brown", "parrot" : "grey"}, ("fish", "mammal", "bird")) |
| 64 | +({"fish": "gold", "monkey": "brown", "parrot" : "grey"}, ("fish", "mammal", "bird")) |
| 65 | + |
| 66 | +#using the plus + operator unpacks each tuple and creates a new tuple. |
| 67 | +>>> new_via_concatenate = ("George", 5) + ("cat", "Tabby") |
| 68 | +("George", 5, "cat", "Tabby") |
| 69 | + |
| 70 | +#likewise, using the multiplication operator * is the equvilent of using + n times |
| 71 | +>>> first_group = ("cat", "dog", "elephant") |
| 72 | + |
| 73 | +>>> multiplied_group = first_group * 3 |
| 74 | +('cat', 'dog', 'elephant', 'cat', 'dog', 'elephant', 'cat', 'dog', 'elephant') |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +Note that generally parentheses are not _required_ to create a `tuple` literal - only commas. Parentheses are required in cases of ambiguity, such as an empty or one-item tuple or where a function takes a tuple as an argument. |
| 79 | + |
| 80 | +## Tuples as related information |
| 81 | + |
| 82 | +Tuples are often used as _records_ containing heterogeneous data that is _organizationally_ or _conceptually_ related and treated as a single unit of information. |
| 83 | + |
| 84 | +```python |
| 85 | + |
| 86 | +>>> student_info = ("Alyssa", "grade 3", "female", 8 ) |
| 87 | + |
| 88 | +``` |
| 89 | + |
| 90 | +Tuples are also used when homogeneous immutable sequences of data are needed for [`hashability`](https://docs.python.org/3/glossary.html#hashable), storage in a set, or creation of keys in a dictionary. |
| 91 | + |
| 92 | +Note that while tuples are in most cases _immutable_, because they can contain _any_ data structure or object they can become mutable if any of their elements is a _mutable type_. Using a mutable data type within a tuple will make the enclosing tuple un-hashable. |
| 93 | + |
| 94 | +```python |
| 95 | + |
| 96 | +>>> cmyk_color_map = { |
| 97 | + (.69, .3, .48, .1) : ("Teal 700", (59, 178, 146), 0x3BB292), |
| 98 | + (0, .5, 1, 0) : ("Pantone 151", (247, 127, 1), 0xF77F01), |
| 99 | + (.37, .89, 0, .44) : ("Pantone 267", (89, 16, 142), 0x59108E), |
| 100 | + (0, 1, .46, .45) : ("Pantone 228", (140, 0, 76), 0x8C004C) |
| 101 | + } |
| 102 | + |
| 103 | +>>>> unique_rgb_colors = { |
| 104 | + (59, 178, 146), |
| 105 | + (247, 127, 1), |
| 106 | + (89, 16, 142), |
| 107 | + (140, 0, 76), |
| 108 | + (76, 0, 140) |
| 109 | + } |
| 110 | + |
| 111 | +>>> teal_700 = hash((59, 178, 146)) |
| 112 | + |
| 113 | +>>> teal_700 = hash(("Pantone 228", [(140, 0, 76), 0x8C004C])) |
| 114 | +Traceback (most recent call last): |
| 115 | + File "<stdin>", line 1, in <module> |
| 116 | +TypeError: unhashable type: 'list' |
| 117 | + |
| 118 | +``` |
| 119 | + |
| 120 | +## Accessing data inside a tuple |
| 121 | + |
| 122 | +Items inside tuples (_like the sequence types `string` and `list`_), can be accessed via 0-based index and _bracket notation_. Indexes can be from **`left`** --> **`right`** (_starting at zero_) or **`right`** --> **`left`** (_starting at -1_). |
| 123 | + |
| 124 | +Items inside tuples can also be _iterated over_ in a loop using `for item in` syntax. |
| 125 | + |
| 126 | +```python |
| 127 | + |
| 128 | +>>> student_info = ("Alyssa", "grade 3", "female", 8 ) |
| 129 | + |
| 130 | +#name is at index 0 or index -4 |
| 131 | +>>> student_name = student_info[0] |
| 132 | +Alyssa |
| 133 | + |
| 134 | +>>> student_name = student_info[-4] |
| 135 | +Alyssa |
| 136 | + |
| 137 | +#grade is at index 1 or index -3 |
| 138 | +>>> student_grade = student_info[1] |
| 139 | +'grade 3' |
| 140 | + |
| 141 | +>>> student_grade = student_info[-3] |
| 142 | +'grade 3' |
| 143 | + |
| 144 | +#age is at index 3 or index -1 |
| 145 | +>>> student_age_1 = student_info[3] |
| 146 | +8 |
| 147 | + |
| 148 | +>>> student_age_2 = student_info[-1] |
| 149 | +8 |
| 150 | + |
| 151 | + |
| 152 | +>>> for item in student_info: |
| 153 | +>>> print(item) |
| 154 | + |
| 155 | +.... |
| 156 | +Alyssa |
| 157 | +grade 3 |
| 158 | +female |
| 159 | +8 |
| 160 | + |
| 161 | +``` |
| 162 | + |
| 163 | +## Extended tuples and related data types |
| 164 | + |
| 165 | +Tuples are often used as _records_, but the data inside them can only be accessed via _position_/_index_. The [`namedtuple()`](https://docs.python.org/3/library/collections.html#collections.namedtuple) class in the [`collections`](https://docs.python.org/3/library/collections.html#module-collections) module extends basic tuple functionality to allow access of elements by _name_. Additionally, users can adapt a [`dataclass`](https://docs.python.org/3/library/dataclasses.html) to provide similar named attribute functionality, with a some [pros and cons](https://stackoverflow.com/questions/51671699/data-classes-vs-typing-namedtuple-primary-use-cases). |
0 commit comments