Error on Mock exam 2 Q1 test question

Hi, firstly, when looking at this question, it feels like the code is not complete. But my issue is with understanding the answer. How can you tell that this is an atribute error?

The type tuple is immutable, which means that after it is created it cannot be modified.
As such it does not have an append method - because that implies modification.
AttributeError is raised because the append method doesn’t exist.

tuple1 = (1,2,3,4,5)
print (tuple1.append(6))

AttributeError: 'tuple' object has no attribute 'append'

If you wanted to be able to modify it, then you would use list not tuple

list1 = [1,2,3,4,5]
print (list1.append(6))

None

The result is None because append does not return anything. However the list has been modified, so if we then did…

print (list1)

[1,2,3,4,5,6]

tuple and list work almost exactly the same except that you cannot change a tuple after it has been created.

1 Like