作者回复: 塌破 ,我再练习几遍
del 准确来说是删除 变量名和 tuple对象之间的联系, 一旦tuple对象没有变量继续引用它了,tuple对象会等待Python解释器释放,释放以后才能进入到内存回收
1
GeekNEO
2023-02-20来自浙江
1、增加元素 报错
tuple_demo = ('x', 'y', 'z')
print(tuple_demo.append('a'))
AttributeError: 'tuple' object has no attribute 'append'
2、删除元素 报错
tuple_demo = ('x', 'y', 'z')
print(tuple_demo.remove('z'))
AttributeError: 'tuple' object has no attribute 'remove'
3、元素索引操作
tuple_demo = ('x', 'y', 'z')
print(tuple_demo.insert(3, 'a'))
AttributeError: 'tuple' object has no attribute 'insert'
索引:
tuple_demo = ('x', 'y', 'z')
for i in range(len(tuple_demo) + 1):
print(tuple_demo[i])
1
水里捞鱼
2023-02-07来自北京
1. .insert()、.append() 方法均不存在:tuple本身不支持增加元素,报错信息为 AttributeError: 'tuple' object has no attribute 'remove'
2. .remove() 同理;del 删除元素 报不支持该命令:TypeError: 'tuple' object doesn't support item deletion
3. 不存在第四个元素,因此报索引错误:IndexError: tuple index out of range
1
Mr.Ma
2023-09-15来自广东
num = ('x','y','z')
#num1 = [x for x in num]
num1 = list(num)
print(num)
print(num1)
num1.append('a')
print(num1)
num = tuple(num1)
print(num)
print(num[3])
Geek_219b52
2023-07-21来自浙江
1、#元组增加元素
tuple = ("x","y","z")
tuple.append("c")
执行后提示:AttributeError: 'tuple' object has no attribute 'append'
2、#元组删除元素
tuple = ("x","y","z")
#tuple.remove("x")
执行后提示:AttributeError: 'tuple' object has no attribute 'remove'
3、#在索引位置插入元素
tuple = ("x","y","z")
tuple.insert(3,"c")
执行后提示:AttributeError: 'tuple' object has no attribute 'insert'
不再是入门水平pytho...
2023-07-11来自上海
tuple_demo = tuple(list('xyz'))
print(tuple_demo)
list_convert = list(tuple_demo)
print(list_convert)
list_convert.append('a')
print(list_convert)
list_convert.pop(0)
print(list_convert)
tuple_demo = tuple(list_convert)
print(tuple_demo)
tuple_demo.append('a')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[25], line 1
----> 1 tuple_demo.append('a')
AttributeError: 'tuple' object has no attribute 'append'
tuple_demo.remove('a')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[26], line 1
----> 1 tuple_demo.remove('a')
AttributeError: 'tuple' object has no attribute 'remove'
tuple_demo.index('p')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[30], line 1
----> 1 tuple_demo.index('p')
ValueError: tuple.index(x): x not in tuple
"""尝试对元祖进行增加,删除,访问不存在的元素,观测错误信息"""
tuple1 = ('x', 'y', 'z')
tuple1[1] = 'z' #TypeError: 'tuple' object does not support item assignment
print(tuple1[3]) #IndexError: tuple index out of range