Tuple

Tuple

immutable, tuple can not be modified. Modify a tuple variable result in a new tuple.
Tuple usually is in parentheses (), for example: (1,2,3,4,5) or (β€˜a’,’b’,’c’) or (1,2,3,’a’,’b’,’10’)
However, parentheses is not indicative to tuple, comma is. Tuple must have comma.
Like list, tuple is a sequence, iterable
t=(β€˜a’,’b’,’c’)
t[0]=β€œa”
t[1]=β€œb”
t[2]=β€œc”
It is zero based indexing, index starts from zero
Tuple can be iterated through:
1
for i in t:
2
print(i)
Copied!
Convert to Tuple, use function tuple()
For example:
1
tuple("12345")
2
('1', '2', '3', '4', '5')
3
​
4
tuple([1,2,3,4,5])
5
(1, 2, 3, 4, 5)
6
​
7
tuple(12345)
8
​
9
---------------------------------------------------------------------------
10
TypeError Traceback (most recent call last)
11
<ipython-input-10-cc97692a48d4> in <module>
12
----> 1 tuple(12345)
13
​
14
TypeError: 'int' object is not iterable
Copied!
Tuple is an object, meaning it has methods and attributes that can be invoked
To see all methods, type
help(<tuple variable>)
In Jupyter notebook, to see list of methods or attributes
Press shift key after enter <tuple variable>.
Last modified 1yr ago
Copy link