Links: Journey to Data Scientist
Characteristics- Ordered
- Can contain arbitrary objects
- Accesible via index
- Can be nested
- Immutable
It shares most of the characteristics as Lists except it is immutable. Therefore, operations on Tuples are faster than Lists (when number of elements is getting larger)
Immutable
t = ("apple","moon","3",57,9.99,0x3e)
print(t)
print(t[1])
print(t[3:6])
print(t[::4])
Output:
('apple', 'moon', '3', 57, 9.99, 62)
moon
(57, 9.99, 62)
('apple', 9.99)
If we try to modify the value:
t[0] = "orange"
Output:
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-9456d7af7fd3> in <module>
----> 1 t[0] = "orange"
TypeError: 'tuple' object does not support item assignment
Unpacking Tuples
t = ("apple","moon","3",57,9.99,0x3e)
(u1,u2,u3,u4,u5,u6) = t
print(u1,u2,u3,u4,u5,u6)
Output: apple moon 3 57 9.99 62
No comments:
Post a Comment