tup = tuple("String")
tup
Output:(‘S’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’)
tup = tuple(['foo',[1,2,3],'zyy'])
tup[1].append(3)
tup
Output:(‘foo’, [1, 2, 3, 3], ‘zyy’)
('foo',[1,2,3],'zyy')+(2,5)+('hhhh',)
Output:('foo', [1, 2, 3], 'zyy', 2, 5, 'hhhh')
('foo',[1,2,3],'zyy')*2
Output:(‘foo’, [1, 2, 3], ‘zyy’, ‘foo’, [1, 2, 3], ‘zyy’)
tup = (1,2,3)
a,b,c = tup
b
Output:2
a,b = 1,2
a,b = b,a
a
Output:2
values = 1,2,3,4
a,b,*rest = values
a,rest
Output:(1, [3, 4])
2、列表:可以修改,[]来初始化
list = ['foo', [1, 2, 3], 'zyy', 'foo', [1, 2, 3], 'zyy']
'zyy' in list
Output:True
list = ['foo', [1, 2, 3], 'zyy', 'foo', [1, 2, 3], 'zyy']
list.extend([2,3,5])
list
Output:[‘foo’, [1, 2, 3], ‘zyy’, ‘foo’, [1, 2, 3], ‘zyy’, 2, 3, 5]
list = ['foo', 'zyyilyd', 'foooof']
list.sort(key=len)
list
Output:[‘foo’, ‘foooof’, ‘zyyilyd’]
list1 = [1,2,4,5,63,3]
list2 = [2,4,63,2,3,6,4]
list1[-3:],list2[::2]
Output:[([5, 63, 3], [2, 63, 3, 4])
(当想要对列表进行翻转的时候,就可以使用list[::-1]
3、内建序列函数
list = ['zyy','zyj','xht','hyj','gjy','cjn']
map = {
}
for i,v in enumerate(list):
map[v]=i
map
Output:{‘zyy’: 0, ‘zyj’: 1, ‘xht’: 2, ‘hyj’: 3, ‘gjy’: 4, ‘cjn’: 5}
list = ['zyy','zyj','xht','hyj','gjy','cjn']
sorted(list)
Output:[‘cjn’, ‘gjy’, ‘hyj’, ‘xht’, ‘zyj’, ‘zyy’]
4、字典:键值,{}来创建
5、集合:无序且元素唯一的容器,可以用set()或者{}来初始化
二、函数
1、命名空间、作用域和本地函数
2、返回多个值
def f():
a = 5
b = 6
c = 7
return a,b,c
a,b,c = f()
a,b,c
Output:(5, 6, 7)
3、函数是对象
4、匿名函数(Lambda):通过单个语句生成函数的方式
5、生成器:通过一致的方法遍历序列
6、错误和异常处理
参考教材:《利用Python进行数据分析》Wes Mckinney著