CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Python – bytearray – A quick walk through with working Examples

A bytearray is also another sequence type in Python; which holds a sequence of bytes. Unlike bytes; bytearray objects are mutable. That means, we can modify the elements in bytearray objects.

Define a bytearray Object

bytearray objects are created using the bytearray class constructor. There is no relevant literal syntax to create bytearray objects.

The syntax of the bytearray constructor is:

Syntax: bytearray([source [, encoding [, errors] ] ])

Note that, all the arguments to the bytearray constructor are optional. If you do not pass any arguments; Python creates a bytearray object with NO elements; that means, an empty instance.

>>> ba1 = bytearray()
>>> ba1
bytearray(b'')
>>> 

In our previous Article “Python – Ranges – A walk through with Examples“, we have discussed ranges; which is a collection of integers. Below is the statement, creates bytearray object using elements of a range object: which creates alternate numbers from 1 to 20 (excluding).

>>> ba2 = bytearray(range(1, 20, 2))
>>> ba2
bytearray(b'\x01\x03\x05\x07\t\x0b\r\x0f\x11\x13')
>>> 

Observe that, when we print the elements of a bytearray; it prints hexadecimal numbers, which is the usual format Python use to display bytearray elements.

Even we can create bytearray object using bytes object. Below statement shows this:

>>> ba3 = bytearray(b"Hello, World!")
>>> ba3
bytearray(b'Hello, World!')
>>> 

Another interesting thing with bytearray is; it allows to create a bytearray object with zeros filled-in, with a given length. This is very useful when you do not know what values need to be added to the bytearray, but you know the length of it. On the fly, you can add the values, once known. The below statement creates a bytearray object; filled with all zeros:

>>> ba4 = bytearray(10)
>>> ba4
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>>

Access bytearray elements

Like other sequence types, bytearray is also allowed to access its’ elements using the index operator. Index starts from “0” and the index of the last element is (number of elements – 1). It also supports, negative index numbering to access the elements from the end. “-1” is the start index from the end.

If you attempt to give an invalid index value; the Python interpreter throws IndexError. For example, if you attempt to access an element from an empty instance (“ba1”, what we have created from above), will throw an Error:

>>> ba1[0]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: bytearray index out of range
>>>

Below are the statements to access byetearray object’s first and last elements.

>>> ba2[0]
1
>>> ba2[-1]
19
>>>

Modify bytearray elements

As mentioned above, bytearray is mutable. That means it allows to modify its’ elements. We can use the index operator and “=” sign to assign/modify bytearray elements.

From above, “ba4bytearray object, contains all zeros. Now we will modify one of its’ elements and display the result.

>>> ba4[3] = 65
>>> ba4
bytearray(b'\x00\x00\x00A\x00\x00\x00\x00\x00\x00')
>>>

Observe that, “A” (ASCII value is 65) was added at the 4th position (index value is 3) and shown in the result when we display it.

I gave a very quick introduction about bytearray. We will discuss more sequence types in the upcoming Articles.

/Shijit/

Python – bytearray – A quick walk through with working Examples

2 thoughts on “Python – bytearray – A quick walk through with working Examples

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top