Loại dữ liệu nào có thể thay đổi trong Python?

• Thông thường, nó làm mọi thứ hiệu quả hơn. Ví dụ, việc tìm kiếm

list[0] = '!'
#['!', '2', '3', '4', '5']
9 của một chuỗi nhanh hơn nhiều, vì nó là một phần của đối tượng chuỗi

Các kiểu dữ liệu có thể thay đổi là những kiểu dữ liệu có trạng thái có thể thay đổi ngay cả sau thời gian chạy. Ví dụ. Danh sách, Tập hợp và Từ điển là các kiểu dữ liệu có thể thay đổi. Tương tự, các lớp hải quan cũng có thể thay đổi. Các loại dữ liệu có thể thay đổi được thảo luận như sau

  1. Danh sách. Đó là chuỗi các đối tượng có thể thay đổi. Nó được tạo bằng cách đặt tất cả các phần tử bên trong dấu ngoặc vuông [ ]. Nó chứa vị trí tuần tự của các yếu tố
  2. Bộ. Nó là tập hợp không có thứ tự của các đối tượng riêng biệt
  3. Từ điển. Nó liên quan đến ánh xạ kết hợp i. e. một khóa được ánh xạ hoặc liên kết với một giá trị trong đó các giá trị có thể thuộc bất kỳ loại dữ liệu nào. Từ điển là các cặp khóa-giá trị không có thứ tự

Trong khi đó, các kiểu dữ liệu không thay đổi là những kiểu dữ liệu có giá trị không thể thay đổi sau khi nó được xác định trong thời gian chạy. Chúng bao gồm giá trị Boolean, số nguyên, float, tuple, string và freezeset

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
    0

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    2

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    3
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    5_______10
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    2
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    6
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    7

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    8
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    10
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    12

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    13
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    14

    Lỗi

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    5




    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    16

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    17

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    18
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    90

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    91
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    10
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    95

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    13
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    97

    Lỗ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
    0

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    99

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    00
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    02
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    03
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    05
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    07
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    10

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    13
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    10

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    17

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    12
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    10
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    16

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    12
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    18
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    2
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    10
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    22

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    13
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    10

    đầ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
    1

Sự kết luận

  1. 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.
  2. 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
  3. 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 tuple
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    9

    Tuple 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

Kiểu dữ liệu nào là bất biến?

Chuỗi là loại dữ liệu không thể thay đổi, nghĩa là không thể cập nhật giá trị của nó.

Ví dụ có thể thay đổi trong Python là gì?

Trong Python, khả năng biến đổi có nghĩa là bạn có thể trực tiếp sửa đổi một đối tượng sau khi tạo . Ví dụ, một danh sách là một đối tượng có thể thay đổi. Sau khi tạo danh sách, bạn có thể thêm, sửa đổi hoặc xóa các phần tử khỏi danh sách đó. Tính bất biến có nghĩa là không có khả năng thay đổi đối tượng sau khi tạo.

Các loại bất biến và có thể thay đổi trong Python là gì?

Loại đối tượng xác định các giá trị và hoạt động có thể. Các đối tượng có giá trị có thể thay đổi được gọi là có thể thay đổi. Các đối tượng có giá trị không thể thay đổi khi chúng được tạo ra được gọi là bất biến

Int có thể thay đổi trong Python không?

int có bản chất là bất biến , vì vậy chúng ta không thể thay đổi hoặc cập nhật kiểu dữ liệu int. Như chúng ta đã đọc trước đó, các đối tượng bất biến đó sẽ thay đổi địa chỉ bộ nhớ của chúng khi chúng được cập nhật. Chúng tôi đã gán một số giá trị số nguyên cho một biến. Sau đó, chúng tôi in id của biến đó.

Chủ Đề