Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Sequence data type trong Python là gì?


Sequence cho phép lưu trữ nhiều giá trị một cách có tổ chức và hiệu quả. Có các kiểu sequence: strings, Unicode strings, lists, tuples, bytearrays, range objects. Dict và set là container cho dữ liệu non-sequential.

Từ Python Docs chính thức:

  • Strings: immutable sequences của Unicode code points.
  • Lists: mutable sequences, thường dùng lưu collection homogeneous items.
  • Tuples: immutable sequences, thường dùng lưu heterogeneous data (ví dụ 2-tuples từ enumerate()).
  • Bytearray: mutable, hỗ trợ mutable sequence operations + common bytes/bytearray ops.
  • Range: immutable sequence của số, thường dùng loop số lần xác định trong for.

Sequence protocol: hỗ trợ len(), __getitem__, __iter__, __contains__, __reversed__, indexing, slicing.