- Python bytearray() Method
- Syntax:
- Parameters:
- Return Value:
- Python: Create a bytearray from a list
- Visualize Python code execution:
- Python: Tips of the Day
- Python: Create a bytearray from a list
- Visualize Python code execution:
- Python: Tips of the Day
- Python bytearray()
- Example
- bytearray() Syntax
- bytearray() Parameters
- bytearray() Return Value
- Example 1: Array of bytes from a string
- Example 2: Array of bytes of given integer size
- Example 3: Array of bytes from an iterable list
Python bytearray() Method
The bytearray() method returns a bytearray object, which is an array of the given bytes. The bytearray class is a mutable sequence of integers in the range of 0 to 256.
Syntax:
bytearray(source, encoding, errors)
Parameters:
- source: (Optional) An integer or iterable to convert it to a byte array.
- If the source is a string, it must be with the encoding parameter.
- If the source is an integer, the array will have that size and will be initialized with null bytes.
- If the source is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.
- If the source is the iterable object, it must have integer elements only in the range 0 to 256.
Return Value:
Returns an array of bytes.
The following example converts integer to bytearrays.
print(bytearray(1)) # A byte array of size 1 print(bytearray(2)) # A byte array of size 2 print(bytearray(3)) # A byte array of size 3
bytearray(b'\x00') bytearray(b'\x00\x00') bytearray(b'\x00\x00\x00')
If the source argument is a string and no encoding method is specified, a TypeError exception is returned.
print(bytearray('Hello World'))
TypeError: string argument without an encoding
The encoding method needs to be specified to return a bytearray object of the string, as shown below.
print(bytearray('Hello World','utf-8'))
An iterable can also be converted to a bytearray object, as shown below.
nums = [1, 2, 3, 4, 5] print(bytearray(nums))
bytearray(b'\x01\x02\x03\x04\x05')
Python: Create a bytearray from a list
Write a Python program to create a bytearray from a list.
Sample Solution:-
Python Code:
print() nums = [10, 20, 56, 35, 17, 99] # Create bytearray from list of integers. values = bytearray(nums) for x in values: print(x) print()
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource’s quiz.
Follow us on Facebook and Twitter for latest update.
Python: Tips of the Day
How to upgrade all Python packages with pip?
There isn’t a built-in flag yet, but you can use.
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
Note: there are infinite potential variations for this. I’m trying to keep this answer short and simple, but please do suggest variations in the comments!
In older version of pip, you can use this instead:
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
The grep is to skip editable («-e») package definitions, as suggested by @jawache. (Yes, you could replace grep+cut with sed or awk or perl or. ).
The -n1 flag for xargs prevents stopping everything if updating one package fails.
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook
Python: Create a bytearray from a list
Write a Python program to create a bytearray from a list.
Sample Solution:-
Python Code:
print() nums = [10, 20, 56, 35, 17, 99] # Create bytearray from list of integers. values = bytearray(nums) for x in values: print(x) print()
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource’s quiz.
Follow us on Facebook and Twitter for latest update.
Python: Tips of the Day
How to upgrade all Python packages with pip?
There isn’t a built-in flag yet, but you can use.
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
Note: there are infinite potential variations for this. I’m trying to keep this answer short and simple, but please do suggest variations in the comments!
In older version of pip, you can use this instead:
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
The grep is to skip editable («-e») package definitions, as suggested by @jawache. (Yes, you could replace grep+cut with sed or awk or perl or. ).
The -n1 flag for xargs prevents stopping everything if updating one package fails.
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook
Python bytearray()
The bytearray() method returns a bytearray object which is an array of the given bytes.
Example
prime_numbers = [2, 3, 5, 7]
# convert list to bytearray byte_array = bytearray(prime_numbers)print(byte_array) # Output: bytearray(b'\x02\x03\x05\x07')bytearray() Syntax
The syntax of bytearray() method is:
bytearray([source[, encoding[, errors]]])
bytearray() method returns a bytearray object (i.e. array of bytes) which is mutable (can be modified) sequence of integers in the range 0
If you want the immutable version, use the bytes() method.
bytearray() Parameters
bytearray() takes three optional parameters:
- source (Optional) — source to initialize the array of bytes.
- encoding (Optional) — if the source is a string, the encoding of the string.
- errors (Optional) — if the source is a string, the action to take when the encoding conversion fails (Read more: String encoding)
The source parameter can be used to initialize the byte array in the following ways:
Type Description String Converts the string to bytes using str.encode() Must also provide encoding and optionally errors Integer Creates an array of provided size, all initialized to null Object A read-only buffer of the object will be used to initialize the byte array Iterable Creates an array of size equal to the iterable count and initialized to the iterable elements Must be iterable of integers between 0 No source (arguments) Creates an array of size 0. bytearray() Return Value
The bytearray() method returns an array of bytes of the given size and initialization values.
Example 1: Array of bytes from a string
string = "Python is interesting."
# string with encoding 'utf-8' arr = bytearray(string, 'utf-8')print(arr)bytearray(b'Python is interesting.')
Example 2: Array of bytes of given integer size
size = 5
arr = bytearray(size)print(arr)bytearray(b'\x00\x00\x00\x00\x00')
Example 3: Array of bytes from an iterable list
rList = [1, 2, 3, 4, 5]
arr = bytearray(rList)print(arr)bytearray(b'\x01\x02\x03\x04\x05')