0
Isn’t tuples immutable?
Then why isn’t the output of this one an error? Id=('pookie','Mr','Rose') print(Id[2])
4 Antworten
+ 3
immutable means it cannot be altered after it is created.
so:
Id = ('pookie', 'Mr', 'Rose')
Id[2] = 'Pink' # error
TypeError: 'tuple' object does not support item assignment (This means it's immutable)
iterable means it's a collection or sequence of elements that can be accessed one at a time . lists, rules, strings, sets, dictionaries, etc are examples of iterables.
print(Id[2])
means you are accessing the third element. You can use and print a tuple's element, but you can't change or add to it after you create it. You can create a new tuple and reassign it to the variable, but that is a creating different tuple.
Id = ('pookie', 'Mr', 'Rose')
print(id(Id))
Id = ('pookie', 'Mr', 'Pink')
print(id(Id)) # different id
+ 2
You might be confusing the terms "immutable" and "iterable"?
+ 1
Lisa after posting it, I get that question. It doesn’t change , It just prints.
But can you tell me about iterable? What's that
+ 1
"iterable" means we can iterate over its elements, for instance, using a for-loop.