Author: Anagha Sivadas T
Tuples are immutable sequences, typically used to store collections of heterogeneous data
Tuples may be constructed in a number of ways:
- Using a pair of parentheses to denote the empty tuple:
() - Using a trailing comma for a singleton tuple:
a,or(a,) - Separating items with commas:
a, b, cor(a, b, c) - Using the
tuple()built-in:tuple()ortuple(iterable)
Examples
- Input:
x=tuple('abc')
print(x)Output: ('a', 'b', 'c')
- Input:
y=tuple( [1, 2, 3] )
print(y)Output: (1, 2, 3)
- Input:
z=tuple()
print(z)Output:
()
Note: It is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity.