# 设置全部行输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
s = 'hello world'
t = tuple(s)
t
type(t)
('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
tuple
L = ['life', 'is', 'short', 'I', 'use', 'python']
t = tuple(L)
t
type(t)
('life', 'is', 'short', 'I', 'use', 'python')
tuple
tu = ([1, 2, 3], '数据分析70期')
tu
([1, 2, 3], '数据分析70期')
a = (1, 'hello world', ['人生苦短', 'I use python'], (1, '123', [1, 2]))
a
type(a)
(1, 'hello world', ['人生苦短', 'I use python'], (1, '123', [1, 2]))
tuple
t = (1)
t
type(t)
1
int
t = ('hello world')
t
type(t)
'hello world'
str
t = ([1, 2, 3])
t
type(t)
[1, 2, 3]
list
只含有一个元素的元组的定义,后面必须加一个逗号
t = (1,)
t
type(t)
(1,)
tuple
t = ('hello world',)
t
type(t)
('hello world',)
tuple
t = ([1, 2, 3],)
t
type(t)
([1, 2, 3],)
tuple
a = (1, 2, 3)
b = (4, 5, 6)
a+b
(1, 2, 3, 4, 5, 6)
a
(1, 2, 3)
b
(4, 5, 6)
(1, 2)*5
(1, 2, 1, 2, 1, 2, 1, 2, 1, 2)
print((1,)*10)
(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
1 in (1, 2, 3)
True
4 in (1, 2, 3)
False
[1, 2] in (1, 2, 3, [1, 2])
True
这里和列表是一模一样的,没有任何区别,这里就给大家练一练切片和索引
tuple1 = ('life', 'is', 'short', 'I', 'use', 'python')
tuple1
('life', 'is', 'short', 'I', 'use', 'python')
#取出索引为0的元素
#取出倒数第一个元素
#从索引为1的地方开始,取出索引为1,2,3的元素
# 如何取出('is', 'I')
tuple1 =(1, 2, 3, 'Hello',(1, 2,([1, 1, '我最特殊', 3], 3) , 4, 5))
tuple1
(1, 2, 3, 'Hello', (1, 2, ([1, 1, '我最特殊', 3], 3), 4, 5))
# 如何取出'Hello'
# 如何取出 'Hel'
# 如何取出 'lle'
# 如何取出列表 [1, 1, '我最特殊', 3]
# 如何取出 '我最特殊'
# 如何取出 ‘特殊’
我们之前说了元组是不可变数据类型,那么他就没有增 删 改的方法,相比列表就少了很多。
如果需要修改,可先转化为列表,通过列表进行修改,再转变成元组。
tuple1 = ('life', 'is', 'short', 'I', 'use', 'python', 'python')
tuple1
('life', 'is', 'short', 'I', 'use', 'python', 'python')
tuple1.count('python')
2
tuple1 = ('life', 'is', 'short', 'I', 'use', 'python', 'python')
tuple1
('life', 'is', 'short', 'I', 'use', 'python', 'python')
tuple1.index('python') #返回某元素第一次出现的索引位置
5
tuple1.index('python',0, 4) # 指定范围内查找,找不到,就报错
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-37-5af542c37a9a> in <module> ----> 1 tuple1.index('python',0, 4) # 指定范围内查找,找不到,就报错 ValueError: tuple.index(x): x not in tuple
tuple1 = (1, 2, 'hello world', ['人生', '苦短', '我用python'])
tuple1
(1, 2, 'hello world', ['人生', '苦短', '我用python'])
tuple1[1] = 1 #我们说了元组是不能改变的,所以报错了,没有问题
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-39-a6c49905432f> in <module> ----> 1 tuple1[1] = 1 #我们说了元组是不能改变的,所以报错了,没有问题 TypeError: 'tuple' object does not support item assignment
tuple1[3][0]='life' #为什么这里没有报错
tuple1 # 看样子我们的元组tuple1改变了....
(1, 2, 'hello world', ['life', '苦短', '我用python'])
元组的不可变是指,元组本身自己的元素不可变,同理列表可变是指列表自己本身的元素可变。
tuple2 = ('a', 'b', ['A', 'B'])
tuple2
('a', 'b', ['A', 'B'])
tuple2[2][0] = 'X'
tuple2[2][1] = 'Y'
tuple2
('a', 'b', ['X', 'Y'])
tuple2[2].append('aaa')
tuple2
('a', 'b', ['X', 'Y', 'aaa'])
当我们把list的元素’A'和‘B'修改为’X'和‘Y'后, tuple2变为:
表面上看, tuple2的元素好像变了,但其实变得不是tuple的元素而是list的元素。
tuple2开始指向的list并没有改成别的list,所以tuple2所谓的 ”不变“ 是指, tuple的每个元素,指向永远不变。
即指向’a',就不能改成指向‘b',指向一个list,就不能改成指向其他对象,但指向的这个list本身是可变的!