元组和列表十分类似,只不过元组和字符串一样是不可变的,即你不能修改元组。

如果程序运行之后,数组中的元素是不应该变化的, 请使用元组类型。
比如网站的所有URL是一个元组, 在网站上线运行之后, 他的URL应该是固定的.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print "all operations of a tuple:"
#count, index
print ", ".join([e for e in dir(tuple) if not e.startswith('_')])


#==================== 定义 ====================
t = tuple()
t = ()
print type(t), t  # <type 'tuple'> ()


#==================== 切片遍历和列表一样 ====================
pass

#==================== 元素不可改变 ====================
t = (1,2,3,4)
t[0] = -1
"""Traceback (most recent call last):
  File "t.py", line 20, in <module>
    t[0] = -1
TypeError: 'tuple' object does not support item assignment
"""