tuple-1

tuple即元组

简介和基本的属性

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Tue Jun 03 20:39:19 2014
 4 
 5 @author: Administrator
 6 """
 7 
 8 """
 9 ' tuple是一种集合类型(collection type),可构建简单对象的组别
10 ' 功能同list,但是tuple不可以修改,用()而不是[]构建,不支持方法调用
11 ' 但是其大多数属性同list
12 ' tuple是任意对象的有序集合
13 ' tuple通过偏移量进行访问
14 ' tuple是分类不可变的序列(of the category immutable sequence)
15 ' tuple是固定长度、异质性、可随意嵌套
16 ' tuple是对象引用的数组
17 """
18 
19 # an empty tuple demo
20 # 用括号可提高代码的可读性
21 tpl_1 = ()
22 print tpl_1
23 
24 # A one-item tuple
25 tpl_2 = (0,)
26 
27 print tpl_2
28 print type(tpl_2)
29 
30 # an integer
31 x = (40)
32 
33 print x
34 print type(x)
35 
36 # a four-item tuple
37 tpl_3 = (0,'Ni',2,3)
38 print tpl_3
39 
40 # another four-item tuple
41 tpl_4 = 0,'Ni',2,3
42 print tpl_4
43 
44 # nested tuples
45 tpl_5 = ('abc',('def','ghi'))
46 print tpl_5
47 
48 # index, index of index, slice and length
49 print tpl_3[2]
50 
51 print tpl_4[1][1]
52 
53 print tpl_3[1:3]
54 
55 print len(tpl_5)
56 
57 # concatenate
58 print tpl_2 + tpl_3
59 
60 print (1,2) + (3,4)
61 
62 # repeat
63 print tpl_3 * 3
64 
65 print (1,2) * 3
66 
67 # iteration, membership
68 for item in tpl_4:
69     print item * 2
70 
71 if('spam' in tpl_4):
72     print 'spam is in tpl_4'
73 else:
74     print 'spam is not in tpl_4'
原文地址:https://www.cnblogs.com/tmmuyb/p/3766485.html