Python/Data Type

[Python] Sequence Data Type - byte, bytearray

데이터 세상 2022. 4. 25. 14:06

Sequence Data Type

  • memership 연산: in 키워드 사용
  • size 함수: len(seq)
  • slicing 속성: seq[:-1]
  • iterability: 반복문에 있는 데이터를 순회할 수 있음
  • 문자열, 튜플, 리스트, 바이트 배열, 바이트 등 5개의 내장 시퀀스 타입이 있음

byte, bytearray

  • raw binary를 처리하는데 사용할 수 있는 데이터 타입
  • byte: immutable, 문자열 타입과 비슷
  • bytearray: mutable, 리스트 타입과 비슷
  • 0~255 범위의 부호 없는 8비트 정수 시퀀스
blist = [1, 2, 3, 255]
the_bytes = bytes(blist)
the_bytes
>> b'\x01\x02\x03\xff'

the_byte_array = bytearray(blist)
the_byte_array
>> bytearray(b'\x01\x02\x03\xff')

the_bytes[1] = 127 # immutable
>> Traceback (most recent call last):
TypeError: 'bytes' object does not support item assignment

the_byte_array[1] = 127 #mutable
the_byte_array
>> bytearray(b'\x01\x7f\x03\xff')

References

반응형