8.Python元组
In [1]:
t1 = ()
print(type(t1))
In [2]:
t2 = ('hello',) #定义一个元素的元组时必须要加逗号,不然被认为是字符串类型
t3 = ('hello')
print(type(t2))
print(type(t3))
In [3]:
tup = (1,2,3,4,5,6,8,9,7,10)
print(max(tup))
print(min(tup))
print(len(tup))
print(sum(tup))
print(tup.count(6)) #找出元组中6的个数
print(tup.index(6)) #找出元组中找出6的下标位置
In [4]:
t1 = (1, 2, 3)
In [5]:
#变量个数与元组个数不一致时会报错
#a,b = t1 #ValueError: too many values to unpack (expected 2)
#a, b, c, d = t1 #ValueError: not enough values to unpack (expected 4, got 3)
a, b, c = t1
In [6]:
t1 = (1, 2, 3, 4, 5, 6)
a, *_, b = t1
print(a)
print(b)
print(_)
In [7]:
t1 = (1, 2)
a, *b, c = t1
print(a)
print(b)
print(c)
版权所有
http://www.cnblogs.com/King-Penguin
posted on 2020-01-06 15:34 King-Penguin 阅读(210) 评论(0) 编辑 收藏 举报