In [7]:
# Creating an immutable array
b1 = bytes([1, 2, 3, 4]) # Integers must be in the range from 0 to 255
b2 = 'The String'.encode('utf-8')
b3 = (-1024).to_bytes(4, byteorder='big', signed=True) # byteorder = "big"/"little"/"sys.byteorder", signed = False/True
b4 = bytes.fromhex('FEADCA')
b5 = bytes(range(10,30,2))
print(b1, b2, b3, b4, b5)
# Преобразование
c: list = list(b'\xfc\x00\x00\x00\x00\x01')
s: str = b'The String'.decode('utf-8')
b: int = int.from_bytes(b'\xfc\x00', byteorder='big', signed=False) # byteorder = big/little/sys.byteorder, signed = False/True
s2: str = b'\xfc\x00\x00\x00\x00\x01'.hex(' ')
print(c, s, b, s2)
with open('1.bin', 'wb') as file: # Byte writing to a file
file.write(b1)
with open('1.bin', 'rb') as file: # Reading from a file
b6 = file.read()
print(b6)
b'\x01\x02\x03\x04' b'The String' b'\xff\xff\xfc\x00' b'\xfe\xad\xca' b'\n\x0c\x0e\x10\x12\x14\x16\x18\x1a\x1c' [252, 0, 0, 0, 0, 1] The String 64512 fc 00 00 00 00 01 b'\x01\x02\x03\x04'
What data types are there in Python? Which of them are mutable and which are immutable?
str, bytes, int, float, complex, bool, None, tuple and frozenset are immutable, while list, set, dict, bytearray and memoryview are mutable.