What is tuples in python
- A tuple is a built-in Python data type used to store multiple items in a single variable.
- Tuples are similar to lists, but unlike lists, they cannot be modified after they are created (they are immutable).
- A tuple is an ordered collection, so the items maintain their original order.
-
Tuple items are separated by commas "," and are usually enclosed in parentheses
(). - Tuples can store duplicate values.
- A tuple can contain different data types, such as strings, integers, floats, and booleans.
- You can access tuple items using their index, just like a list.
- Tuples are generally faster and more memory-efficient than lists because they are immutable.
Example 1
tuple1 = (1, 2, 2, 3, 5, 4, 6)
tuple2 = ("Red", "Green", "Blue")
print(tuple1)
print(tuple2)
Output:
(1, 2, 2, 3, 5, 4, 6)
('Red', 'Green', 'Blue')
Example 2
tuple = (1, 2, 'Mind', 3, 5.6, 4, 6)
print(tuple)
Output:
(1, 2, 'Mind', 3, 5.6, 4, 6)