版权声明:本文版权归作者所有,未经允许不得转载! https://blog.csdn.net/m0_37827994/article/details/86490996
三、元组
元组也是Python中的对象,可以保存数据,但不能替换它们的值(因此,元组称为不可变,而列表称为可变)。
- 元组(tuples)要用 ( ) 来装数字
- 元组(tuples)中的内容不可改变
1、创建一个元组
# Creating a tuple
tuple_x = (3.0, "hello")
print (tuple_x)
输出结果:
(3.0, 'hello')
2、向元组中加入一个值
# Adding values to a tuple
tuple_x = tuple_x + (5.6)
print (tuple_x)
输出结果:
(3.0, 'hello', 5.6)
3、改变元组中的一个值(会报错)
# Trying to change a tuples value (you can't)
tuple_x[1] = "world"
输出结果:
TypeError Traceback (most recent call last)
<ipython-input-15-86abc19d8025> in <module>()
----> 1 tuple_x[1] = "world"
TypeError: 'tuple' object does not support item assignment