Bytes and Bytearray tutorial in Python 3


[DevDungeon Intro] Hello, this is … [email protected] and we’re going to look at … working with binary data in Python 3. This is the first video in a playlist of several other … videos related to working with binary data in python 3. Most of these features are not available in Python 2 so you … must have Python 3 to follow along. Source code is available on DevDungeon.com for … reference. Check the description for a … link. https://www.devdungeon.com/content/working-binary-data-python In this video we are going to look at the bytes and … bytearray classes. A byte is a data type that … consists of 8 bits. These are the most … fundamental building blocks when working with binary data. Let’s start by creating some bytes! Let’s create a variable named empty_bytes and assign it … the value of 4 freshly created bytes. So let’s type out empty_bytes equals bytes, parenthesis. Remember, 4 Four bytes is the equivalent of 32 bits worth of raw … information. If you wanted to create more … or less bytes, then you can provide a … different number in the parenthesis. After we create the empty_bytes variable, let’s print it out to the console. So, print, parenthesis, empty_bytes Let’s also print out the type of the variable so we can see … what class it is. Now let’s run this program and review the output. Ok, we see on the first line b, followed by slash x zero zero repeated four times. The b means it is binary data, and the x tells you that the … numeric value is in hexadecimal format. In this case, all four bytes have the value of zero. On the second line, we see the type is a class named bytes. It is important to note that bytes created this way are … immutable which means they cannot be modified in any … way after being created. If you have worked with … tuples in Python, they are also immutable and … cannot be changed once they are created. Creating empty bytes with a value of 0 is only so useful. Let’s demonstrate how to create some bytes with real values. Let’s write data_bytes=bytes parenthesis. Now, instead of just passing an … integer to tell it how many bytes to create, we want to pass it some actual values. To pass raw binary values we are going to use hexadecimal … notation. It takes the exact same … format that we saw output when we printed out empty bytes. We need to write lowercase b, following by a single quote to open and a single quote to … encapsulate the values. Each value is written using … the slash X followed by two numbers which represent the … hexadecimal value. The maximum value for a … byte is 255, which translates to FF. So let’s create two bytes with the highest possible value. If you don’t understand hexadecimal notation, I recommend taking a few minutes to do an internet … search and review how it works. Let’s also add a print statement to print out the … value of the data_bytes. Now, let’s run the program and see how it looks. Ok now we see the value of our data_bytes contains the … values we provided instead of just storing 0 like our empty … bytes. This method is much more … useful than the first method. Since the bytes type is not mutable, we must use a different data type if we want to manipulate … the data. Python provides the … bytearray type for this purpose. A bytearray behaves like a regular array except it … contains a series of bytes. I’m going to start a new file for this example. Let’s create a bytearray. Write mutable_bytes equals … bytearray parenthesis. This will create an empty … array that can store bytes. Let’s also print out the value … of our new variable as well as it’s type. Let’s run the file and see what happens. We see similar output as before, except the array is empty. We also see it is of class bytearray. Like the previous examples, we can provide a single … integer to the bytearray function and it will create that … number of empty bytes. Let’s try 3 and rerun the program. Now we can see there are 3 bytes in the array all with a value of 0. Also, like the previous example, we can provide raw data. Let’s pass a few hexadecimal … values and re-run the program. As expected, the values match what we provided. Because we are working with a mutable array, we can index and manipulate specific elements. Let’s change the value of first element in the array to 0. When addressing individual elements we must use the … integer decimal notation so we just pass a 0 instead of … the slash X 00 format. We can also append, pop, and push, just like a regular array. Let’s try appending another FF to the end. Write mutable_bytes.append(255). 255 is the equivalent of FF in hexadecimal and is the … maximum value a byte can store. Like other arrays, we can access a slice of the … array by using the colon notation for the index. For example, let’s print mutable_bytes … bracket 0 colon 2. This will print the first two … elements of the array. Let’s run the program one final time and view the … output. The first element of the array … should be set to 0 and we should have an extra element … of value FF at the end of the array. That’s all we’re going to cover in this video. Hopefully you have an understanding of how to … create mutable and immutable bytes as well as … modify and access the data. If you have any questions … post a comment, or join the Discord server and … ask for help. It’s a growing community of … helpful veterans and a place to meet other aspiring … beginners. All skill levels are welcome. A link to the Discord server is available in the description, and from the DevDungeon.com homepage. https://discord.gg/JWsSHJC Remember, you can find the full written … tutorial on DevDungeon.com at … https://www.devdungeon.com/content/working-binary-data-python Thanks for watching.

8 thoughts on “Bytes and Bytearray tutorial in Python 3

  1. mutable_bytes=bytearray((b'x15x16x65'))
    print(mutable_bytes)

    >>bytearray(b'x15x16e') couldnt understand the meaning ,why last 2 concatenated ?

    GOt it it will print the ascii value if possible, u could have mentioned in yr video. 🙁

  2. This is a great super beginner tutorial. Now let's talk about real-world applications. Say you have a phrase, "I love burritos!" encoded in UTF-8. How do you convert that to binary and how do you read it back and "decode" it for human consumption?

Leave a Reply

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