Interview questions on tuple in python #python#pythonforbeginners#interviewquestions#pythontutorial

preview_player
Показать описание
A tuple in Python cannot be modified after it is created. Tuples are immutable, which means their elements cannot be changed, added, or removed once the tuple is assigned.

Here are some characteristics of immutable objects and why tuples exhibit this behavior:

1. **Immutability:**
- Once a tuple is created, its elements are fixed and cannot be altered. Any attempt to modify a tuple will result in an error.

2. **Hashability:**
- Tuples are hashable because of their immutability. This makes them suitable for use as keys in dictionaries or elements in sets.

3. **Preservation of Order:**
- Tuples preserve the order of elements, and this order remains constant throughout the tuple's existence.

Here's an example illustrating the immutability of tuples:

```python
my_tuple = (1, 2, 3, 4)
# Attempting to modify the tuple will raise an error
# For example, the following line would result in an error:
# my_tuple[0] = 10
```

If you need a data structure that allows modifications, you should use a list instead of a tuple. Lists are mutable and provide methods like `append()`, `extend()`, `remove()`, and `pop()` for modifying their contents.
Рекомендации по теме
Комментарии
Автор

Yes, we can change the list in the given tuple example, by using indexing we can add or modify the elements in that list

KurapatiR
visit shbcf.ru