8. Python Bytes & Byte arrays Data Types


Bytes data type:

If we want to represent a group of bytes value or binary data or images, videos like, then we need to take a bytes data type.

How to create it, check some examples

r = [10,20,30,40]

b = bytes(r)

print (type(b))

for x in b:

    print (x)

<class 'bytes'>  #Result

Results would be like this: 10,20,30,40 virtually.

Values range 0 to 255 only

Bytes are immutable

r = [10,20,30,40]

b = bytes(r)

b[0] = 77

for x in b:

    print(x)

#Result  TypeError: 'bytes' object does not support item assignment

Byte arrays data type:

If we want to represent a group of Byte arrays values or binary data or images, videos like that we need to take a Bytearray data type

How to create it, check some examples

r = [10,20,30,40]

b = bytearray(r)

print (type(b))

for x in b:

    print (x)

<class 'bytearray'>  #Result

Results would be like this: 10,20,30,40 virtually.

Values range 0 to 255 only

Bytearrays are mutable

r = [10,20,30,40]

b = bytearray(r)

b[0] = 77

for x in b:

    print(x)

Results would be like this: 77,20,30,40 virtually.

 


0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post