# 元组

2022-08-08 15:03:55  阅读：429  来源： 互联网

##元组

#代码：
rhyme = (1,2,3,4,5,"上山打老虎")
rhyme
(1, 2, 3, 4, 5, '上山打老虎')
rhyme = 1,2,3,4,5,"上山打老虎"
rhyme
(1, 2, 3, 4, 5, '上山打老虎')
rhyme
1
rhyme[-1]
'上山打老虎'
rhyme[:3]
(1, 2, 3)
rhyme[3:]
(4, 5, '上山打老虎')
rhyme[::2]
(1, 3, 5)
rhyme[::-1]
('上山打老虎', 5, 4, 3, 2, 1)

nums = (2,3,5,4,3,1,2,3,4)
nums.count(3)
3
heros = ("蜘蛛侠","绿巨人","黑寡妇")
heros.index("黑寡妇")
2
s = (1,2,3)
t = (4,5,6)
s + t
(1, 2, 3, 4, 5, 6)
s * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
w =s, t
w
((1, 2, 3), (4, 5, 6))

for each in s:
print(each)

1
2
3
type(x)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
type(x)
NameError: name 'x' is not defined
for i in w:
for each in i:
print(each)

1
2
3
4
5
6
s = (1,2,3,4,5)
[each * 2 for each in s]
[2, 4, 6, 8, 10]
(each * 2 for each in s)
<generator object <genexpr> at 0x00000284FA46FCA0>
x = (520)
x
520
type(x)
<class 'int'>
x = (520,)
x
(520,)
type(x)
<class 'tuple'>
t = (123,"Flish",3.14)
x, y, z = t
x
123
y
'Flish'
z
3.14
t = [123,"Flish",3.14]
x, y, z = t
x
123
y
'Flish'
z
3.14
a,b,c,d,e = "Flish"
a
'F'
b
'l'
c
'i'
d
's'
e
'h'

x
x,y = 10,20
x
10
y
20
_=(10,20)
x,y=_
x
10
y
20
s = [1,2,3]
t = [4,5,6]
w = (s,t)
w
([1, 2, 3], [4, 5, 6])
w=0
w
([0, 2, 3], [4, 5, 6])
w
5
w=1
w
([0, 2, 3], [4, 1, 6])