Tuples
Tuples allow to aggregate many values into one single name. They are in many ways similar to lists but there is one key difference: tuple
objects are immutable. They are constructed with parentheses instead of square brackets,
>>> t = (1, 1, 2, 5, 14, 42, 132)
or just separating items with commas
>>> t = 1, 1, 2, 5, 14, 42, 132
>>> t
(1, 1, 2, 5, 14, 42, 132)
Since tuples are immutable all elements must be defined at creation time and cannot be added or removed dynamically:
>>> t[0] = 0
TypeError: 'tuple object does not support item assignment'
>>> del t[0]
TypeError: 'tuple object does not support item deletion'
>>> t.append(429)
AttributeError: 'tuple object has no attribute append'
Lliçons.jutge.org
Víctor Adell
Universitat Politècnica de Catalunya, 2023
Prohibit copiar. Tots els drets reservats.
No copy allowed. All rights reserved.