Prev: Dictionary functions and methods | Next: The 'set' object
t = (1,2,3)
t[0] = 'X'
- sequencial, heterogeneous, ordered: just like a list
- indexed: a tuple implements integer-based indices.
- When writing a tuple the values are enclosed in parentheses (or even without them as a comma-separated list of items)
- When accessing a tuple item the index is written in square brackets
- immutable: as explained above
t = (1,2,3,4)
#...............
t = 10,9,8,7
t
t = (1,2,3,4)
t = t[:3]+(10,) # Note the comma in (10,): denotes an one-item tuple
t
t = [100], [20, 30], 'str'
t[1][0] = 'X'
t
t = tuple('spam')
print(t)
L = list('spamspam')
t = tuple(L)
print(t)
...works just like a list with one subtle difference:
- A tuple iterable can not be assigned a different value in the loop (while a list iterable can). So, if you want to make sure that the iterable does not change during loop execution use tuple instead.
iterobj = [1,2,3,4,5]
for i in iterobj:
if i%2:
iterobj[i-1] = 'ok'
print(iterobj)
BLACK = (0,0,0)
WHITE = (255,255,255)
print(BLACK, WHITE)
di = {(0,0,0):'black', (255,255,255):'White'}
di[(0,0,0)]
. Free learning material
. See full copyright and disclaimer notice