元组(Tuple) 是 Python 中常用的序列,它是不可修改的。
定义元组
tuple1 = ()
tuple2 = ('hello', 'python')
tuple3 = ('hello', 'python', 2020) # python 是完全动态数据类型,同一个元组元素类型可以是多种的
print(tuple1) # ('hello', 'python')
获取元组长度
length = len(tuple2)
print(length) # 2
元组简单切片
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
numbers_slicing = numbers[3:5]
print(numbers_slicing) # (4, 5)
元组相加
numbers1 = (1, 2, 3, 4)
numbers2 = (5, 6, 7)
numbers = numbers1 + numbers2
print(numbers) # (1, 2, 3, 4, 5, 6, 7)
元组相乘
numbers = (1, 2) * 3
print(numbers) # (1, 2, 1, 2, 1, 2)
还不快抢沙发