0

Isn’t tuples immutable?

Then why isn’t the output of this one an error? Id=('pookie','Mr','Rose') print(Id[2])

8th Jan 2025, 4:12 PM
Rouza TN
Rouza TN - avatar
4 ответов
+ 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
8th Jan 2025, 4:55 PM
Bob_Li
Bob_Li - avatar
+ 2
You might be confusing the terms "immutable" and "iterable"?
8th Jan 2025, 4:14 PM
Lisa
Lisa - avatar
+ 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
8th Jan 2025, 4:34 PM
Rouza TN
Rouza TN - avatar
+ 1
"iterable" means we can iterate over its elements, for instance, using a for-loop.
8th Jan 2025, 4:46 PM
Lisa
Lisa - avatar