Câu trả lời là tùy chọn C [Tuple]. Tuple là bất biến i. e chúng ta có thể thay đổi các phần tử, xóa các phần tử hiện có và không thể thêm các phần tử mới. Đây là một ví dụ về những gì sẽ xảy ra nếu bạn cố gán lại một phần tử trong bộ dữ liệu
một = [1, 2, 3, 4]
a[0] = 1
Khi chạy đoạn mã trên, bạn sẽ nhận được thông báo lỗi “TypeError. đối tượng 'tuple' không hỗ trợ gán vật phẩm”
Các kiểu dữ liệu như int, float, bool, str, tuple và Unicode là bất biến. Các kiểu dữ liệu như list, set, dict có thể thay đổi
Tôi khuyên bạn nên xem Hướng dẫn Python này của Intellipaat để tìm hiểu thêm về Kiểu dữ liệu trong Python
Các kiểu dữ liệu bất biến là các đối tượng không thể sửa đổi và thay đổi [i. e. thêm phần tử mới, loại bỏ phần tử, thay thế phần tử] sau khi tạo đối tượng. Các kiểu dữ liệu bất biến trong Python là
- Tuple
- số nguyên
- Trôi nổi
- Phức tạp
- Bộ Stringfrozen [ lưu ý. phiên bản bất biến của tập hợp]
- byte
Bạn có thể xem qua Hướng dẫn Python này của Intellipaat để tìm hiểu thêm về Kiểu dữ liệu trong Python
Ngoài ra, hãy xem video này về các loại dữ liệu Python
Mỗi biến trong python chứa một thể hiện của một đối tượng. Có hai loại đối tượng trong python i. e. Các đối tượng có thể thay đổi và không thể thay đổi. Bất cứ khi nào một đối tượng được khởi tạo, nó sẽ được gán một id đối tượng duy nhất. Loại đối tượng được xác định trong thời gian chạy và nó không thể thay đổi sau đó. Tuy nhiên, trạng thái của nó có thể được thay đổi nếu nó là một đối tượng có thể thay đổi
Để tóm tắt sự khác biệt, các đối tượng có thể thay đổi có thể thay đổi trạng thái hoặc nội dung của chúng và các đối tượng không thể thay đổi không thể thay đổi trạng thái hoặc nội dung của chúng
- Đối tượng bất biến. Đây là các loại dựng sẵn như int, float, bool, string, unicode, tuple. Nói một cách đơn giản, một đối tượng bất biến không thể thay đổi sau khi nó được tạo
Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
9Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
0Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
1Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
2Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
3Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
4Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
0Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
1Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
2Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
1Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
4Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
1Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
6Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
7Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
8Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
0Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
00Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
3Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
02Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
03Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
04Lỗi
Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
4Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
9Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
06Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
07Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
08Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
3Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
80Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
81Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
0____100Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
3Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
85Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
03Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
87Lỗi
Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
- Đối tượng có thể thay đổi. Đây là các loại list, dict, set. Các lớp tùy chỉnh thường có thể thay đổi
Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
9Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
89Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
90_______43Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
92____393Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
1Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
95Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
1Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
97Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
00Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
03Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
00Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
07Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
02Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
0Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
00Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
3Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
06Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
02Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
08Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
2Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
00Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
3Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
12Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
03Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
00đầu ra
Traceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
0
Phần kết luận
- Các đối tượng có thể thay đổi và không thể thay đổi được xử lý khác nhau trong python. Các đối tượng bất biến truy cập nhanh hơn và tốn kém để thay đổi vì nó liên quan đến việc tạo một bản sao.
Trong khi các đối tượng có thể thay đổi thì dễ dàng thay đổi. - Nên sử dụng các đối tượng có thể thay đổi khi có nhu cầu thay đổi kích thước hoặc nội dung của đối tượng
- Ngoại lệ. Tuy nhiên, cũng có một ngoại lệ trong tính bất biến. Chúng tôi biết rằng tuple trong python là bất biến. Nhưng bộ dữ liệu bao gồm một chuỗi các tên có liên kết không thể thay đổi đối với các đối tượng.
Xem xét một tupleTraceback [most recent call last]: File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
8Tuple bao gồm một chuỗi và một danh sách. Chuỗi là bất biến nên chúng ta không thể thay đổi giá trị của nó. Nhưng nội dung của danh sách có thể thay đổi. Bản thân bộ dữ liệu không thể thay đổi nhưng chứa các mục có thể thay đổi
Theo nguyên tắc thông thường, các loại giống Nguyên thủy nói chung có thể là bất biến và các loại giống Vùng chứa tùy chỉnh hầu hết có thể thay đổi