Python Concepts/Tuples
Objective
[edit | edit source]
|
Lesson
[edit | edit source]
The Python Tuple[edit | edit source]In Python, a tuple is an immutable sequence. This means that a tuple is similar to a list, except you cannot dynamically modify the tuple itself. Once it's created, it can't be changed or modified. The way the tuple is stored in memory will also be important later in the course. To create a tuple, you create a group of items separated by a comma ( >>> spam = 1, 2, 3
>>> spam
(1, 2, 3)
>>> "A", "B", "C", "D"
('A', 'B', 'C', 'D')
>>> True, False, True, True, False
(True, False, True, True, False)
>>> (1, 3, "G", True)
(1, 3, 'G', True)
>>> bacon = 1,
>>> bacon
(1,)
>>> spam = 1, 2, 3,
>>> spam
(1, 2, 3)
Also note that an empty tuple is simply a pair of parentheses. To review: >>> a = () ; a ; isinstance(a, tuple)
()
True
>>> a = (1) ; a ; isinstance(a, tuple)
1
False
>>> a = (1,) ; a ; isinstance(a, tuple)
(1,)
True
>>> a = (1,2) ; a ; isinstance(a, tuple)
(1, 2)
True
>>> a = (1,2,) ; a ; isinstance(a, tuple)
(1, 2)
True
>>>
Tuple Indexing[edit | edit source]Tuples, like lists and strings, follow the same standard for indexing. Indexing starts at 0 for the first item and -1 for the start of the last item. Examples are shown below. >>> ("A", "B", "C", "D", "E", "F")[0]
'A'
>>> ("A", "B", "C", "D", "E", "F")[1]
'B'
>>> ("A", "B", "C", "D", "E", "F")[-1]
'F'
>>> ("A", "B", "C", "D", "E", "F")[-2]
'E'
Tuple Slicing[edit | edit source]Like other common sequences, tuples can be sliced. The standard slicing is exactly like slicing taught in previous lessons: >>> ("A", "B", "C", "D", "E", "F")[1:]
('B', 'C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[:4]
('A', 'B', 'C', 'D')
>>> ("A", "B", "C", "D", "E", "F")[-4:]
('C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[0:]
('A', 'B', 'C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[0:2]
('A', 'B')
>>> ("A", "B", "C", "D", "E", "F")[11:]
()
>>> ("A", "B", "C", "D", "E", "F")[:-19]
()
>>> ("A", "B", "C", "D", "E", "F")[::]
('A', 'B', 'C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[::1]
('A', 'B', 'C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[::2]
('A', 'C', 'E')
>>> ("A", "B", "C", "D", "E", "F")[::3]
('A', 'D')
>>> ("A", "B", "C", "D", "E", "F")[::-1]
('F', 'E', 'D', 'C', 'B', 'A')
>>> ("A", "B", "C", "D", "E", "F")[::-2]
('F', 'D', 'B')
>>> ("A", "B", "C", "D", "E", "F")[:3:-1]
('F', 'E')
>>> ("A", "B", "C", "D", "E", "F")[0:4:1]
('A', 'B', 'C', 'D')
>>> ("A", "B", "C", "D", "E", "F")[-2::-1]
('E', 'D', 'C', 'B', 'A')
>>> ("A", "B", "C", "D", "E", "F")[:-19:-1]
('F', 'E', 'D', 'C', 'B', 'A')
>>> ("A", "B", "C", "D", "E", "F")[::11]
('A',)
>>> ("A", "B", "C", "D", "E", "F")[::-56]
('F',)
|
Built-in Tuple Functions
[edit | edit source]
[edit | edit source] |
Tuple Usage
[edit | edit source]
Tuples are great for holding static data that you don't plan on modifying and changing a lot. Like the previous lesson stated, tuples are four to seven times faster than lists. This means for every list item manipulated, four to seven tuples could be manipulated within that time frame. This has huge advantages for scientific work, as this allows for speedy data reading. Although all of the tuple's advantages sound good, the tuple itself is static and immutable. This means that a tuple's content is read-only. To change its content, a tuple must be converted to a dynamic, mutable sequence and then converted back to a tuple. For this reason, if you do a lot of adding and removing of items in a sequence, you shouldn't use the tuple. It is not possible to assign to the individual items of a tuple, however it is possible to create tuples that contain mutable objects, such as lists. Tuples containing lists behave like lists. For example, create a tuple containing a list of five rows in which each row contains four powers of integer >>>
>>> a = tuple( [ [i, i*i, i*i*i, i*i*i*i] for i in range(-2,3) ] ) ; a
([-2, 4, -8, 16], [-1, 1, -1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [2, 4, 8, 16]) # enclosing parentheses '()'.
>>>
>>> isinstance(a,tuple)
True
>>>
>>> a[0] ; a[1] ; a[2] ; a[3] ; a[4]
[-2, 4, -8, 16]
[-1, 1, -1, 1]
[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 4, 8, 16]
>>> a[0][3] ; a[1][2] ; a[2][1] ; a[3][0] ; a[4][1]
16
-1
0
1
4
>>>
>>> a += ([-5,-4,-3,-2],) ; a # tuple extended by concatenation. Note the comma '],)'.
([-2, 4, -8, 16], [-1, 1, -1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [2, 4, 8, 16], [-5, -4, -3, -2])
>>>
>>> a[3][2] = -13 ; a
([-2, 4, -8, 16], [-1, 1, -1, 1], [0, 0, 0, 0], [1, 1, -13, 1], [2, 4, 8, 16]) # Contents of list within a tuple can be manipulated.
>>>
>>> b = list(a) ; b
[[-2, 4, -8, 16], [-1, 1, -1, 1], [0, 0, 0, 0], [1, 1, -13, 1], [2, 4, 8, 16]] # Shallow copy of tuple a.
>>>
>>> b[2][1] = -7 ; b ; a
[[-2, 4, -8, 16], [-1, 1, -1, 1], [0, -7, 0, 0], [1, 1, -13, 1], [2, 4, 8, 16]] #
([-2, 4, -8, 16], [-1, 1, -1, 1], [0, -7, 0, 0], [1, 1, -13, 1], [2, 4, 8, 16]) # Both are affected.
>>>
>>> a[0] = [3,5,7,9] # Basic structure of tuple cannot be changed.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>
For a true deep copy: >>> import copy
>>>
>>> t1 = ([-2, 4, -7, 16], [-1, 1, -1, 1], ) ; t1
([-2, 4, -7, 16], [-1, 1, -1, 1])
>>> t2 = copy.deepcopy(t1) ; t1 ; t2
([-2, 4, -7, 16], [-1, 1, -1, 1])
([-2, 4, -7, 16], [-1, 1, -1, 1])
>>> t1[1][2] = -3 ; t1 ; t2
([-2, 4, -7, 16], [-1, 1, -3, 1]) # Only t1 has changed.
([-2, 4, -7, 16], [-1, 1, -1, 1]) # t2 remains a true copy of t1 before t1 was changed.
>>>
|
Assignments
[edit | edit source]
|
Further Reading or Review
[edit | edit source]
|
References
[edit | edit source]
4. Python's documentation: "Tuples," "Common Sequence Operations," "Tuples and Sequences," "Why are there separate tuple and list data types?"
|