Regarding tuples, the term was covered in the previous lecture. This lecture deals with it in its entirety.
Let's look at an example first:
>>>#Variables referencing str >>> s = "abc" >>> s 'abc' >>>#If it was written that way, it would be... >>> t = 123,'abc',["come","here"] >>> t (123, 'abc', ['come', 'here'])
The variable t, as seen in the above example, does not report an error, nor is it "last valid", but rather the object is assigned to the variable t as a new data type: tuple.
Tuples are enclosed in parentheses and the elements in them are separated by commas. (All are in English half-width)
A tuple is a sequence of data, similar to a list/str in this respect. Its characteristic is that its elements cannot be changed, which is different from list, but similar to str; its elements can be any type of data, which is the same as list, but different from str.
>>> t = 1,"23",[123,"abc"],("python","learn") #Element diversity, near-list >>> t (1, '23', [123, 'abc'], ('python', 'learn')) >>> t[0] = 8 # Can't modify in-place, near-str Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> ("no") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'append' >>>
From the simple comparison above it seems fair to assume that tuple is a hybridization that incorporates some of the list and some of the str properties. This makes sense.
Accessing elements and slices like a list
Review a little bit of the knowledge in the LIST first:
>>> one_list = ["python","qiwsir","github","io"] >>> one_list[2] 'github' >>> one_list[1:] ['qiwsir', 'github', 'io'] >>> for word in one_list: ... print word ... python qiwsir github io >>> len(one_list) 4
Here's another experiment to see if the above list works if replaced with a tuple
>>> t (1, '23', [123, 'abc'], ('python', 'learn')) >>> t[2] [123, 'abc'] >>> t[1:] ('23', [123, 'abc'], ('python', 'learn')) >>> for every in t: ... print every ... 1 23 [123, 'abc'] ('python', 'learn') >>> len(t) 4 >>> t[2][0] #Oh, yeah, you can do that in a list. 123 >>> t[3][1] 'learn'
All methods that can modify a list in a list, in a tuple, fail.
The transformation can be achieved by using list() and tuple() respectively.
>>> t (1, '23', [123, 'abc'], ('python', 'learn')) >>> tls = list(t) #tuple-->list >>> tls [1, '23', [123, 'abc'], ('python', 'learn')] >>> t_tuple = tuple(tls) #list-->tuple >>> t_tuple (1, '23', [123, 'abc'], ('python', 'learn'))
Where is tuple used?
Since it's a mashup of list and str, what purpose does it serve? Isn't it fine to use both list and str?
In many cases, it is true that both list and str will work. But don't forget that the problems we solve in computer languages are not always simple, and just as in our natural language, we still need to use some words in some situations, even though they may seem optional and can be replaced by other words.
It is generally recognized that the tuple has these characteristics, and that this is also the context in which it is used.
Tuples are faster than lists. If you define a constant set of values and the only thing you want to do with it is to keep iterating over it, use tuple instead of list.
Code can be made safer if it is "write-protected" for data that does not need to be modified. Using a tuple instead of a list is like having an implicit assert statement that this data is constant. If you must change these values, you need to perform a tuple to list conversion (which requires the use of a special function).
Tuples can be used as keys in a dictionary, but lists can't. Actually, it's more complicated than that. In fact, it's more complicated than that: a dictionary key must be immutable; a tuple itself is immutable, but if you have a tuple of lists, it's considered mutable, and it's not safe to use it as a dictionary key. Only strings, integers, or other dictionary-safe tuples can be used as dictionary keys.
Tuples can be used in string formatting, which will be used later.