一、元组的值不能被修改
#!/usr/bin/python tup =("a","b","c"); tup[1]=7
执行后出现如下错误:
Traceback (most recent call last):
File "C:\Program Files\Sublime Text 3\time.py", line 4, in <module>
tup[1]=7
TypeError: ‘tuple‘ object does not support item assignment
二、元组的元素不能单个删除,只能全部删除
#!/usr/bin/python tup =("a","b","c"); del tup print "After deleting tuple:\n" print tup
执行结果:
After deleting tuple:
Traceback (most recent call last):
File "C:\Program Files\Sublime Text 3\time.py", line 6, in <module>
print tup
NameError: name ‘tup‘ is not defined
三、只有一个元素的元组圆括号必须有逗号,否则不会认为是元组
#!/usr/bin/python tupleone=(1,) print "tupleone is :",type(tupleone) tupleoneerror=(1) print "tupleanotherone is :",type(tupleoneerror)
执行结果;
tupleone is : <type ‘tuple‘>
tupleanotherone is : <type ‘int‘>
四、元组元素的拆分,定义变量
#!/usr/bin/python myinfo=("jinhoward",30,"Male") name,age,gender=myinfo print "Name=",name print "Age=",age print "Gender=",gender
执行结果:
Name= jinhoward
Age= 30
Gender= Male
时间: 2024-11-08 10:40:05