In many ways tuples and lists are very similar. In fact tuples are basically immutable lists, that is, lists which cannot be changed. All the same operators work on them; concatenation, repetition, subscription, and slicing. All the comparison operators work in the same way: equivalence, less than, greater than, 'is'. However, assignments to tuples cannot be made as they are immutable, or unchangeable. Once a tuple has been formed, it cannot be changed. New tuples can be formed from others using concatenation, repetition, etc... but the operand tuples remain unchanged by the operators.
Tuples are technically formed in python using a comma, although most often they are found recorded in round braces. Tuples can be of any length, and have their elements indexed from 0 to len(tuple)-1.
Python 2.4.3 (#1, Oct 2 2006, 21:50:13) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1, 2 (1, 2) >>> 1, (1,) >>> (1) 1 >>> (1, 2) (1, 2) >>> ("Tuples", "can", "have", "elements", "of", "more", "than", 1, "type") ('Tuples', 'can', 'have', 'elements', 'of', 'more', 'than', 1, 'type') >>>
Breaking it down we get to the old grind of operators, and in the case of tuples a a few new ideas. Let's get the operators out of the way! If both 'a' and 'b' are tuples, 'v' is any expression, and 'i', 'j', and 'k' are integers then
a + b
yields Concatenation.a * i
yields Repetition.a[i]
yields Subscription, or the 'i'th element of
'a'.a[i:j:k]
yields Slicing, or a copy of the list 'a'
from its 'i'th element to its (j-1)'th element, taking only every
'k'th element. If 'k' is omitted, every element in the range is
taken.a[:i]
yields a slice from the beginning of the
list to (i-1)'th element of 'a'.a[i:]
yields a slice from the 'i'th element to the
end of the tuple.a[:]
yields a slice of the entire tuple.For completeness sake, here are examples ...
Python 2.4.3 (#1, Oct 2 2006, 21:50:13) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = (1,2,3) >>> a+(4,5) (1, 2, 3, 4, 5) >>> >>> (4,5)+a (4, 5, 1, 2, 3)
>>> a*2 (1, 2, 3, 1, 2, 3) >>> >>> (1,)*2+2*(2,) (1, 1, 2, 2) >>> >>> ((1,)*2,(2,)*2) ((1, 1), (2, 2)) >>> >>> (1,)*2*3 (1, 1, 1, 1, 1, 1) >>>
>>> a[0] 1 >>> a[1:3] (2, 3) >>> a = (1, 2, 3, 4, 5) >>> a[0:6:2] (1, 3, 5) >>> a[:3] (1, 2, 3) >>> a[3:] (4, 5) >>> a[:] (1, 2, 3, 4, 5) >>>
The comparison of tuples to other tuples happens in the same way as lists to lists.
a == b
yields Equivalencea < b
yields Less Than. Note that tuples are
compared in sequence order, meaning that 'a' is less then 'b' if
'a's first element is less than 'b's first element. If 'a's first
element and 'b's first element are equal, the second elements are
checked, etc...v in a
is True only if the tuple 'a' contains an
element whose value is that of the expression 'v'.a is b
is True only if 'a' and 'b' are the same
tuple.If tuples have a subset of the functionality of lists, why ever use them? What can they do that lists cannot. Well, actually they provide a very convenient way to package multiple variables together for passing to a function as a parameter, or more likely returning from a function. Technically speaking functions can only return one value, but since a tuple is a single 'value' no matter how many elements it contains, a tuple could be used in a function that returns a two-component piece of data, for example an x:y coordinate. Even better, is that python implicitly unpacks tuples. By unpack we mean the assignment of multiple variables at once to their respective (based on position) elements in a tuple, for example
>>> t = (1, 2, 3) >>> a, b, c = t >>> a 1 >>> b 2 >>> c 3 >>>
And since tuples are formed by a comma, this provides us with a nifty little method to swap the values of two variables,
>>> a = 1 >>> b = 2 >>> b, a = a, b >>> a 2 >>> b 1 >>>
a, b = b, a
swaps the values of a
and b. What would happen if added a third variable to the
mix, c, as in a, b, c = b, c, a
? What would
happen if one repeated the statement indefinitely?a =
()
), how does one 'append' an element to 'a'?[('Maths', 'D'),
('Comp Sci', 'B'), ('English', 'C'), ('French', 'A'),
('Science', 'B'), ('History', 'E')]
: