forked from childe/simplepython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht.py
More file actions
43 lines (34 loc) · 1011 Bytes
/
t.py
File metadata and controls
43 lines (34 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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, len(t) # <type 'tuple'> () 0
t = (1, 2)
print type(t), t # <type 'tuple'> (1, 2)
t = 1, 2
print type(t), t # <type 'tuple'> (1, 2)
# 注意,只有一个元素的话, 后面一定要加逗号
t = (1)
print type(t) # <type 'int'>
t = (1,)
print type(t) # <type 'tuple'>
t = 1,
print type(t) # <type 'tuple'>
#==================== 将元组中的元素依次赋值 ====================
t = (1, 2, 3)
a, b, c = t
print a, b, c # 1 2 3
#==================== 切片遍历和列表一样 ====================
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
"""