Data Types in Python | Binary Sequence Type in Python | Leela Soft - Madhu Sir

Опубликовано: 04 Ноябрь 2024
на канале: Leela Soft
258
51

#LeelaSOFT #BPKTECHTUTS #Madhu
Binary Sequence Type: bytes and bytearray
Bytes Objects:
Sequence of single bytes.
Bytes objects are immutable.

How to create bytes objects:
1.Single quotes: b'Python'
2.Double quotes: b"Python"
3.Triple quotes: b'''Python''' or b"""Python"""

Bytes objects range: 0-255
Creation of bytes object:
x = b'Python'
print(x[0])

x[0] =90
TypeError: 'bytes' object does not support item assignment

We can create by using bytes():
bytes()
bytes(5) #zero-filled instance
bytes(range(5,16)), bytes("Java", 'utf-8')

Construct an immutable array of bytes from:
an iterable yielding integers in range(256)
a text string encoded using the specified encoding
any object implementing the buffer API.
an integer

Bytearray Objects:
bytearray objects are mutable.

Creation:
We can create by using bytearray():
bytearray()
bytearray(5)
bytearray(range(5,16)), bytearray("Java", 'utf-8')
bytearray(b'Python')

x = bytearray(range(0,257))
ValueError: byte must be in range(0, 256)

x = bytes(range(0,257))
ValueError: bytes must be in range(0, 256)

x = bytearray([10,20])
x[0] = 100
x[0] #it will print 100