2.6 Tuple:
In list we can add, delete or modify any element but in tuple we can nod add any add, delete or modify any element from tuple.
A tuple is a collection which is ordered and unchangeable.
In Python tuples are written with round brackets.
tuple = ("Apple", 10.4, 10)
print(tuple)
Output:
(‘Apple’, 10.4, 10)
2.6.1 Accessing Tuple Elements:
tuple = ("Apple", 10.4, 10)
print(tuple[1])
Output:
10.4
Note: Once we create a tuple, then we can not change, modify or delete any element from tuple but we can delete complete tuple. Only we can access tuple.
tuple = ("Apple", 10.4, 10)
for x in tuple:
print(x)
Output:
Apple
10.4
10
Check if "apple" is present in the tuple:
tuple = ("Apple", 10.4, 10)
if "Apple" in tuple:
print("Yes, 'Apple' is in the fruits tuple")
Output:
Yes, 'Apple' is in the fruits tuple
To determine how many items a tuple has, use the len() method:
tuple = ("Apple", 10.4, 10)
print(len(tuple))
Output:
3