What are tuples?

Tuples are an immutable, ordered collection of elements in Python. Tuples can store multiple items of different data types, such as integers, strings, or even other tuples. 

Key characteristics of tuples:

1. **Immutable:** Once a tuple is created, its elements cannot be modified.

2. **Ordered:** The elements in a tuple have a defined order, which will not change.

3. **Indexable:** Elements in a tuple can be accessed by their index, starting from 0.

4. **Heterogeneous:** Tuples can contain elements of different types.

Tuples are often used to group related data together. For example, you might use a tuple to store the coordinates of a point (x, y) or to return multiple values from a function.

Here’s an example of a tuple in Python:

```python

my_tuple = (1, "apple", 3.14)

print(my_tuple[1])  # Output: apple

```

In this example, `my_tuple` contains an integer, a string, and a float. The tuple itself cannot be modified after it is created, but you can access its elements via indexing.

Post a Comment

If you have any doubt, Questions and query please leave your comments

Previous Post Next Post