Recently learned Django, in the configuration templates path Template_dirs, dead and alive to give me a fault: thetemplate_dirs setting must a tuple. When the tuple has only one element, it should be followed by a comma to avoid ambiguity . Now to learn the various uses of the tuple system, test version python3.4.
Tuple Chinese name is tuple: similar to list but there is a big difference. as a tuple, it performs better than list and has high code security performance . A tuple is defined by enclosing the elements in square brackets, separating the elements with commas, and the elements separated by commas are usually tuple by default.
First, once the tuple element is defined, it will not be changed, and since it is defined, its size and data will not change .
tup0= (1,2,3,4) tup0[0]=2' tuple ' object does not support item assignment
Then, a tuple tuple can be deleted by Del:
tup4= (1' TUP4 ' is not defined
Then, the tuple tuple can perform some operations, such as * and +
tup1= print (tup1)print (tup1+tup1) # (1, 2, 1, 2, 1, 2) # (1, 2, 1, 2)
If there is only one element in a tuple, such as 1, then Python will default to 1, with parentheses on both sides as parentheses in the arithmetic , not tuples!
t= (1) print (T) #2 result is 2
So in order to avoid ambiguity, when there is only one element, we have to add a comma after this element: This is also the reason for the above path error
t= (1,) print (T) (1, 1)
In addition, a tuple can use the slice function, or get the elements in a tuple directly from the index.
tup= (0,1,2,3,4,5) print (tup[0]) print (tup[0:3]) #0# (0, 1, 2)
The count (value) method in a tuple can find several of the same value in a tuple.
The index (value,) method in a tuple can find a value that is indexed.
The __add__ (tuple) method in a tuple actually connects two tuples together.
_contains_ (value) in a tuple to see if there are any specified elements in the tuple.
Len (tuple), max (tuple), min (tuple) as the name implies.
Finally, you can use a tuple (list) to change the list to a tuple:
lis=[1,2,3]print (lis) tu=tuple (LIS) print (TU) #[1, 2, 3]# (1, 2, 3)
The rest of the way to use the time to say it, the above:
Python tuple basic usage