In Python, tuples are pretty amazing. They are iterable data containers that, once created, cannot be mutated.* To create a tuple you must use the tuple constructor, which takes one argument, or a parentheses with a comma. The comma is necessary. Without a comma, it's not a tuple.
>>> demo = (1, 2, 3)
>>> type(demo)
<class 'tuple'>
>>> demo = (1,)
>>> demo2 = (1,)
>>> type(demo2)
<class 'tuple'>
>>> demo3 = tuple((1, 2, 3))
>>> demo3 = tuple([1, 2, 3])
>>> type(demo3)
<class 'tuple'>
>>> demo4 = (1)
>>> type(demo4)
<class 'int'>
Iterable data set methods like .pop and .append won't work on tuples. If you try to add or remove something from a tuple, you will get an error similar to this:
>>> demo = (("one", 1), ("two", 2), ("three", 3))
>>> demo
(('one', 1), ('two', 2), ('three', 3))
>>> demo.append(("four", 4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
Tuples are lightweight, which means that they do not take up a lot of memory. They are ordered and therefore indexable. You can use methods like .slice or .count on them. You can also rapidly assign the values of a tuple to variables:
>>> one, two, three = demo
>>> one
('one', 1)
>>> two
('two', 2)
>>> three
('three', 3)
>>> crazy, right = one
>>> crazy
'one'
>>> right
1
Just make sure that you assign the exact number of variables as there are indexes in the tuple or you will get a 'ValueError':
>>> four, five = demo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
>>> six, seven, eight, nine = demo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 4, got 3)
You can compare the values of tuples. Tuples get compared position by position.
>>> one = tuple((1, 2, 3))
>>> two = tuple((1, 2, 3))
>>> three = tuple((0, 5, 7))
>>> four = tuple((4, 9, 1))
>>> one == two
True
>>> one > three
True
>>> one > four
False
So you can create a tuple full of data and have an unalterable data set that can ensure consistency and predictability in your app...
For the most part. Remember that asterisk from earlier.
While you cannot append to a tuple, for some reason you can concatenate tuples together:
# With tuple constructor
>>> demo += tuple((("four", 4), ("five", 5)))
>>> demo
(('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5))
>>> omed = (("six", 6), ("seven", 7))
# With assignment operator
>>> demo += omed
>>> demo
(('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7))
But you cannot subtract from a tuple:
>>> demo -= demo[-1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -=: 'tuple' and 'tuple'
>>> demo - ("seven", 7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
Also, the "immutability" of tuples depends on the types of data stored in a tuple. Tuples by nature are heterogeneous. Meaning they can store many different data types, at the same time if need be. So if you store a data type that is mutable, like a list or dict, then you can change the values contained in the mutable types within the tuple. Let's repurpose the 'demo' variable for this example:
>>> demo = (('one', 1), [1,2,3], "string", 23, {"key": "value"})
>>> demo
(('one', 1), [1, 2, 3], 'string', 23, {'key': 'value'})
>>> demo[1][0] = 99
>>> demo[4]["key"] = "changed"
>>> demo[4]["new_key"] = "new_value"
>>> demo
(('one', 1), [99, 2, 3], 'string', 23, {'key': 'changed', 'new_key': 'new_value'})
But tuples will not let you replace mutable data types, even with the same type:
>>> demo[1] = [6, 7, 8]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ATypeError: 'tuple' object does not support item assignment
But if you really wanted to adjust a tuple, you could. Simply turn the tuple into a list, use any number of list methods to make any adjustments desired, and then turn the list back into a tuple. That way you can make use of the tuples immutablilty, but manipulate the data in your app as you wish.
>>> demo = (("one", 1), ("two", 2), ("three", 3))
>>> temp = list(demo)
>>> temp.pop(0)
('one', 1)
>>> temp.append(("get", "it?"))
>>> temp
[('two', 2), ('three', 3), ('get', 'it?')]
>>> demo = tuple(temp)
>>> demo
(('two', 2), ('three', 3), ('get', 'it?'))
As you can see, there is so much you can do with a tuple. In my opinion, tuples are a very useful data set when need some consistency, yet versatility, with data for your app without having to interact with a database. But this is just scratching the surface of what you can do with tuples. For a deeper dive on tuples, check out this site: